Class: Overlord::GoogleBase

Inherits:
Object
  • Object
show all
Extended by:
JsonParser
Defined in:
lib/overlord/google_base.rb

Direct Known Subclasses

GoogleFeedRequest, GoogleSearchRequest

Class Method Summary collapse

Methods included from JsonParser

parse_json, try_active_json, try_crack, try_json

Class Method Details

.api_key_idObject



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(query_options)
  key = self.api_key_id || Overlord.configuration.google_ajax_api_key rescue ''
  query_options[:v] = '1.0'
  query_options[:key] = key if key
  query_options[:userip] = self.user_ip if self.user_ip
  query_options
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, options = {})
  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(options[:headers]) if options[:headers]
  # to_params comes from the httparty gem
  params = "?#{options[:query].to_params}" if options[: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

.refererObject



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_ipObject



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