Class: Nihaopay::Query

Inherits:
Object
  • Object
show all
Includes:
Api
Defined in:
lib/nihaopay/query.rb

Constant Summary collapse

VALID_OPTIONS =
%i[limit starting_after ending_before].freeze

Constants included from Api

Api::LIVE_HOST, Api::TEST_HOST, Api::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Api

included

Class Method Details

.build_transactions(response) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/nihaopay/query.rb', line 52

def build_transactions(response)
  validate_collection!(response)
  transactions = response.parsed_response['transactions']
  transactions.map do |transaction|
    Nihaopay::Transactions::Base.build(transaction)
  end
end

.fetch(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nihaopay/query.rb', line 28

def fetch(options = {})
  query = query_params(options)
  response = if query && !query.empty?
               HTTParty.get(url, headers: request_headers, query: query)
             else
               HTTParty.get(url, headers: request_headers)
             end
  build_transactions(response)
rescue Nihaopay::TransactionError => e
  raise Nihaopay::TransactionLookUpError, e.message
end

.query_params(options) ⇒ Object



48
49
50
# File 'lib/nihaopay/query.rb', line 48

def query_params(options)
  Nihaopay::HashUtil.slice(options, *VALID_OPTIONS).select { |_, v| v && (!v.respond_to?(:empty?) || !v.empty?) }
end

.request_headersObject



44
45
46
# File 'lib/nihaopay/query.rb', line 44

def request_headers
  authorization
end

.urlObject



40
41
42
# File 'lib/nihaopay/query.rb', line 40

def url
  "#{base_url}/transactions"
end

Instance Method Details

#after(time) ⇒ Object



17
18
19
20
# File 'lib/nihaopay/query.rb', line 17

def after(time)
  @starting_after = time
  self
end

#before(time) ⇒ Object



12
13
14
15
# File 'lib/nihaopay/query.rb', line 12

def before(time)
  @ending_before = time
  self
end

#fetch(options = nil) ⇒ Object



22
23
24
25
# File 'lib/nihaopay/query.rb', line 22

def fetch(options = nil)
  options ||= { limit: @limit, starting_after: @starting_after, ending_before: @ending_before }
  self.class.fetch(options)
end

#limit(num) ⇒ Object



7
8
9
10
# File 'lib/nihaopay/query.rb', line 7

def limit(num)
  @limit = num
  self
end