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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/invoiced.rb', line 41

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

  # Object endpoints
  @CreditNote = Invoiced::CreditNote.new(self)
  @Customer = Invoiced::Customer.new(self)
  @Estimate = Invoiced::Estimate.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.



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

def api_key
  @api_key
end

#api_urlObject (readonly)

Returns the value of attribute api_url.



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

def api_url
  @api_url
end

#CreditNoteObject (readonly)

Returns the value of attribute CreditNote.



39
40
41
# File 'lib/invoiced.rb', line 39

def CreditNote
  @CreditNote
end

#CustomerObject (readonly)

Returns the value of attribute Customer.



39
40
41
# File 'lib/invoiced.rb', line 39

def Customer
  @Customer
end

#EstimateObject (readonly)

Returns the value of attribute Estimate.



39
40
41
# File 'lib/invoiced.rb', line 39

def Estimate
  @Estimate
end

#EventObject (readonly)

Returns the value of attribute Event.



39
40
41
# File 'lib/invoiced.rb', line 39

def Event
  @Event
end

#FileObject (readonly)

Returns the value of attribute File.



39
40
41
# File 'lib/invoiced.rb', line 39

def File
  @File
end

#InvoiceObject (readonly)

Returns the value of attribute Invoice.



39
40
41
# File 'lib/invoiced.rb', line 39

def Invoice
  @Invoice
end

#sandboxObject (readonly)

Returns the value of attribute sandbox.



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

def sandbox
  @sandbox
end

#SubscriptionObject (readonly)

Returns the value of attribute Subscription.



39
40
41
# File 'lib/invoiced.rb', line 39

def Subscription
  @Subscription
end

#TransactionObject (readonly)

Returns the value of attribute Transaction.



39
40
41
# File 'lib/invoiced.rb', line 39

def Transaction
  @Transaction
end

Instance Method Details

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



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
88
89
90
91
# File 'lib/invoiced.rb', line 57

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