Class: Yahoo::SE::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/yahoo-se/request.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_path, options) ⇒ Request

Returns a new instance of Request.



13
14
15
16
17
18
19
# File 'lib/yahoo-se/request.rb', line 13

def initialize(service_path, options)
  @service_path = service_path
  @options = options
  @options[:appid] = Yahoo::SE.application_id
  @options[:output] = "json"
  @query_string = self.class.hash_to_query(@options)
end

Class Method Details

.hash_to_query(hash) ⇒ Object

Converts a hash to a query string



9
10
11
# File 'lib/yahoo-se/request.rb', line 9

def self.hash_to_query(hash)
  hash.map {|key, value| "#{key}=#{value}"}.join("&")
end

Instance Method Details

#pathObject

The API URL call



22
23
24
# File 'lib/yahoo-se/request.rb', line 22

def path
  "#{@service_path}?#{@query_string}"
end

#responseObject

The response object of the request



27
28
29
# File 'lib/yahoo-se/request.rb', line 27

def response
  @response ||= Yahoo::SE::Response.new(response_body)
end

#response_bodyObject

The response body of the request



32
33
34
35
36
37
38
# File 'lib/yahoo-se/request.rb', line 32

def response_body
  begin
    @response_body = open(path,"User-Agent" => "Ruby/Yahoo Site Explorer Gem v#{Yahoo::SE::VERSION}").readlines.join
  rescue OpenURI::HTTPError => @errors
    raise ResponseError, @errors
  end
end

#resultsObject

The results from the response object



41
42
43
# File 'lib/yahoo-se/request.rb', line 41

def results
  response.results
end