Class: Paysio::Client

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

Class Method Summary collapse

Class Method Details

.execute(opts) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/paysio/client.rb', line 36

def execute(opts)
  RestClient::Request.execute(opts)
rescue RuntimeError => e
  case e.http_code.to_i
  when 400
    raise Paysio::Errors::BadRequest, e.http_body
  when 401
    raise Paysio::Errors::Unauthorized, e.http_body
  when 403
    raise Paysio::Errors::Forbidden, e.http_body
  when 404
    raise Paysio::Errors::NotFound, e.http_body
  when 500
    raise Paysio::Errors::InternalError, e.http_body
  else
    raise e
  end
end

.flatten_params(params, parent_key = nil) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/paysio/client.rb', line 59

def flatten_params(params, parent_key = nil)
  result = []
  params.each do |key, value|
    flatten_key = parent_key ? "#{parent_key}[#{uri_escape(key)}]" : uri_escape(key)
    result += value.is_a?(Hash) ? flatten_params(value, flatten_key) : [[flatten_key, value]]
  end
  result
end

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



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/paysio/client.rb', line 4

def request(method, path, params = {}, headers = {})
  unless Paysio.api_key
    raise Paysio::Errors::Unauthorized, "Please specify Paysio.api_key"
  end
  headers = {
    :user_agent => "Pays.io RubyClient/#{Paysio::VERSION}",
    :content_type => 'application/x-www-form-urlencoded'
  }.merge(headers)
  opts = {
    :url => Paysio.api_url(path),
    :method => method,
    :timeout => 80,
    :headers => headers
  }

  # build params
  case method.to_s.downcase.to_sym
  when :get, :head, :delete
    opts[:url] += "?#{stringify_params(params)}" if params.count > 0
  else
    opts[:payload] = stringify_params(params)
  end
  response = execute(opts)
  body, code = response.body, response.code
  begin
    resp = Paysio::JSON.decode(body)
  rescue Oj::ParseError
    raise Paysio::Exceptions::InternalError.new("Invalid response object from API: #{body.inspect} (#{code})", code, body)
  end
  Paysio::Resource.build_from(resp, response.headers[:location])
end

.stringify_params(params) ⇒ Object



68
69
70
# File 'lib/paysio/client.rb', line 68

def stringify_params(params)
  flatten_params(params).collect{|key, value| "#{key}=#{uri_escape(value)}"}.join('&')
end

.uri_escape(key) ⇒ Object



55
56
57
# File 'lib/paysio/client.rb', line 55

def uri_escape(key)
  URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end