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'
OpenTimeout =
30
ReadTimeout =
80

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, sandbox = false, sso_key = false) ⇒ Client

Returns a new instance of Client.



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
# File 'lib/invoiced.rb', line 61

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

  # Object endpoints
  @Item = Invoiced::Item.new(self)
  @Charge = Invoiced::Charge.new(self)
  @Coupon = Invoiced::Coupon.new(self)
  @CreditBalanceAdjustment = Invoiced::CreditBalanceAdjustment.new(self)
  @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)
  @Note = Invoiced::Note.new(self)
  @Payment = Invoiced::Payment.new(self)
  @Plan = Invoiced::Plan.new(self)
  @Refund = Invoiced::Refund.new(self)
  @Subscription = Invoiced::Subscription.new(self)
  @Task = Invoiced::Task.new(self)
  @TaxRate = Invoiced::TaxRate.new(self)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



58
59
60
# File 'lib/invoiced.rb', line 58

def api_key
  @api_key
end

#api_urlObject (readonly)

Returns the value of attribute api_url.



58
59
60
# File 'lib/invoiced.rb', line 58

def api_url
  @api_url
end

#ChargeObject (readonly)

Returns the value of attribute Charge.



59
60
61
# File 'lib/invoiced.rb', line 59

def Charge
  @Charge
end

#CouponObject (readonly)

Returns the value of attribute Coupon.



59
60
61
# File 'lib/invoiced.rb', line 59

def Coupon
  @Coupon
end

#CreditBalanceAdjustmentObject (readonly)

Returns the value of attribute CreditBalanceAdjustment.



59
60
61
# File 'lib/invoiced.rb', line 59

def CreditBalanceAdjustment
  @CreditBalanceAdjustment
end

#CreditNoteObject (readonly)

Returns the value of attribute CreditNote.



59
60
61
# File 'lib/invoiced.rb', line 59

def CreditNote
  @CreditNote
end

#CustomerObject (readonly)

Returns the value of attribute Customer.



59
60
61
# File 'lib/invoiced.rb', line 59

def Customer
  @Customer
end

#EstimateObject (readonly)

Returns the value of attribute Estimate.



59
60
61
# File 'lib/invoiced.rb', line 59

def Estimate
  @Estimate
end

#EventObject (readonly)

Returns the value of attribute Event.



59
60
61
# File 'lib/invoiced.rb', line 59

def Event
  @Event
end

#FileObject (readonly)

Returns the value of attribute File.



59
60
61
# File 'lib/invoiced.rb', line 59

def File
  @File
end

#InvoiceObject (readonly)

Returns the value of attribute Invoice.



59
60
61
# File 'lib/invoiced.rb', line 59

def Invoice
  @Invoice
end

#ItemObject (readonly)

Returns the value of attribute Item.



59
60
61
# File 'lib/invoiced.rb', line 59

def Item
  @Item
end

#NoteObject (readonly)

Returns the value of attribute Note.



59
60
61
# File 'lib/invoiced.rb', line 59

def Note
  @Note
end

#PaymentObject (readonly)

Returns the value of attribute Payment.



59
60
61
# File 'lib/invoiced.rb', line 59

def Payment
  @Payment
end

#PlanObject (readonly)

Returns the value of attribute Plan.



59
60
61
# File 'lib/invoiced.rb', line 59

def Plan
  @Plan
end

#RefundObject (readonly)

Returns the value of attribute Refund.



59
60
61
# File 'lib/invoiced.rb', line 59

def Refund
  @Refund
end

#sandboxObject (readonly)

Returns the value of attribute sandbox.



58
59
60
# File 'lib/invoiced.rb', line 58

def sandbox
  @sandbox
end

#sso_keyObject (readonly)

Returns the value of attribute sso_key.



58
59
60
# File 'lib/invoiced.rb', line 58

def sso_key
  @sso_key
end

#SubscriptionObject (readonly)

Returns the value of attribute Subscription.



59
60
61
# File 'lib/invoiced.rb', line 59

def Subscription
  @Subscription
end

#TaskObject (readonly)

Returns the value of attribute Task.



59
60
61
# File 'lib/invoiced.rb', line 59

def Task
  @Task
end

#TaxRateObject (readonly)

Returns the value of attribute TaxRate.



59
60
61
# File 'lib/invoiced.rb', line 59

def TaxRate
  @TaxRate
end

Instance Method Details

#generate_sign_in_token(customerId, ttl) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/invoiced.rb', line 121

def (customerId, ttl)
    if !@sso_key
        raise "Please provide a single sign-on key! You can find this value in Settings > Developers > Single Sign-On of the Invoiced application."
    end

    expires = Time.now + ttl # TTL should be in seconds

    payload = {
        :sub => customerId,
        :iss => "Invoiced Ruby/#{Invoiced::VERSION}",
        :iat => Time.now.to_i,
        :exp => expires.to_i
    }

    JWT.encode payload, @sso_key, 'HS256'
end

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/invoiced.rb', line 87

def request(method, endpoint, params={}, opts={})
    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 => buildHeaders(opts),
            :payload => payload,
            :open_timeout => OpenTimeout,
            :timeout => ReadTimeout
        )
    rescue RestClient::Exception => e
        if e.response
            handle_api_error(e.response)
        else
            handle_network_error(e)
        end
    end

    parse(response)
end