Class: Gnip::GnipFullArchive::FullArchive
- Inherits:
-
Object
- Object
- Gnip::GnipFullArchive::FullArchive
- Includes:
- HTTParty
- Defined in:
- lib/gnip/gnip-full-archive/full_archive.rb
Direct Known Subclasses
Defined Under Namespace
Classes: InvalidRequestException
Instance Attribute Summary collapse
-
#counts_url ⇒ Object
readonly
Returns the value of attribute counts_url.
-
#search_url ⇒ Object
readonly
Returns the value of attribute search_url.
Instance Method Summary collapse
-
#initialize(client) ⇒ FullArchive
constructor
alias :total :total_entries.
- #search(options = {}) ⇒ Object
-
#total(options = {}) ⇒ Object
return total contents in a specific date interval with a passed query.
-
#total_by_time_period(options = {}) ⇒ Object
full aarchive search endpoints return total contents by day, minute, hour paginated so to get totals across time period passed may need to run more than one call, the stop condition is cursor nil bucket: must be one of [minute, hour, day].
Constructor Details
#initialize(client) ⇒ FullArchive
alias :total :total_entries
15 16 17 18 19 |
# File 'lib/gnip/gnip-full-archive/full_archive.rb', line 15 def initialize(client) @search_url = "https://data-api.twitter.com/search/fullarchive/accounts/#{client.account}/#{client.label}.json" @counts_url = "https://data-api.twitter.com/search/fullarchive/accounts/#{client.account}/#{client.label}/counts.json" @auth = { username: client.username, password: client.password } end |
Instance Attribute Details
#counts_url ⇒ Object (readonly)
Returns the value of attribute counts_url.
11 12 13 |
# File 'lib/gnip/gnip-full-archive/full_archive.rb', line 11 def counts_url @counts_url end |
#search_url ⇒ Object (readonly)
Returns the value of attribute search_url.
11 12 13 |
# File 'lib/gnip/gnip-full-archive/full_archive.rb', line 11 def search_url @search_url end |
Instance Method Details
#search(options = {}) ⇒ Object
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 |
# File 'lib/gnip/gnip-full-archive/full_archive.rb', line 27 def search( = {}) = {} [:query] = [:query] || '' [:maxResults] = [:per_page] || 500 [:fromDate] = Gnip.format_date([:date_from]) if [:date_from] [:toDate] = Gnip.format_date([:date_to]) if [:date_to] [:next] = [:next_cursor] if [:next_cursor] url = [search_url, .to_query].join('?') begin gnip_call = self.class.get(url, basic_auth: @auth) response = gnip_call.response parsed_response = gnip_call.parsed_response parsed_response = (parsed_response || {}).with_indifferent_access raise response. unless parsed_response.present? if parsed_response[:error].present? response = { results: [], next: nil, url: url, error: parsed_response[:error][:message], code: response.code.to_i } else response = { results: parsed_response[:results], url: url, next: parsed_response[:next], code: response.code.to_i } end rescue StandardError => e response = { results: [], url: url, next: nil, error: e., code: 500 } end response end |
#total(options = {}) ⇒ Object
return total contents in a specific date interval with a passed query
112 113 114 115 116 117 |
# File 'lib/gnip/gnip-full-archive/full_archive.rb', line 112 def total( = {}) extra = {} response = total_by_time_period() extra = { error: response[:error] } if response[:error].present? { query: [:query], total: response[:results].map { |item| item[:count] }.reduce(:+), calls: response[:calls] }.merge!(extra) end |
#total_by_time_period(options = {}) ⇒ Object
full aarchive search endpoints return total contents by day, minute, hour paginated so to get totals across time period passed may need to run more than one call, the stop condition is cursor nil bucket: must be one of [minute, hour, day]
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 |
# File 'lib/gnip/gnip-full-archive/full_archive.rb', line 67 def total_by_time_period( = {}) response = [:response] || {} = {} [:query] = [:query] || '' [:bucket] = [:bucket] || 'day' [:fromDate] = Gnip.format_date([:date_from]) if [:date_from] [:toDate] = Gnip.format_date([:date_to]) if [:date_to] [:next] = [:next_cursor] if [:next_cursor] url = [counts_url, .to_query].join('?') call_done = 0 begin gnip_call = self.class.get(url, basic_auth: @auth) parsed_response = gnip_call.parsed_response parsed_response = (parsed_response || {}).with_indifferent_access raise gnip_call.response. unless parsed_response.present? if parsed_response[:error].present? response = { results: [], next: nil, error: parsed_response[:error][:message], code: gnip_call.response.code.to_i, calls: (response[:calls] || 0) + 1 } else call_done = 1 # we have received a valid response parsed_response[:results].each_with_index do |item, i| parsed_response[:results][i] = item.merge(timePeriod: DateTime.parse(item[:timePeriod]).to_s) end response = { results: (response[:results] || []) + parsed_response[:results], next: parsed_response[:next], code: gnip_call.response.code.to_i, calls: (response[:calls] || 0) + 1 } end rescue StandardError => e response = { results: [], next: nil, error: e., code: 500, calls: (response[:calls] || 0) + call_done } end # If the next cursor is not present we fetched all the data # It happens that twitter returns the same cursor, in that case we stop return response if !parsed_response[:next].to_s.present? || (parsed_response[:next].to_s.present? && parsed_response[:next] == [:next]) total_by_time_period(query: [:query], date_from: [:fromDate], date_to: [:toDate], bucket: [:bucket], next_cursor: parsed_response[:next], response: response) end |