Module: Micropayment::API

Extended by:
API
Included in:
API
Defined in:
lib/services/api.rb

Constant Summary collapse

RootCA =
'/etc/ssl/certs'
DEFAULT_TIMEOUT =
10

Instance Method Summary collapse

Instance Method Details

#call(url, method, data = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/services/api.rb', line 14

def call(url, method, data={})
  ensure_api_key!
  data[:action]     = method
  data[:accessKey]  = Config.api_key
  data[:testMode] ||= Config.sandbox_param
  data.delete_if {|k,v| v.nil? }
  uri = Addressable::URI.parse(url)
  uri.query_values = data
  parseNVP get(uri)
end

#ensure_api_key!Object



10
11
12
# File 'lib/services/api.rb', line 10

def ensure_api_key!
  raise "Micropayment::Config.api_key has not been set. See github.com/GeneralScripting/micropayment"  unless Config.api_key
end

#get(uri) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/services/api.rb', line 25

def get(uri)
  use_ssl = uri.scheme == 'https'
  http = Net::HTTP.new(uri.host, use_ssl ? 443 : 80)
  http.use_ssl = use_ssl
  #http.read_timeout = timeout
  if use_ssl
    if File.directory? RootCA
      http.ca_path = RootCA
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      http.verify_depth = 5
    else
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  end
  response = http.get uri.request_uri, 'User-agent' => 'Micropayment Ruby Client'
  case response
  when Net::HTTPSuccess, Net::HTTPOK
    response.body
  else
    response.error!
  end
end

#parseNVP(text) ⇒ Object



48
49
50
# File 'lib/services/api.rb', line 48

def parseNVP(text)
  Addressable::URI.parse("http://example.com?#{text.split("\n").join("&")}").query_values
end