Class: Invoiced::Client

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

Constant Summary collapse

ApiBase =
'https://api.invoiced.com'
ApiBaseSandbox =
'https://api.sandbox.invoiced.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, sandbox = false) ⇒ Client

Returns a new instance of Client.



35
36
37
38
39
40
41
42
43
# File 'lib/invoiced.rb', line 35

def initialize(api_key, sandbox=false)
  @api_key = api_key
  @sandbox = sandbox
  @api_url = sandbox ? ApiBaseSandbox : ApiBase
  @Customer = Invoiced::Customer.new(self)
  @Invoice = Invoiced::Invoice.new(self)
  @Transaction = Invoiced::Transaction.new(self)
  @Subscription = Invoiced::Subscription.new(self)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



32
33
34
# File 'lib/invoiced.rb', line 32

def api_key
  @api_key
end

#api_urlObject (readonly)

Returns the value of attribute api_url.



32
33
34
# File 'lib/invoiced.rb', line 32

def api_url
  @api_url
end

#CustomerObject (readonly)

Returns the value of attribute Customer.



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

def Customer
  @Customer
end

#InvoiceObject (readonly)

Returns the value of attribute Invoice.



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

def Invoice
  @Invoice
end

#sandboxObject (readonly)

Returns the value of attribute sandbox.



32
33
34
# File 'lib/invoiced.rb', line 32

def sandbox
  @sandbox
end

#SubscriptionObject (readonly)

Returns the value of attribute Subscription.



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

def Subscription
  @Subscription
end

#TransactionObject (readonly)

Returns the value of attribute Transaction.



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

def Transaction
  @Transaction
end

Instance Method Details

#request(method, endpoint, params = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/invoiced.rb', line 45

def request(method, endpoint, params={})
  url = @api_url + endpoint

      case method.to_s.downcase.to_sym
      # These methods don't have a request body
      when :get, :head, :delete
        # Make params into GET parameters
        url += "#{URI.parse(url).query ? '&' : '?'}#{Util.uri_encode(params)}" if params && params.any?
        payload = nil
      # Otherwise, encode request body to JSON
      else
        payload = params.to_json
      end

      begin
  response = RestClient::Request.execute(
    :method => method,
    :url => url,
    :headers => {
      :authorization => Util.auth_header(@api_key),
      :content_type => "application/json",
      :user_agent => "Invoiced Ruby/#{Invoiced::VERSION}"
    },
    :payload => payload
  )
 rescue RestClient::Exception => e
  if e.response
    rescue_api_error(e.response)
  else
    rescue_rest_client_error(e)
  end
 end

 parse(response)
end