Module: MangoPay

Defined in:
lib/mangopay/json.rb,
lib/mangopay.rb,
lib/mangopay/temp.rb,
lib/mangopay/card.rb,
lib/mangopay/hook.rb,
lib/mangopay/user.rb,
lib/mangopay/event.rb,
lib/mangopay/client.rb,
lib/mangopay/errors.rb,
lib/mangopay/pay_in.rb,
lib/mangopay/refund.rb,
lib/mangopay/wallet.rb,
lib/mangopay/version.rb,
lib/mangopay/pay_out.rb,
lib/mangopay/transfer.rb,
lib/mangopay/resource.rb,
lib/mangopay/http_calls.rb,
lib/mangopay/legal_user.rb,
lib/mangopay/transaction.rb,
lib/mangopay/bank_account.rb,
lib/mangopay/natural_user.rb,
lib/mangopay/kyc_document.rb,
lib/mangopay/pre_authorization.rb,
lib/mangopay/card_registration.rb,
lib/mangopay/authorization_token.rb

Overview

We can use MultiJson directly , why do we even have this module ?

Defined Under Namespace

Modules: AuthorizationToken, HTTPCalls, JSON, Temp Classes: BankAccount, Card, CardRegistration, Client, Configuration, Error, Event, Hook, KycDocument, LegalUser, NaturalUser, PayIn, PayOut, PreAuthorization, Refund, Resource, ResponseError, Transaction, Transfer, User, Wallet

Constant Summary collapse

VERSION =
'3.0.15'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration


52
53
54
# File 'lib/mangopay.rb', line 52

def configuration
  @configuration
end

Class Method Details

.api_pathObject


54
55
56
# File 'lib/mangopay.rb', line 54

def api_path
  "/v2/#{MangoPay.configuration.client_id}"
end

.api_uri(url = '') ⇒ Object


63
64
65
# File 'lib/mangopay.rb', line 63

def api_uri(url='')
  URI(configuration.root_url + url)
end

.configure {|configuration| ... } ⇒ Object

Yields:


58
59
60
61
# File 'lib/mangopay.rb', line 58

def configure
  self.configuration ||= Configuration.new
  yield configuration
end

.request(method, url, params = {}, filters = {}, headers = request_headers, before_request_proc = nil) ⇒ Object

  • method: HTTP method; lowercase symbol, e.g. :get, :post etc.

  • url: the part after Configuration#root_url

  • params: hash; entity data for creation, update etc.; will dump it by JSON and assign to Net::HTTPRequest#body

  • filters: hash; pagination params etc.; will encode it by URI and assign to URI#query

  • headers: hash; request_headers by default

  • before_request_proc: optional proc; will call it passing the Net::HTTPRequest instance just before Net::HTTPRequest#request

Raises MangoPay::ResponseError if response code != 200.


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mangopay.rb', line 77

def request(method, url, params={}, filters={}, headers = request_headers, before_request_proc = nil)
  uri = api_uri(url)
  uri.query = URI.encode_www_form(filters) unless filters.empty?

  res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
    req.body = JSON.dump(params)
    before_request_proc.call(req) if before_request_proc
    http.request req
  end

  # decode json data
  data = JSON.load(res.body.to_s) rescue {}


  unless res.is_a?(Net::HTTPOK)
    raise MangoPay::ResponseError.new(uri, res.code, data)
  end

  # copy pagination info if any
  ['x-number-of-pages', 'x-number-of-items'].each { |k|
    filters[k.gsub('x-number-of-', 'total_')] = res[k].to_i if res[k]
  }

  data
end