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.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/invoiced.rb', line 39

def initialize(api_key, sandbox=false)
  @api_key = api_key
  @sandbox = sandbox
  @api_url = sandbox ? ApiBaseSandbox : ApiBase

  # Object endpoints
  @Customer = Invoiced::Customer.new(self)
  @Event = Invoiced::Event.new(self)
  @File = Invoiced::File.new(self)
  @Invoice = Invoiced::Invoice.new(self)
  @Subscription = Invoiced::Subscription.new(self)
  @Transaction = Invoiced::Transaction.new(self)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



36
37
38
# File 'lib/invoiced.rb', line 36

def api_key
  @api_key
end

#api_urlObject (readonly)

Returns the value of attribute api_url.



36
37
38
# File 'lib/invoiced.rb', line 36

def api_url
  @api_url
end

#CustomerObject (readonly)

Returns the value of attribute Customer.



37
38
39
# File 'lib/invoiced.rb', line 37

def Customer
  @Customer
end

#EventObject (readonly)

Returns the value of attribute Event.



37
38
39
# File 'lib/invoiced.rb', line 37

def Event
  @Event
end

#FileObject (readonly)

Returns the value of attribute File.



37
38
39
# File 'lib/invoiced.rb', line 37

def File
  @File
end

#InvoiceObject (readonly)

Returns the value of attribute Invoice.



37
38
39
# File 'lib/invoiced.rb', line 37

def Invoice
  @Invoice
end

#sandboxObject (readonly)

Returns the value of attribute sandbox.



36
37
38
# File 'lib/invoiced.rb', line 36

def sandbox
  @sandbox
end

#SubscriptionObject (readonly)

Returns the value of attribute Subscription.



37
38
39
# File 'lib/invoiced.rb', line 37

def Subscription
  @Subscription
end

#TransactionObject (readonly)

Returns the value of attribute Transaction.



37
38
39
# File 'lib/invoiced.rb', line 37

def Transaction
  @Transaction
end

Instance Method Details

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



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
80
81
82
83
84
85
86
87
# File 'lib/invoiced.rb', line 53

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