Class: PagSeguro::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/pagseguro/transaction/search.rb

Direct Known Subclasses

SearchAbandoned, SearchByDate, SearchByReference

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options, page = 0) ⇒ Search

Returns a new instance of Search.



17
18
19
20
21
# File 'lib/pagseguro/transaction/search.rb', line 17

def initialize(path, options, page = 0)
  @path = path
  @options = options
  @page = page
end

Instance Attribute Details

#errorsObject (readonly)

Set the errors from the report request.



12
13
14
# File 'lib/pagseguro/transaction/search.rb', line 12

def errors
  @errors
end

#optionsObject (readonly)

Set the report options.

# per_page: the page size. # starts_at: the report’s starting date. Can’t be older than 6 months. # ends_at: the report’s ending date. Can’t be greater than 30 days from the starting date.



9
10
11
# File 'lib/pagseguro/transaction/search.rb', line 9

def options
  @options
end

#pageObject (readonly)

Return the current page.



15
16
17
# File 'lib/pagseguro/transaction/search.rb', line 15

def page
  @page
end

Instance Method Details

#created_atObject

The report’s creation date.



37
38
39
40
41
# File 'lib/pagseguro/transaction/search.rb', line 37

def created_at
  xml do |xml|
    @created_at ||= Time.parse xml.css("transactionSearchResult > date").text
  end
end

#next_page!Object

Move the page pointer to the next page.



68
69
70
71
72
# File 'lib/pagseguro/transaction/search.rb', line 68

def next_page!
  return unless next_page?
  @page += 1
  clear!
end

#next_page?Boolean

Detect if the report has a next page.

Returns:

  • (Boolean)


58
59
60
# File 'lib/pagseguro/transaction/search.rb', line 58

def next_page?
  page.zero? || page < total_pages
end

#previous_page!Object

Move the page pointer to the previous page.



75
76
77
78
79
# File 'lib/pagseguro/transaction/search.rb', line 75

def previous_page!
  return unless previous_page?
  @page -= 1
  clear!
end

#previous_page?Boolean

Detect if the report has a previous page.

Returns:

  • (Boolean)


63
64
65
# File 'lib/pagseguro/transaction/search.rb', line 63

def previous_page?
  page > 1
end

#resultsObject

How many results the report returned on the given page.



44
45
46
47
48
# File 'lib/pagseguro/transaction/search.rb', line 44

def results
  xml do |xml|
    @results ||= xml.css("transactionSearchResult > resultsInThisPage").text.to_i
  end
end

#total_pagesObject

How many pages the report returned.



51
52
53
54
55
# File 'lib/pagseguro/transaction/search.rb', line 51

def total_pages
  xml do |xml|
    @total_pages ||= xml.css("transactionSearchResult > totalPages").text.to_i
  end
end

#transactionsObject

Return the list of transactions. Each item will be wrapped in a PagSeguro::Transaction instance. Notice that transactions instantiated by the report won’t have all attributes. If you need additional attributes, do a PagSeguro::Transaction.find_by_notification_code call. Remember that this will perform an additional HTTP request.



28
29
30
31
32
33
34
# File 'lib/pagseguro/transaction/search.rb', line 28

def transactions
  xml do |xml|
    xml.css("transactionSearchResult > transactions > transaction").map do |node|
      Transaction.load_from_xml(node)
    end
  end
end

#valid?Boolean

Detect if the report request returned errors.

Returns:

  • (Boolean)


82
83
84
# File 'lib/pagseguro/transaction/search.rb', line 82

def valid?
  fetch { errors.empty? }
end