Class: PaysonAPI::V2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/payson_api/v2/client.rb

Class Method Summary collapse

Class Method Details

.account_infoObject



9
10
11
12
# File 'lib/payson_api/v2/client.rb', line 9

def self.
  response = payson_request(Net::HTTP::Get, PAYSON_API_RESOURCES[:accounts][:get])
  PaysonAPI::V2::Models::.from_hash(JSON.parse(response.body))
end

.create_checkout(request) ⇒ Object



19
20
21
22
23
# File 'lib/payson_api/v2/client.rb', line 19

def self.create_checkout(request)
  response = payson_request(Net::HTTP::Post, PAYSON_API_RESOURCES[:checkouts][:create], request)
  handle_validation_error(response)
  PaysonAPI::V2::Models::Checkout.from_hash(JSON.parse(response.body))
end

.get_checkout(id) ⇒ Object



14
15
16
17
# File 'lib/payson_api/v2/client.rb', line 14

def self.get_checkout(id)
  response = payson_request(Net::HTTP::Get, PAYSON_API_RESOURCES[:checkouts][:get] % id)
  PaysonAPI::V2::Models::Checkout.from_hash(JSON.parse(response.body))
end

.handle_validation_error(response) ⇒ Object



41
42
43
44
45
# File 'lib/payson_api/v2/client.rb', line 41

def self.handle_validation_error(response)
  if response.code == '400' || response.code == '500' # rubocop:disable Style/GuardClause
    raise PaysonAPI::V2::Errors::ValidationError, errors: JSON.parse(response.body)['errors']
  end
end

.hash_to_params(hash) ⇒ Object



47
48
49
# File 'lib/payson_api/v2/client.rb', line 47

def self.hash_to_params(hash)
  hash.map { |k, v| "#{k}=#{URI.encode_www_form_component(v.to_s)}" }.join('&')
end

.list_checkouts(request) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/payson_api/v2/client.rb', line 31

def self.list_checkouts(request)
  path = [PAYSON_API_RESOURCES[:checkouts][:list], hash_to_params(request.to_hash)].join('?')
  response = payson_request(Net::HTTP::Get, path)
  [].tap do |checkouts|
    JSON.parse(response.body)['data'].each do |json|
      checkouts << PaysonAPI::V2::Models::Checkout.from_hash(json)
    end
  end
end

.payson_request(method, resource, request = nil) ⇒ Object

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/payson_api/v2/client.rb', line 51

def self.payson_request(method, resource, request = nil)
  url = [PaysonAPI::V2.api_base_url, PAYSON_API_VERSION, resource].join('/')
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  req = method.new(uri.request_uri)
  req.basic_auth PaysonAPI::V2.config.api_user_id, PaysonAPI::V2.config.api_password
  req.body = JSON.generate(request.to_hash) unless request.nil?
  req['Content-Type'] = 'application/json'
  response = http.request(req)
  raise PaysonAPI::V2::Errors::UnauthorizedError if response.code == '401'

  response
end

.update_checkout(request) ⇒ Object



25
26
27
28
29
# File 'lib/payson_api/v2/client.rb', line 25

def self.update_checkout(request)
  response = payson_request(Net::HTTP::Put, PAYSON_API_RESOURCES[:checkouts][:update] % request.id, request)
  handle_validation_error(response)
  PaysonAPI::V2::Models::Checkout.from_hash(JSON.parse(response.body))
end