diff --git a/Gemfile b/Gemfile index ecaa518..8a58725 100644 --- a/Gemfile +++ b/Gemfile @@ -13,6 +13,10 @@ gem 'turbolinks' gem 'jbuilder', '~> 2.0' gem 'sdoc', '~> 0.4.0', group: :doc +gem 'omniauth' +gem 'omniauth-twitter' +gem 'settingslogic' + gem "oauth" gem 'twitter' gem "tweetstream" diff --git a/Gemfile.lock b/Gemfile.lock index 24b3b24..4491147 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -83,6 +83,7 @@ GEM multipart-post (>= 1.2, < 3) globalid (0.3.3) activesupport (>= 4.1.0) + hashie (3.4.0) hike (1.2.3) http (0.6.3) http_parser.rb (~> 0.6.0) @@ -111,6 +112,15 @@ GEM nokogiri (1.6.6.2) mini_portile (~> 0.6.0) oauth (0.4.7) + omniauth (1.2.2) + hashie (>= 1.2, < 4) + rack (~> 1.0) + omniauth-oauth (1.0.1) + oauth + omniauth (~> 1.0) + omniauth-twitter (1.1.0) + multi_json (~> 1.3) + omniauth-oauth (~> 1.0) rack (1.6.0) rack-test (0.6.3) rack (>= 1.0) @@ -152,6 +162,7 @@ GEM sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) + settingslogic (2.0.9) simple_oauth (0.3.1) spring (1.3.3) sprockets (2.12.3) @@ -209,9 +220,12 @@ DEPENDENCIES jbuilder (~> 2.0) jquery-rails oauth + omniauth + omniauth-twitter rails (= 4.2.0) sass-rails (~> 5.0) sdoc (~> 0.4.0) + settingslogic spring sqlite3 turbolinks diff --git a/app/controllers/admin/bot_controller.rb b/app/controllers/admin/bot_controller.rb index 1b0fd9a..4822a0f 100644 --- a/app/controllers/admin/bot_controller.rb +++ b/app/controllers/admin/bot_controller.rb @@ -1,3 +1,5 @@ +require 'oauth' + class Admin::BotController < ApplicationController NG = "ng".freeze @@ -7,9 +9,36 @@ def index @bot = Bot.new end + def update + parameters = params.require(:tweet).permit :bot_id, :content + bot = Bot.find_by_id(parameters[:bot_id]) + + puts "bot_id" + parameters[:bot_id] + + client = Twitter::REST::Client.new do |config| + config.consumer_key = Settings.twitter.consumer_key + config.consumer_secret = Settings.twitter.consumer_secret + config.access_token = bot.access_token + config.access_token_secret = bot.access_secret + end + + puts "content" + parameters[:content] + + + client.update(parameters[:content]) + + redirect_to "/admin/bot" + end + def create bots - @bot = Bot.new(bot_params) + + paramerters = bot_params + + paramerters[:access_token] = session[:access_token] + paramerters[:access_secret] = session[:access_secret] + + @bot = Bot.new(paramerters) render action: 'admin/bot/index', alert: "登録失敗!!" if @bot.nil? if @bot.save @@ -29,13 +58,23 @@ def destroy end end + def callback + auth = request.env["omniauth.auth"] + token = auth[:credentials] + + session[:access_token] = token[:token] + session[:access_secret] = token[:secret] + + redirect_to "/admin/bot" + end + private def bots @bots = Bot.find_by end def bot_params - params.require(:bot).permit :twitter_name, :twitter_id, :access_token, :hash_tags + params.require(:bot).permit :twitter_name, :twitter_id, :hash_tags end def find_destroy_bot diff --git a/app/models/bot.rb b/app/models/bot.rb index 66aac7c..3ba3d7f 100644 --- a/app/models/bot.rb +++ b/app/models/bot.rb @@ -4,6 +4,7 @@ class Bot < ActiveRecord::Base validates :twitter_name, presence: true validates :twitter_id, presence: true validates :access_token, presence: true + validates :access_secret, presence: true validates :hash_tags, presence: true # 論理削除されていないレコードを全取得 diff --git a/app/views/admin/bot/index.html.erb b/app/views/admin/bot/index.html.erb index d34adb3..ab54314 100644 --- a/app/views/admin/bot/index.html.erb +++ b/app/views/admin/bot/index.html.erb @@ -2,6 +2,8 @@

❏ 登録

+<%= link_to "Sign in with Twitter", "/auth/twitter" %> +
<%= form_for @bot, url: {action: 'create'} do |f| %> <%= render 'shared/error_messages' %> @@ -9,7 +11,6 @@

<%= alert %>

<%= f.label :twitter_name, "twitter_name" %><%= f.text_field :twitter_name %>

<%= f.label :twitter_id, "twitter_id" %><%= f.text_field :twitter_id %>

-

<%= f.label :access_token, "access_token" %><%= f.text_field :access_token %>

<%= f.label :hash_tags, "hash_tags" %><%= f.number_field :hash_tags %>

<%= button_tag('bot登録') %> <% end %> @@ -17,6 +18,18 @@
+

❏ TweetTest

+ +
+<%= form_tag "/bot/update" do %> +

Bot id<%= text_field :tweet, :bot_id %>

+

Tweet<%= text_field :tweet, :content %>

+<%= button_tag('Tweet') %> +<% end %> +
+ +
+

❏ 一覧

@@ -25,10 +38,12 @@ + - + + @@ -36,10 +51,12 @@ <%= form_tag do %> + - + + <% end %> diff --git a/app/views/job/twitter/index.html.erb b/app/views/job/twitter/index.html.erb new file mode 100644 index 0000000..2013063 --- /dev/null +++ b/app/views/job/twitter/index.html.erb @@ -0,0 +1,16 @@ +

TwitterTestView

+ +
+ +

❏ CreateTest

+ +
+<%= form_tag "/twitter/update" do %> +

Consumer Key<%= f.text_field :con_key %>

+

Consumer Secret<%= f.text_field :con_sec %>

+<%= button_tag('bot登録') %> +<% end %> +
+ + + diff --git a/config/initializers/0_settings.rb b/config/initializers/0_settings.rb new file mode 100644 index 0000000..7bd640f --- /dev/null +++ b/config/initializers/0_settings.rb @@ -0,0 +1,4 @@ +class Settings < Settingslogic + source "#{Rails.root}/config/settings.yml" + namespace Rails.env +end diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 0000000..68eb01a --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1,3 @@ +Rails.application.config.middleware.use OmniAuth::Builder do + provider :twitter, Settings.twitter.consumer_key, Settings.twitter.consumer_secret +end diff --git a/config/routes.rb b/config/routes.rb index aee5e98..d45f667 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,9 @@ Rails.application.routes.draw do + + match '/auth/twitter/callback', to: 'admin/bot#callback', via: 'get' + + match '/bot/update', to: 'admin/bot#update', via: 'post' + # 管理機能 namespace :admin do # ADMIN TOP diff --git a/config/settings.yml b/config/settings.yml new file mode 100644 index 0000000..c20e09d --- /dev/null +++ b/config/settings.yml @@ -0,0 +1,13 @@ +defaults: &defaults + +development: + <<: *defaults + twitter: + consumer_key: CTGO18drqCEpTMbPhYucIV3Hc + consumer_secret: 90EU6sv8Bw3PRgM8H0yLrAnXoo8jXZavVx1FsibiRe1DzQAlYw + +test: + <<: *defaults + +production: + <<: *defaults diff --git a/db/migrate/20150313230136_delete_column_pin_from_bots.rb b/db/migrate/20150313230136_delete_column_pin_from_bots.rb new file mode 100644 index 0000000..43f34c1 --- /dev/null +++ b/db/migrate/20150313230136_delete_column_pin_from_bots.rb @@ -0,0 +1,5 @@ +class DeleteColumnPinFromBots < ActiveRecord::Migration + def change + remove_column :bots, :pin + end +end diff --git a/db/migrate/20150313230137_add_column_pin_to_bots.rb b/db/migrate/20150313230137_add_column_pin_to_bots.rb new file mode 100644 index 0000000..f43ad19 --- /dev/null +++ b/db/migrate/20150313230137_add_column_pin_to_bots.rb @@ -0,0 +1,5 @@ +class AddColumnPinToBots < ActiveRecord::Migration + def change + add_column :bots, :pin, :integer + end +end diff --git a/db/migrate/20150313230138_add_column_acc_to_bots.rb b/db/migrate/20150313230138_add_column_acc_to_bots.rb new file mode 100644 index 0000000..e7074b5 --- /dev/null +++ b/db/migrate/20150313230138_add_column_acc_to_bots.rb @@ -0,0 +1,6 @@ +class AddColumnAccToBots < ActiveRecord::Migration + def change + add_column :bots, :acc_key, :integer + add_column :bots, :acc_sec, :integer + end +end diff --git a/db/migrate/20150313_add_column_to_bots.rb b/db/migrate/20150313_add_column_to_bots.rb new file mode 100644 index 0000000..4d697db --- /dev/null +++ b/db/migrate/20150313_add_column_to_bots.rb @@ -0,0 +1,5 @@ +class AddColumnToBots < ActiveRecord::Migration + def change + add_column :bots, :pin, :integer + end +end diff --git a/db/migrate/20150316125814_add_access_secret_to_bot.rb b/db/migrate/20150316125814_add_access_secret_to_bot.rb new file mode 100644 index 0000000..c948c39 --- /dev/null +++ b/db/migrate/20150316125814_add_access_secret_to_bot.rb @@ -0,0 +1,5 @@ +class AddAccessSecretToBot < ActiveRecord::Migration + def change + add_column :bots, :access_secret, :string + end +end diff --git a/db/migrate/20150316125910_remove_pin_from_bot.rb b/db/migrate/20150316125910_remove_pin_from_bot.rb new file mode 100644 index 0000000..dbe3022 --- /dev/null +++ b/db/migrate/20150316125910_remove_pin_from_bot.rb @@ -0,0 +1,5 @@ +class RemovePinFromBot < ActiveRecord::Migration + def change + remove_column :bots, :pin, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index a9bd3ca..5ec3252 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,16 +11,19 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150310151917) do +ActiveRecord::Schema.define(version: 20150316125910) do create_table "bots", force: :cascade do |t| t.string "twitter_name" t.string "twitter_id" t.string "access_token" - t.boolean "deleted", default: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.boolean "deleted", default: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.integer "hash_tags" + t.integer "acc_key" + t.integer "acc_sec" + t.string "access_secret" end create_table "hash_tags", force: :cascade do |t|
No.id twitter_name twitter_idaccess_token hash_tagsaccess_tokenaccess_secret 複製 削除
<%= cnt+1 %><%= b.id %> <%= text_field :bot, :twitter_name, value: b.twitter_name %> <%= text_field :bot, :twitter_id, value: b.twitter_id %><%= text_field :bot, :access_token, value: b.access_token %> <%= text_field :bot, :hash_tags, value: b.hash_tags %><%= b.access_token %><%= b.access_secret %> <%= button_tag('bot複製') %> <%= link_to('bot削除', "bot/#{b.id}", method: :delete) %>