Class: Overlord::GoogleBase
- Inherits:
-
Object
- Object
- Overlord::GoogleBase
- Extended by:
- JsonParser
- Defined in:
- lib/overlord/google_base.rb
Direct Known Subclasses
Class Method Summary collapse
- .api_key_id ⇒ Object
-
.api_key_id=(val) ⇒ Object
api_key_id: Valid Google access key (optional).
-
.build_google_query(query_options) ⇒ Object
Add standard items to the google query.
-
.get(uri, options = {}) ⇒ Object
Gets and parses json from a given url uri: Uri from which to obtain json options: Options to be sent :query - query :headers - headers.
- .parse_name(name) ⇒ Object
- .referer ⇒ Object
-
.referer=(val) ⇒ Object
referer: Url of the website making request.
- .user_ip ⇒ Object
-
.user_ip=(val) ⇒ Object
userip: Ip address of user on whose behave a request is made.
Methods included from JsonParser
parse_json, try_active_json, try_crack, try_json
Class Method Details
.api_key_id ⇒ Object
12 13 14 |
# File 'lib/overlord/google_base.rb', line 12 def self.api_key_id @api_key_id end |
.api_key_id=(val) ⇒ Object
api_key_id: Valid Google access key (optional)
8 9 10 |
# File 'lib/overlord/google_base.rb', line 8 def self.api_key_id=(val) @api_key_id = val end |
.build_google_query(query_options) ⇒ Object
Add standard items to the google query
35 36 37 38 39 40 41 |
# File 'lib/overlord/google_base.rb', line 35 def self.build_google_query() key = self.api_key_id || Overlord.configuration.google_ajax_api_key rescue '' [:v] = '1.0' [:key] = key if key [:userip] = self.user_ip if self.user_ip end |
.get(uri, options = {}) ⇒ Object
Gets and parses json from a given url uri: Uri from which to obtain json options: Options to be sent
:query - query
:headers - headers
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/overlord/google_base.rb', line 48 def self.get(uri, = {}) header_params = { "UserAgent" => "Ruby/#{RUBY_VERSION}" } ref = self.referer || Overlord.configuration.request_referer rescue nil header_params["Referer"] = ref if ref header_params = header_params.merge([:headers]) if [:headers] # to_params comes from the httparty gem params = "?#{[:query].to_params}" if [:query] buffer = open("#{uri}#{params}", header_params).read return parse_json(buffer) || {} end |
.parse_name(name) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/overlord/google_base.rb', line 60 def self.parse_name(name) return '' if name.blank? names = name.split(' ') return '' if names.length <= 0 return [names[0], names[0]] if names.length == 1 [names[0], names.slice(1, names.length).join(' ')] end |
.referer ⇒ Object
21 22 23 |
# File 'lib/overlord/google_base.rb', line 21 def self.referer @referer end |
.referer=(val) ⇒ Object
referer: Url of the website making request. Required by Google.
17 18 19 |
# File 'lib/overlord/google_base.rb', line 17 def self.referer=(val) @referer = val end |
.user_ip ⇒ Object
30 31 32 |
# File 'lib/overlord/google_base.rb', line 30 def self.user_ip @userip end |
.user_ip=(val) ⇒ Object
userip: Ip address of user on whose behave a request is made. Helps reduce the chance that google will ban the server.
26 27 28 |
# File 'lib/overlord/google_base.rb', line 26 def self.user_ip=(val) @userip = val end |