Skip to content

Commit

Permalink
Merge pull request #19 from Gaddict/develop
Browse files Browse the repository at this point in the history
Implement tweet function
  • Loading branch information
pollseed committed Mar 16, 2015
2 parents 8912bac + 7c4910b commit 88e5baa
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
43 changes: 41 additions & 2 deletions app/controllers/admin/bot_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'oauth'

class Admin::BotController < ApplicationController

NG = "ng".freeze
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

# 論理削除されていないレコードを全取得
Expand Down
23 changes: 20 additions & 3 deletions app/views/admin/bot/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,34 @@

<h2>❏ 登録</h2>

<%= link_to "Sign in with Twitter", "/auth/twitter" %>

<div style="padding-bottom: 10px;">
<%= form_for @bot, url: {action: 'create'} do |f| %>
<%= render 'shared/error_messages' %>
<p style="color: green;"><%= notice %></p>
<p style="color: red;"><%= alert %></p>
<p><%= f.label :twitter_name, "twitter_name" %><%= f.text_field :twitter_name %></p>
<p><%= f.label :twitter_id, "twitter_id" %><%= f.text_field :twitter_id %></p>
<p><%= f.label :access_token, "access_token" %><%= f.text_field :access_token %></p>
<p><%= f.label :hash_tags, "hash_tags" %><%= f.number_field :hash_tags %></p>
<%= button_tag('bot登録') %>
<% end %>
</div>

<hr>

<h2>❏ TweetTest</h2>

<div style="padding-bottom: 10px;">
<%= form_tag "/bot/update" do %>
<p>Bot id<%= text_field :tweet, :bot_id %></p>
<p>Tweet<%= text_field :tweet, :content %></p>
<%= button_tag('Tweet') %>
<% end %>
</div>

<hr>

<h2>❏ 一覧</h2>

<div>
Expand All @@ -25,21 +38,25 @@
<table border="1">
<tr>
<th>No.</th>
<th>id</th>
<th>twitter_name</th>
<th>twitter_id</th>
<th>access_token</th>
<th>hash_tags</th>
<th>access_token</th>
<th>access_secret</th>
<th>複製</th>
<th>削除</th>
</tr>
<% @bots.each_with_index do |b,cnt| %>
<tr>
<%= form_tag do %>
<td><%= cnt+1 %></td>
<td><%= b.id %></td>
<td><%= text_field :bot, :twitter_name, value: b.twitter_name %></td>
<td><%= text_field :bot, :twitter_id, value: b.twitter_id %></td>
<td><%= text_field :bot, :access_token, value: b.access_token %></td>
<td><%= text_field :bot, :hash_tags, value: b.hash_tags %></td>
<td><%= b.access_token %></td>
<td><%= b.access_secret %></td>
<td><%= button_tag('bot複製') %></td>
<td><%= link_to('bot削除', "bot/#{b.id}", method: :delete) %></td>
<% end %>
Expand Down
16 changes: 16 additions & 0 deletions app/views/job/twitter/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1>TwitterTestView</h1>

<hr>

<h2>❏ CreateTest</h2>

<div style="padding-bottom: 10px;">
<%= form_tag "/twitter/update" do %>
<p>Consumer Key<%= f.text_field :con_key %></p>
<p>Consumer Secret<%= f.text_field :con_sec %></p>
<%= button_tag('bot登録') %>
<% end %>
</div>


</div>
4 changes: 4 additions & 0 deletions config/initializers/0_settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Settings < Settingslogic
source "#{Rails.root}/config/settings.yml"
namespace Rails.env
end
3 changes: 3 additions & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, Settings.twitter.consumer_key, Settings.twitter.consumer_secret
end
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 13 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defaults: &defaults

development:
<<: *defaults
twitter:
consumer_key: CTGO18drqCEpTMbPhYucIV3Hc
consumer_secret: 90EU6sv8Bw3PRgM8H0yLrAnXoo8jXZavVx1FsibiRe1DzQAlYw

test:
<<: *defaults

production:
<<: *defaults
5 changes: 5 additions & 0 deletions db/migrate/20150313230136_delete_column_pin_from_bots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class DeleteColumnPinFromBots < ActiveRecord::Migration
def change
remove_column :bots, :pin
end
end
5 changes: 5 additions & 0 deletions db/migrate/20150313230137_add_column_pin_to_bots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddColumnPinToBots < ActiveRecord::Migration
def change
add_column :bots, :pin, :integer
end
end
6 changes: 6 additions & 0 deletions db/migrate/20150313230138_add_column_acc_to_bots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddColumnAccToBots < ActiveRecord::Migration
def change
add_column :bots, :acc_key, :integer
add_column :bots, :acc_sec, :integer
end
end
5 changes: 5 additions & 0 deletions db/migrate/20150313_add_column_to_bots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddColumnToBots < ActiveRecord::Migration
def change
add_column :bots, :pin, :integer
end
end
5 changes: 5 additions & 0 deletions db/migrate/20150316125814_add_access_secret_to_bot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAccessSecretToBot < ActiveRecord::Migration
def change
add_column :bots, :access_secret, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20150316125910_remove_pin_from_bot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemovePinFromBot < ActiveRecord::Migration
def change
remove_column :bots, :pin, :integer
end
end
11 changes: 7 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit 88e5baa

Please sign in to comment.