Class: GoogleRest::Request

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/google_rest.rb

Defined Under Namespace

Modules: Util

Constant Summary collapse

API_VERSION =
"1.0"
API_URL =
{
  :feed_lookup => "/feed/lookup",
  :feed_load => "/feed/load",
  :web => "/search/web",
  :blog => "/search/blogs",
  :news => "/search/news",
  :image => "/search/images",
  :local => "/search/local",
  :video => "/search/video"
}
@@ascii_available =
"".respond_to?(:to_ascii)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(userip = nil) ⇒ Request

Returns a new instance of Request.



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/google_rest.rb', line 93

def initialize(userip = nil)
  path = Rails.root.join("config/google_rest.yml")
  if !File.exists?(path)
    raise StandardError, "Missing config file: #{path}"
  else
    config = YAML.load_file(path) || {}
    data = config[Rails.env.to_s] || config
    self.api_key = data['api_key']
    self.referer = data['referer']
    self.userip = userip
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



75
76
77
# File 'lib/google_rest.rb', line 75

def api_key
  @api_key
end

#refererObject

Returns the value of attribute referer.



76
77
78
# File 'lib/google_rest.rb', line 76

def referer
  @referer
end

#useripObject

Returns the value of attribute userip.



77
78
79
# File 'lib/google_rest.rb', line 77

def userip
  @userip
end

Instance Method Details

#api_key?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/google_rest.rb', line 157

def api_key?
  !self.api_key.blank?
end

#blog_search(query, options = {}) ⇒ Object



123
124
125
# File 'lib/google_rest.rb', line 123

def blog_search(query, options = {})
  common_search(:blog, {:scoring => 'd', :rsz => 'large', :q => query}.merge(options))
end

#feed_load(feed_url, count_entries = false) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/google_rest.rb', line 112

def feed_load(feed_url, count_entries = false)
  return nil if feed_url.blank?
  if count_entries == false
    # we retrieve the current feed entries
    res = google_request(:feed_load, {:q => feed_url, :num => -1})
  else
    res = google_request(:feed_load, {:q => feed_url, :num => count_entries, :scoring => 'h'} )
  end
  res.empty? ? nil : res.raw["feed"]
end

#feed_lookup(website) ⇒ Object



106
107
108
109
110
# File 'lib/google_rest.rb', line 106

def feed_lookup(website)
  return nil if website.blank?
  res = google_request(:feed_lookup, {:q => website.to_s.gsub(/^https?:\/\//i, '')})
  res.empty? ? nil : res.raw["url"]
end

#image_search(query, options = {}) ⇒ Object



135
136
137
# File 'lib/google_rest.rb', line 135

def image_search(query, options = {})
  common_search(:image, {:q => query}.merge(options))
end


147
148
149
150
# File 'lib/google_rest.rb', line 147

def inbound_links(url)
  res = common_search(:web, {:q => "link:#{url.gsub(/^https?:\/\//,'')}"})
  res.cursor.blank? ? 0 : res.cursor["estimatedResultCount"].to_i
end

#indexed_pages(url) ⇒ Object



152
153
154
155
# File 'lib/google_rest.rb', line 152

def indexed_pages(url)
  res = common_search(:web, {:q => "site:#{url.gsub(/^https?:\/\//,'')}"})
  res.cursor.blank? ? 0 : res.cursor["estimatedResultCount"].to_i
end

#local_search(query, options = {}) ⇒ Object



143
144
145
# File 'lib/google_rest.rb', line 143

def local_search(query, options = {})
  common_search(:local, {:q => query}.merge(options))
end

#news_search(query, options = {}) ⇒ Object



131
132
133
# File 'lib/google_rest.rb', line 131

def news_search(query, options = {})
  common_search(:news, {:rsz => 'large', :scoring => 'd', :q => query}.merge(options))
end

#userip?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/google_rest.rb', line 161

def userip?
  !self.userip.blank?
end

#video_search(query, options = {}) ⇒ Object



139
140
141
# File 'lib/google_rest.rb', line 139

def video_search(query, options = {})
  common_search(:video, {:q => query}.merge(options))
end

#web_search(query, options = {}) ⇒ Object



127
128
129
# File 'lib/google_rest.rb', line 127

def web_search(query, options = {})
  common_search(:web, {:rsz => 'large', :q => query}.merge(options))
end