Module: Cryptopay::Request

Defined in:
lib/cryptopay/net.rb

Class Method Summary collapse

Class Method Details

.get(path, options = {}) ⇒ Object



29
30
31
# File 'lib/cryptopay/net.rb', line 29

def self.get(path, options={})
  self.request(:GET, path, options)
end

.post(path, options = {}) ⇒ Object



33
34
35
# File 'lib/cryptopay/net.rb', line 33

def self.post(path, options={})
  self.request(:POST, path, options)
end

.request(verb, path, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cryptopay/net.rb', line 7

def self.request(verb, path, options={})
  verb = verb.upcase.to_sym

  options[:api_key] = Cryptopay.key

  resource = RestClient::Resource.new to_uri(path), content_type: 'application/json'
  begin
    if verb == :GET
      response = RestClient.get "#{to_uri(path)}?".concat(options.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&'))
    elsif verb == :POST
      response = resource.post options
    end

    return JSON.parse response

  rescue => e
    error = Cryptopay::Error.new "#{e.message} (#{e.class})"
    error.errors = JSON.parse(e.response)
    raise error
  end
end

.to_uri(path) ⇒ Object



3
4
5
# File 'lib/cryptopay/net.rb', line 3

def self.to_uri(path)
  "https://cryptopay.me/api/v1/#{path}"
end