Module: Paidy

Defined in:
lib/paidy.rb,
lib/paidy/charge.rb,
lib/paidy/version.rb,
lib/paidy/errors/api_error.rb,
lib/paidy/errors/paidy_error.rb,
lib/paidy/errors/authentication_error.rb

Defined Under Namespace

Classes: ApiError, AuthenticationError, Charge, PaidyError

Constant Summary collapse

VERSION =
"0.0.6"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.secret_keyObject

Returns the value of attribute secret_key.



19
20
21
# File 'lib/paidy.rb', line 19

def secret_key
  @secret_key
end

Class Method Details

.api_uri(path: '') ⇒ Object



22
23
24
# File 'lib/paidy.rb', line 22

def self.api_uri(path: '')
  URI.parse([@api_base, path].join('/'))
end

.request(method, path, params = {}, headers = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/paidy.rb', line 26

def self.request(method, path, params = {}, headers = {})
  if secret_key.nil?
    raise Paidy::AuthenticationError.new('API key does not set.' \
      'You should set `Paidy.secret_key = YOUR_PAIDY_SECRET_KEY`.'
    )
  end

  uri = api_uri(path: path)

  case method.to_s.downcase.to_sym
  when :get
    uri += (uri.query.present? ? '&' : '?') + query_parameter(params) if params.present?
    req = Net::HTTP::Get.new(uri)
  when :post
    req = Net::HTTP::Post.new(uri)
    req.body = params.to_json
  end

  req['Content-Type'] = 'application/json'
  req['Paidy-Version'] = @api_version
  req['Authorization'] = "Bearer #{secret_key}"

  req_options = {
    use_ssl: @use_ssl,
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(req)
  end

  body = JSON.parse(response.body)
  case response.code.to_i
  when 200
    body
  else
    raise Paidy::ApiError.new "code: #{body['code']}, title: #{body['title']}, description: #{body['description']}"
  end
end