Class: Client

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

Defined Under Namespace

Classes: NotAuthorized

Instance Method Summary collapse

Instance Method Details

#baseURLObject



6
7
8
# File 'lib/recharge/client.rb', line 6

def baseURL
  "https://www.rechargebilling.com/API/v2/"
end

#request(method, uri, data = nil, apiKey = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/recharge/client.rb', line 10

def request(method, uri, data=nil, apiKey=nil)
  response = sendRequest(method, uri, data, apiKey)
  response.assertValidResponse
  if response.code.to_i == 200
    if method != "DELETE"
      Recharge::Base.returnImportantXML(response.body)
    else
      Recharge::Base.returnStatusXML(response.body)
    end
  else 
    Recharge::Base.returnErrorXML(response.body)
  end
  
end

#sendRequest(method, uri, data = nil, apiKey = nil) ⇒ Object



25
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
64
65
66
# File 'lib/recharge/client.rb', line 25

def sendRequest(method, uri, data=nil, apiKey=nil)
  require "net/https"
  require "uri"
  
  if !Recharge.api_key
    if !apiKey
      raise NotAuthorized, "No API Key"
      return
    end
  else
    if !apiKey
      apiKey = Recharge.api_key
    end
  end
  
  uri = baseURL+uri
  uri = URI.parse(uri)
  
  if method == "GET"
    request = Net::HTTP::Get.new(uri.request_uri)
  end
  
  if method == "POST"
    request = Net::HTTP::Post.new(uri.request_uri)
    request.set_form_data(data)
  end
  
  if method == "DELETE"
    request = Net::HTTP::Delete.new(uri.request_uri)
  end
  
  request.basic_auth(apiKey, "")
  
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  
  response = http.request(request)
  
  return Response.new(response.code, response.body)

end