Skip to content

Commit

Permalink
twitter import & others
Browse files Browse the repository at this point in the history
  • Loading branch information
vanakenm committed Dec 15, 2017
1 parent 96effa5 commit 3006024
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@
/yarn-error.log

.byebug_history

# Ignore application configuration
/config/application.yml
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ gem 'pg', '~> 0.18'
gem 'pg_search'

gem 'httparty'
gem 'figaro'
gem 'twitter'
gem 'bootstrap-sass', '~> 3.3.7'
gem 'jquery-rails'

# Use Puma as the app server
gem 'puma', '~> 3.7'
Expand Down
47 changes: 47 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ GEM
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
arel (8.0.0)
autoprefixer-rails (7.1.1)
execjs
bindex (0.5.0)
bootstrap-sass (3.3.7)
autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4)
buftok (0.2.0)
builder (3.2.3)
byebug (9.1.0)
capybara (2.16.1)
Expand All @@ -62,18 +68,36 @@ GEM
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
crass (1.0.3)
domain_name (0.5.20170404)
unf (>= 0.0.5, < 1.0.0)
equalizer (0.0.11)
erubi (1.7.0)
execjs (2.7.0)
ffi (1.9.18)
figaro (1.1.1)
thor (~> 0.14)
globalid (0.4.1)
activesupport (>= 4.2.0)
http (3.0.0)
addressable (~> 2.3)
http-cookie (~> 1.0)
http-form_data (>= 2.0.0.pre.pre2, < 3)
http_parser.rb (~> 0.6.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
http-form_data (2.0.0)
http_parser.rb (0.6.0)
httparty (0.14.0)
multi_xml (>= 0.5.2)
i18n (0.9.1)
concurrent-ruby (~> 1.0)
jbuilder (2.7.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
jquery-rails (4.3.1)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -83,12 +107,16 @@ GEM
nokogiri (>= 1.5.9)
mail (2.7.0)
mini_mime (>= 0.1.1)
memoizable (0.4.2)
thread_safe (~> 0.3, >= 0.3.1)
method_source (0.9.0)
mini_mime (1.0.0)
mini_portile2 (2.3.0)
minitest (5.10.3)
multi_json (1.12.2)
multi_xml (0.6.0)
multipart-post (2.0.0)
naught (1.1.0)
nio4r (2.1.0)
nokogiri (1.8.1)
mini_portile2 (~> 2.3.0)
Expand Down Expand Up @@ -145,6 +173,7 @@ GEM
selenium-webdriver (3.8.0)
childprocess (~> 0.5)
rubyzip (~> 1.0)
simple_oauth (0.3.1)
spring (2.0.2)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
Expand All @@ -163,10 +192,24 @@ GEM
turbolinks (5.0.1)
turbolinks-source (~> 5)
turbolinks-source (5.0.3)
twitter (6.2.0)
addressable (~> 2.3)
buftok (~> 0.2.0)
equalizer (~> 0.0.11)
http (~> 3.0)
http-form_data (~> 2.0)
http_parser.rb (~> 0.6.0)
memoizable (~> 0.4.0)
multipart-post (~> 2.0)
naught (~> 1.0)
simple_oauth (~> 0.3.0)
tzinfo (1.2.4)
thread_safe (~> 0.1)
uglifier (4.0.2)
execjs (>= 0.3.0, < 3)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.4)
web-console (3.5.1)
actionview (>= 5.0)
activemodel (>= 5.0)
Expand All @@ -182,11 +225,14 @@ PLATFORMS
ruby

DEPENDENCIES
bootstrap-sass (~> 3.3.7)
byebug
capybara (~> 2.13)
coffee-rails (~> 4.2)
figaro
httparty
jbuilder (~> 2.5)
jquery-rails
listen (>= 3.0.5, < 3.2)
pg (~> 0.18)
pg_search
Expand All @@ -197,6 +243,7 @@ DEPENDENCIES
spring
spring-watcher-listen (~> 2.0.0)
turbolinks (~> 5)
twitter
tzinfo-data
uglifier (>= 1.3.0)
web-console (>= 3.3.0)
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/commands.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Place all the styles related to the Commands controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

@import "bootstrap-sprockets";
@import "bootstrap";
2 changes: 1 addition & 1 deletion app/controllers/commands_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def contents(quote)
"attachments": [
{
"title": quote.contents,
"title_link": quote.url,
"title_link": quote.twitter_url,
"image_url": quote.url
}
]
Expand Down
35 changes: 32 additions & 3 deletions app/controllers/effin_quotes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,43 @@ def index
@quotes = EffinQuote.all
end

def check
@quote = EffinQuote.incomplete.first
@count = EffinQuote.incomplete.count

render :show
end

def show
@quote = EffinQuote.find(params[:id])
@count = EffinQuote.incomplete.count
end

def find
@quote = EffinQuote.find_by_word(params[:query])
@quote = EffinQuote.all.sample unless @quote
def incomplete
@quotes = EffinQuote.incomplete

render :index
end

def update
@quote = EffinQuote.find(params[:id])
@quote.update!(quote_params)

redirect_to check_effin_quotes_path
end

def find
@quote = EffinQuote.find_by_word(params[:query]) || EffinQuote.all.sample
redirect_to @quote
end

def destroy
@quote = EffinQuote.find(params[:id])
@quote.destroy
redirect_to check_effin_quotes_path
end

def quote_params
params.require(:effin_quote).permit(:contents)
end
end
14 changes: 13 additions & 1 deletion app/models/effin_quote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@ class EffinQuote < ApplicationRecord
include PgSearch
pg_search_scope :search_by_word, against: [ :contents ]

def complete?
url && twitter_url && contents
end

def self.complete
where("url is not null and twitter_url is not null and contents is not null")
end

def self.incomplete
where("url is null or twitter_url is null or contents is null")
end

def self.find_by_word(word)
search_by_word(word).first
complete.search_by_word(word).first
end
end
25 changes: 20 additions & 5 deletions app/views/effin_quotes/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
<h1>Effin Quotes</h1>
<ul>

<table class="table table-striped">
<tbody>

<% @quotes.each do |quote| %>
<li>
<%= link_to quote.contents, quote %>
</li>
<tr>
<td>
<%= link_to quote.id, quote %>
</td>
<td>
<%= quote.contents %>
</td>
<td>
<%= quote.url %>
</td>
<td>
<%= quote.twitter_url %>
</td>
</tr>
<% end %>
</ul>
</tbody>
</table>
19 changes: 18 additions & 1 deletion app/views/effin_quotes/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
<h1>Effin Quote</h1>

<%= image_tag(@quote.url) %>
<p><%= @count %> incomplete quotes</p>

<p>
URL:
<%= @quote.twitter_url %>
</p>

<p>
<%= @quote.contents %>
</p>

<%= form_for(@quote) do |f| %>
<%= f.text_field :contents %>
<%= f.submit %>
<% end %>

<%= link_to "Destroy", @quote, method: :delete, class: "btn btn-danger" %>

<p>
<%= image_tag(@quote.url) %>
</p>

2 changes: 1 addition & 1 deletion app/views/effin_quotes/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1 +1 @@
json.extract! @quote, :id, :contents, :url
json.extract! @quote, :id, :contents, :url, :twitter_url
4 changes: 3 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
</head>

<body>
<%= yield %>
<div class="container">
<%= yield %>
</div>
</body>
</html>
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

root to: "effin_quotes#index"

resources :effin_quotes, only: [:index, :show] do
resources :effin_quotes, only: [:index, :show, :destroy, :update] do
collection do
get :find
get :incomplete
get :check
end
end

Expand Down
36 changes: 36 additions & 0 deletions db/from_tweets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
client = Twitter::REST::Client.new do |config|
config.consumer_key = ENV("TWITTER_CONSUMER_KEY")
config.consumer_secret = ENV("TWITTER_CONSUMER_SECRET")
config.access_token = ENV("TWITTER_ACCESS_TOKEN")
config.access_token_secret = ENV("TWITTER_ACCESS_TOKEN_SECRET")
end

tweets = client.get_all_tweets("effinbirds")

raw = tweets.map do |t|
next unless t.media && t.media.first && t.media.first.uri
image_url = t.media.first.media_uri
tweet_url = t.uri

[image_url, tweet_url]
end.compact

raw.each do |r|
next if EffinQuote.find_by(url: r.first.to_s)

EffinQuote.create(url: r.first.to_s, twitter_url: r.second.to_s)
end

def collect_with_max_id(collection=[], max_id=nil, &block)
response = yield(max_id)
collection += response
response.empty? ? collection.flatten : collect_with_max_id(collection, response.last.id - 1, &block)
end

def client.get_all_tweets(user)
collect_with_max_id do |max_id|
options = {count: 200, include_rts: true}
options[:max_id] = max_id unless max_id.nil?
user_timeline(user, options)
end
end
5 changes: 5 additions & 0 deletions db/migrate/20171215110042_add_twitter_url_to_effin_quotes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTwitterUrlToEffinQuotes < ActiveRecord::Migration[5.1]
def change
add_column :effin_quotes, :twitter_url, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20171212203743) do
ActiveRecord::Schema.define(version: 20171215110042) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -20,6 +20,7 @@
t.string "url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "twitter_url"
end

end
Loading

0 comments on commit 3006024

Please sign in to comment.