-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
160 lines (134 loc) · 4.28 KB
/
app.rb
File metadata and controls
160 lines (134 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require 'json'
require 'sinatra'
require 'simple_oauth'
require 'excon'
require 'gon-sinatra'
require 'uri'
require 'date'
require_relative 'models'
Sinatra::register Gon::Sinatra
if ENV['RACK_ENV'] != "production"
require 'dotenv'
Dotenv.load ".env"
end
def show_params
p "params are #{params}"
end
##### twitter api
def tweets_hashtag(hashtag)
hashtag[0] == "#" ? search_term = hashtag : search_term = ("#" + "#{hashtag}")
p "search term is #{search_term}"
twitter_api_search(search_term, :count => 100)
end
def twitter_api_url(endpoint, version = '1.1')
"https://api.twitter.com/#{version}#{endpoint}"
end
def twitter_api_search(search_term, params = {})
twitter_api_get_request("/search/tweets.json", params.merge(:q => URI.encode("#{search_term}")))
end
def twitter_api_get_request(endpoint, params, consumer_key = ENV['TWITTER_API_KEY'], consumer_secret = ENV['TWITTER_API_SECRET'])
twitter_api_request("get", endpoint, params, consumer_key, consumer_secret)
end
def twitter_api_request(method, endpoint, params, consumer_key = ENV['TWITTER_API_KEY'], consumer_secret = ENV['TWITTER_API_SECRET'])
api_url = twitter_api_url(endpoint)
authorization_header = SimpleOAuth::Header.new(method,
api_url,
params,
{ :consumer_key => consumer_key,
:consumer_secret => consumer_secret})
response = Excon.send(method, api_url, {
:query => params,
:headers => { "Authorization" => authorization_header.to_s }
})
response = JSON.parse(response.body)
if response.respond_to?(:has_key?) && response.has_key?("errors")
messages = []
response["errors"].each do |error|
messages.push(error["message"])
end
raise "Oops, errors! #{messages.join("\n")}"
else
return response
end
end
def query_twitter_api(search_word)
response = tweets_hashtag(search_word)
@tweets = response["statuses"]
@report = Report.new(@tweets, search_word)
@report.calc_average_word_count
@report.save_report_data
end
##### Report class
class Report
attr_reader :tweets_hash, :tweets_text_array, :run_at, :average_word_count
attr_accessor :words_count
def initialize(tweets, tag)
@tag = tag
@run_at = Time.now
@tweets_hash = tweets
@tweets_text_array = []
@tweets_hash.each { |tweet| @tweets_text_array << tweet["text"] }
@words_count = @tweets_text_array.map { |string| string.split(" ").count }
@average_word_count = 0
end
def calc_average_word_count
total_words = 0
@tweets_text_array.each do |string|
total_words += string.split(" ").count
end
if @tweets_text_array.count == 0
@average_word_count = 0
else
@average_word_count = total_words.to_f / @tweets_text_array.count.to_f
end
end
def min_word_count
@words_count.min
end
def max_word_count
@words_count.max
end
def total_tweets
@tweets_text_array.count
end
def save_report_data
report_data_attributes = {
"tag_name" => @tag,
"created_at" => @run_at,
"average_word_count" => @average_word_count,
"min_word_count" => @words_count.min,
"max_word_count" => @words_count.max,
"total_tweets" => @tweets_text_array.count
}
Report_data.create(report_data_attributes)
end
end
##### routes
get('/') do
@tweets = []
@report = Report.new({}, "")
@report.words_count = [0]
erb :home
end
get('/tweets_hashtag') do
show_params
@search_word = params[:q]
@saved_report = Report_data.last(:tag_name => @search_word)
if @search_word == ""
redirect("/")
elsif !@saved_report
query_twitter_api(@search_word)
gon.report = [@report.min_word_count, @report.max_word_count, @report.average_word_count]
erb(:home)
elsif @saved_report && (DateTime.now - @saved_report.created_at) * 24.0 <= 1.0
gon.report = [@saved_report.min_word_count, @saved_report.max_word_count, @saved_report.average_word_count]
erb(:cached_report)
elsif @saved_report && (DateTime.now - @saved_report.created_at) * 24.0 > 1.0
query_twitter_api(@search_word)
gon.report = [@report.min_word_count, @report.max_word_count, @report.average_word_count]
erb(:home)
end
end
get('/home') do
redirect("/")
end