class PagSeguro::SubscriptionSearch

Attributes

errors[R]

Set the errors from the report request.

options[R]

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.

page[R]

Return the current page.

Public Class Methods

new(path, options) click to toggle source
# File lib/pagseguro/subscription/subscription_search.rb, line 17
def initialize(path, options)
  @path = path
  @page = options.delete(:page) || 0
  @options = options
  @errors = Errors.new
end

Public Instance Methods

created_at() click to toggle source

The report’s creation date.

# File lib/pagseguro/subscription/subscription_search.rb, line 38
def created_at
  xml do |xml|
    @created_at ||= Time.parse xml.css("preApprovalSearchResult > date").text
  end
end
next_page!() click to toggle source

Move the page pointer to the next page.

# File lib/pagseguro/subscription/subscription_search.rb, line 69
def next_page!
  return unless next_page?
  @page += 1
  clear!
end
next_page?() click to toggle source

Detect if the report has a next page.

# File lib/pagseguro/subscription/subscription_search.rb, line 59
def next_page?
  page.zero? || page < total_pages
end
previous_page!() click to toggle source

Move the page pointer to the previous page.

# File lib/pagseguro/subscription/subscription_search.rb, line 76
def previous_page!
  return unless previous_page?
  @page -= 1
  clear!
end
previous_page?() click to toggle source

Detect if the report has a previous page.

# File lib/pagseguro/subscription/subscription_search.rb, line 64
def previous_page?
  page > 1
end
results() click to toggle source

How many results the report returned on the given page.

# File lib/pagseguro/subscription/subscription_search.rb, line 45
def results
  xml do |xml|
    @results = xml.css("preApprovalSearchResult > resultsInThisPage").text.to_i
  end
end
subscriptions() click to toggle source

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

# File lib/pagseguro/subscription/subscription_search.rb, line 29
def subscriptions
  xml do |xml|
    xml.css("preApprovalSearchResult > preApprovals > preApproval").map do |node|
      Subscription.load_from_xml(node)
    end
  end
end
total_pages() click to toggle source

How many pages the report returned.

# File lib/pagseguro/subscription/subscription_search.rb, line 52
def total_pages
  xml do |xml|
    @total_pages ||= xml.css("preApprovalSearchResult > totalPages").text.to_i
  end
end
valid?() click to toggle source

Detect if the report request returned errors.

# File lib/pagseguro/subscription/subscription_search.rb, line 83
def valid?
  fetch { errors.empty? }
end

Private Instance Methods

api_version() click to toggle source

The default PagSeguro API version

# File lib/pagseguro/subscription/subscription_search.rb, line 106
def api_version
  :v2
end
clear!() click to toggle source
# File lib/pagseguro/subscription/subscription_search.rb, line 118
def clear!
  @fetched = false
end
fetch(&block) click to toggle source
# File lib/pagseguro/subscription/subscription_search.rb, line 122
def fetch(&block)
  unless fetched?
    perform_request_and_serialize
    fetched!
  end

  instance_eval(&block)
end
fetched!() click to toggle source
# File lib/pagseguro/subscription/subscription_search.rb, line 114
def fetched!
  @fetched = true
end
fetched?() click to toggle source
# File lib/pagseguro/subscription/subscription_search.rb, line 110
def fetched?
  @fetched
end
params() click to toggle source

The params that will be sent

# File lib/pagseguro/subscription/subscription_search.rb, line 94
def params
  {}.tap do |param|
    param[:credentials] = options[:credentials]
    param[:page] = page unless page.zero?
    param[:maxPageResults] = options[:per_page]
    param[:initialDate] = options[:starts_at].xmlschema if options[:starts_at]
    param[:finalDate] = options[:ends_at].xmlschema if options[:ends_at]
    param[:interval] = options[:interval] if options[:interval]
  end
end
perform_request_and_serialize() click to toggle source
# File lib/pagseguro/subscription/subscription_search.rb, line 88
def perform_request_and_serialize
  @response = Request.get(@path, api_version, params)
  @errors = Errors.new(@response)
end
xml(&block) click to toggle source
# File lib/pagseguro/subscription/subscription_search.rb, line 131
def xml(&block)
  valid? && block.call(@response.data)
end