Class: LiveAPI::Search::Request
- Inherits:
-
Object
- Object
- LiveAPI::Search::Request
- Defined in:
- lib/live-search/request.rb
Constant Summary collapse
- API_DOMAIN =
"http://api.search.live.net"- FORMAT =
"json"- SOURCE =
"web"- DEFAULT_OFFSET =
0- DEFAULT_COUNT =
50- DEFAULT_MARKET =
"en-US"
Class Method Summary collapse
-
.hash_to_query(hash) ⇒ Object
Converts a hash to a query string.
Instance Method Summary collapse
-
#initialize(options) ⇒ Request
constructor
A new instance of Request.
-
#path ⇒ Object
The API URL call.
- #query_string ⇒ Object
-
#response ⇒ Object
The response object of the request.
-
#response_body ⇒ Object
The response body of the request.
-
#results ⇒ Object
The results from the response object.
-
#total ⇒ Object
The total number of results from the request.
Constructor Details
#initialize(options) ⇒ Request
Returns a new instance of Request.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/live-search/request.rb', line 16 def initialize() @options = @options[:AppId] = LiveAPI.application_id @options[:Sources] = SOURCE @options[:Count] = @options[:Count] ||= DEFAULT_COUNT @options[:Offset] = @options[:Offset] ||= DEFAULT_OFFSET @options[:Market] = @options[:Market] ||= DEFAULT_MARKET raise "Application ID is needed" if @options[:AppId].empty? raise "A query is needed" unless @options.has_key?(:Query) end |
Class Method Details
.hash_to_query(hash) ⇒ Object
Converts a hash to a query string
12 13 14 |
# File 'lib/live-search/request.rb', line 12 def self.hash_to_query(hash) hash.map {|key, value| "#{key}=#{value}"}.join("&") end |
Instance Method Details
#path ⇒ Object
The API URL call
29 30 31 32 33 34 35 |
# File 'lib/live-search/request.rb', line 29 def path @options["Web.Count"] = @options[:Count] @options["Web.Offset"] = @options[:Offset] @options.delete(:Count) @options.delete(:Offset) "#{API_DOMAIN}/#{FORMAT}.aspx?#{query_string}" end |
#query_string ⇒ Object
57 58 59 |
# File 'lib/live-search/request.rb', line 57 def query_string self.class.hash_to_query(@options) end |
#response ⇒ Object
The response object of the request
38 39 40 |
# File 'lib/live-search/request.rb', line 38 def response @response = @response ||= LiveAPI::Search::Response.new(response_body) end |
#response_body ⇒ Object
The response body of the request
43 44 45 |
# File 'lib/live-search/request.rb', line 43 def response_body open(path).readlines.join end |
#results ⇒ Object
The results from the response object
48 49 50 |
# File 'lib/live-search/request.rb', line 48 def results response.results end |
#total ⇒ Object
The total number of results from the request
53 54 55 |
# File 'lib/live-search/request.rb', line 53 def total response.total end |