Class: AmazonAssociate::Response
- Inherits:
-
Object
- Object
- AmazonAssociate::Response
- Defined in:
- lib/amazon_associate/response.rb
Overview
Response object returned after a REST call to Amazon service.
Instance Attribute Summary collapse
-
#request_url ⇒ Object
Returns the value of attribute request_url.
Instance Method Summary collapse
-
#doc ⇒ Object
Return Hpricot object.
-
#error ⇒ Object
Return error message.
-
#first_item ⇒ Object
Return the first item (AmazonAssociate::Element).
-
#has_error? ⇒ Boolean
Return true if response has an error.
-
#initialize(xml, request_url) ⇒ Response
constructor
XML input is in string format.
-
#is_valid_request? ⇒ Boolean
Return true if request is valid.
-
#item_page ⇒ Object
Return current page no if :item_page option is when initiating the request.
-
#items ⇒ Object
Return an array of AmazonAssociate::Element item objects.
-
#total_pages ⇒ Object
Return total pages.
-
#total_results ⇒ Object
Return total results.
Constructor Details
#initialize(xml, request_url) ⇒ Response
XML input is in string format
7 8 9 10 11 12 13 14 15 |
# File 'lib/amazon_associate/response.rb', line 7 def initialize(xml, request_url) @doc = Hpricot(xml) @items = nil @item_page = nil @total_results = nil @total_pages = nil self.request_url = request_url end |
Instance Attribute Details
#request_url ⇒ Object
Returns the value of attribute request_url.
5 6 7 |
# File 'lib/amazon_associate/response.rb', line 5 def request_url @request_url end |
Instance Method Details
#doc ⇒ Object
Return Hpricot object.
18 19 20 |
# File 'lib/amazon_associate/response.rb', line 18 def doc @doc end |
#error ⇒ Object
Return error message.
33 34 35 |
# File 'lib/amazon_associate/response.rb', line 33 def error Element.get(@doc, "error/message") end |
#first_item ⇒ Object
Return the first item (AmazonAssociate::Element)
46 47 48 |
# File 'lib/amazon_associate/response.rb', line 46 def first_item items.first end |
#has_error? ⇒ Boolean
Return true if response has an error.
28 29 30 |
# File 'lib/amazon_associate/response.rb', line 28 def has_error? !(error.nil? || error.empty?) end |
#is_valid_request? ⇒ Boolean
Return true if request is valid.
23 24 25 |
# File 'lib/amazon_associate/response.rb', line 23 def is_valid_request? (@doc/"isvalid").inner_html == "True" end |
#item_page ⇒ Object
Return current page no if :item_page option is when initiating the request.
51 52 53 54 55 56 |
# File 'lib/amazon_associate/response.rb', line 51 def item_page unless @item_page @item_page = (@doc/"itemsearchrequest/itempage").inner_html.to_i end @item_page end |
#items ⇒ Object
Return an array of AmazonAssociate::Element item objects.
38 39 40 41 42 43 |
# File 'lib/amazon_associate/response.rb', line 38 def items unless @items @items = (@doc/"item").collect {|item| Element.new(item)} end @items end |
#total_pages ⇒ Object
Return total pages.
67 68 69 70 71 72 |
# File 'lib/amazon_associate/response.rb', line 67 def total_pages unless @total_pages @total_pages = (@doc/"totalpages").inner_html.to_i end @total_pages end |
#total_results ⇒ Object
Return total results.
59 60 61 62 63 64 |
# File 'lib/amazon_associate/response.rb', line 59 def total_results unless @total_results @total_results = (@doc/"totalresults").inner_html.to_i end @total_results end |