Class: AmazonAssociate::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon_associate/response.rb

Overview

Response object returned after a REST call to Amazon service.

Instance Attribute Summary collapse

Instance Method Summary collapse

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_urlObject

Returns the value of attribute request_url.



5
6
7
# File 'lib/amazon_associate/response.rb', line 5

def request_url
  @request_url
end

#unsigned_urlObject

Returns the value of attribute unsigned_url.



5
6
7
# File 'lib/amazon_associate/response.rb', line 5

def unsigned_url
  @unsigned_url
end

Instance Method Details

#docObject

Return Hpricot object.



18
19
20
# File 'lib/amazon_associate/response.rb', line 18

def doc
  @doc
end

#errorObject

Return error message.



33
34
35
# File 'lib/amazon_associate/response.rb', line 33

def error
  Element.get(@doc, "error/message")
end

#first_itemObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


23
24
25
# File 'lib/amazon_associate/response.rb', line 23

def is_valid_request?
  (@doc/"isvalid").inner_html == "True"
end

#item_pageObject

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

#itemsObject

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_pagesObject

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_resultsObject

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