Class: BingHelper::Bing

Inherits:
Object
  • Object
show all
Defined in:
lib/bing-helper/bing.rb

Constant Summary collapse

API_URL =
"http://api.search.live.net/json.aspx"
BASE_OPTIONS =
[:version, :market, :adult, :query, :appid, :sources]
QUERY_KEYWORDS =
[:site, :language, :contains, :filetype, :inanchor, :inbody, :intitle, :ip, :loc, :location, :prefer, :feed, :hasfeed, :url]
SOURCES =
%w(Ad Image InstantAnswer News Phonebook RelatedSearch Spell Web)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id, options = {}) ⇒ Bing

Returns a new instance of Bing.



55
56
57
# File 'lib/bing-helper/bing.rb', line 55

def initialize(app_id, options={})
  self.instance_options = options.merge(:AppId=>app_id)
end

Instance Attribute Details

#faraday_clientObject (readonly)

Returns the value of attribute faraday_client.



8
9
10
# File 'lib/bing-helper/bing.rb', line 8

def faraday_client
  @faraday_client
end

#instance_optionsObject

Returns the value of attribute instance_options.



7
8
9
# File 'lib/bing-helper/bing.rb', line 7

def instance_options
  @instance_options
end

Class Method Details

.base_urlObject



69
70
71
# File 'lib/bing-helper/bing.rb', line 69

def self.base_url
  return API_URL
end

Instance Method Details

#query_params_for(source, query, options) ⇒ Object



48
49
50
51
52
53
# File 'lib/bing-helper/bing.rb', line 48

def query_params_for(source, query, options)

  final_options = self.instance_options.merge({:sources=>source, :query=>build_query(query, options)})
  final_options.merge(clean_options(options))
  
end

#search(source, query, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/bing-helper/bing.rb', line 37

def search(source, query, options={})
  response = self.faraday_client.get do |req|
    req.url "", query_params_for(source, query, options)
  end
  begin
    Response.new(Yajl::Parser.new.parse(response.body))
  rescue
    nil
  end
end