Class: YANAPI::Query
- Inherits:
-
Object
- Object
- YANAPI::Query
- Defined in:
- lib/yanapi/query.rb
Direct Known Subclasses
Constant Summary collapse
- HOST =
'http://answers.yahooapis.com'- SERVICE =
'AnswersService'- SERVICE_VERSION =
'V1'- VALID_PARAMS =
[:appid, :output, :callback]
- REQUIRED_PARAMS =
[:appid]
- VALID_OUTPUT_FORMATS =
[nil, 'xml', 'php', 'rss', 'json']
Instance Method Summary collapse
-
#get ⇒ Object
main method.
-
#initialize(params) ⇒ Query
constructor
It accepts a two dimensional hash: => ‘questionSearch’, :query_params => {:appid => ‘YahooDemo’, :query => ‘Haus’}.
Constructor Details
#initialize(params) ⇒ Query
It accepts a two dimensional hash: => ‘questionSearch’, :query_params =>
{:appid => 'YahooDemo', :query => 'Haus'}
21 22 23 24 25 26 |
# File 'lib/yanapi/query.rb', line 21 def initialize(params) @method = params[:method] @params = check_params(params[:query_params]) @url = build_url(@params) @output = @params[:output] || 'xml' end |
Instance Method Details
#get ⇒ Object
main method
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/yanapi/query.rb', line 29 def get http_response = Net::HTTP.get_response(@url) case @output when 'xml' # Set the value to STRICT (0), otherwise no errors will be raised! xml = Nokogiri::XML::Document.parse(http_response.body, nil, nil, 0) result = prove_xml(xml) ? http_response.body : nil when 'json' raise NotImplementedError, 'We do not handle JSON yet!' when 'php' raise NotImplementedError, 'We do not handle PHP yet!' when 'rss' raise NotImplementedError, 'We do not handle RSS yet!' end # TODO: make a fine grained distinction case http_response when Net::HTTPSuccess return result when Net::HTTPBadRequest, Net::HTTPForbidden, Net::HTTPServiceUnavailable raise ExternalError.new("#{http_response}:\n\n#{result}") else raise ExternalError.new("#{http_response}:\n\n#{result}") end # are all external errors caught here? rescue Net::HTTPError, SocketError, Timeout::Error, IOError, SystemCallError => e raise ExternalError, e # add JSON, PHP, RSS errors here rescue Nokogiri::XML::SyntaxError => e raise ContentError.new(e) end |