Module: Octobat

Defined in:
lib/octobat.rb,
lib/octobat/item.rb,
lib/octobat/util.rb,
lib/octobat/order.rb,
lib/octobat/coupon.rb,
lib/octobat/payout.rb,
lib/octobat/tax_id.rb,
lib/octobat/invoice.rb,
lib/octobat/product.rb,
lib/octobat/version.rb,
lib/octobat/checkout.rb,
lib/octobat/customer.rb,
lib/octobat/document.rb,
lib/octobat/supplier.rb,
lib/octobat/file_link.rb,
lib/octobat/usage_item.rb,
lib/octobat/credit_note.rb,
lib/octobat/file_upload.rb,
lib/octobat/list_object.rb,
lib/octobat/transaction.rb,
lib/octobat/api_resource.rb,
lib/octobat/subscription.rb,
lib/octobat/tax_evidence.rb,
lib/octobat/plaza/account.rb,
lib/octobat/purchase_item.rb,
lib/octobat/beanie/session.rb,
lib/octobat/emails_setting.rb,
lib/octobat/octobat_object.rb,
lib/octobat/payment_source.rb,
lib/octobat/exports_setting.rb,
lib/octobat/errors/api_error.rb,
lib/octobat/plaza/capability.rb,
lib/octobat/proforma_invoice.rb,
lib/octobat/document_language.rb,
lib/octobat/document_template.rb,
lib/octobat/multipart_encoder.rb,
lib/octobat/payment_recipient.rb,
lib/octobat/plaza/country_spec.rb,
lib/octobat/tax_region_setting.rb,
lib/octobat/api_operations/list.rb,
lib/octobat/balance_transaction.rb,
lib/octobat/errors/octobat_error.rb,
lib/octobat/reporting/report_run.rb,
lib/octobat/self_billing_invoice.rb,
lib/octobat/tax_evidence_request.rb,
lib/octobat/api_operations/create.rb,
lib/octobat/api_operations/delete.rb,
lib/octobat/api_operations/update.rb,
lib/octobat/certificate_blacklist.rb,
lib/octobat/reporting/report_type.rb,
lib/octobat/singleton_api_resource.rb,
lib/octobat/document_email_template.rb,
lib/octobat/errors/octobat_lib_error.rb,
lib/octobat/invoice_numbering_sequence.rb,
lib/octobat/errors/api_connection_error.rb,
lib/octobat/errors/authentication_error.rb,
lib/octobat/payment_recipient_reference.rb,
lib/octobat/customer_balance_transaction.rb,
lib/octobat/errors/invalid_request_error.rb,
lib/octobat/credit_note_numbering_sequence.rb

Defined Under Namespace

Modules: APIOperations, Beanie, CertificateBlacklist, Plaza, Reporting, Util Classes: APIConnectionError, APIError, APIResource, AuthenticationError, BalanceTransaction, Checkout, Coupon, CreditNote, CreditNoteNumberingSequence, Customer, CustomerBalanceTransaction, Document, DocumentEmailTemplate, DocumentLanguage, DocumentTemplate, EmailsSetting, ExportsSetting, FileLink, FileUpload, InvalidRequestError, Invoice, InvoiceNumberingSequence, Item, ListObject, MultipartEncoder, OctobatError, OctobatLibError, OctobatObject, Order, PaymentRecipient, PaymentRecipientReference, PaymentSource, Payout, Product, ProformaInvoice, PurchaseItem, SelfBillingInvoice, SingletonAPIResource, Subscription, Supplier, TaxEvidence, TaxEvidenceRequest, TaxId, TaxRegionSetting, Transaction, UsageItem

Constant Summary collapse

VERSION =
'2.0.26'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

Returns the value of attribute api_base.



100
101
102
# File 'lib/octobat.rb', line 100

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key.



100
101
102
# File 'lib/octobat.rb', line 100

def api_key
  @api_key
end

.api_versionObject

Returns the value of attribute api_version.



100
101
102
# File 'lib/octobat.rb', line 100

def api_version
  @api_version
end

.initial_network_retry_delayObject (readonly)

Returns the value of attribute initial_network_retry_delay.



101
102
103
# File 'lib/octobat.rb', line 101

def initial_network_retry_delay
  @initial_network_retry_delay
end

.max_network_retry_delayObject (readonly)

Returns the value of attribute max_network_retry_delay.



101
102
103
# File 'lib/octobat.rb', line 101

def max_network_retry_delay
  @max_network_retry_delay
end

.uploads_baseObject

Returns the value of attribute uploads_base.



100
101
102
# File 'lib/octobat.rb', line 100

def uploads_base
  @uploads_base
end

.verify_ssl_certsObject

Returns the value of attribute verify_ssl_certs.



100
101
102
# File 'lib/octobat.rb', line 100

def verify_ssl_certs
  @verify_ssl_certs
end

Class Method Details

.api_url(url = '', api_base_url = nil) ⇒ Object



104
105
106
# File 'lib/octobat.rb', line 104

def self.api_url(url='', api_base_url=nil)
  (api_base_url || @api_base) + url
end

.execute_request_with_rescues(request_opts, api_base_url, retry_count = 0) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/octobat.rb', line 167

def self.execute_request_with_rescues(request_opts, api_base_url, retry_count = 0)
  begin
    response = execute_request(request_opts)
  rescue SocketError => e
    response = handle_restclient_error(e, request_opts, retry_count, api_base_url)
  rescue NoMethodError => e
    # Work around RestClient bug
    if e.message =~ /\WRequestFailed\W/
      e = APIConnectionError.new('Unexpected HTTP response code')
      response = handle_restclient_error(e, request_opts, retry_count, api_base_url)
    else
      raise
    end
  rescue RestClient::ExceptionWithResponse => e
    if rcode = e.http_code and rbody = e.http_body
      handle_api_error(rcode, rbody)
    else
      response = handle_restclient_error(e, request_opts, retry_count, api_base_url)
    end
  rescue RestClient::Exception, Errno::ECONNREFUSED => e
    response = handle_restclient_error(e, request_opts, retry_count, api_base_url)
  end

  response
end

.max_network_retriesObject



158
159
160
# File 'lib/octobat.rb', line 158

def self.max_network_retries
  @max_network_retries
end

.max_network_retries=(val) ⇒ Object



162
163
164
# File 'lib/octobat.rb', line 162

def self.max_network_retries=(val)
  @max_network_retries = val.to_i
end

.request(method, url, api_key, params = {}, headers = {}, api_base_url = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/octobat.rb', line 108

def self.request(method, url, api_key, params={}, headers={}, api_base_url=nil)
  api_base_url = api_base_url || @api_base

  unless api_key ||= @api_key
    raise AuthenticationError.new('No API key provided. ' \
      'Set your API key using "Octobat.api_key = <API-KEY>". ')
  end

  if api_key =~ /\s/
    raise AuthenticationError.new('Your API key is invalid, as it contains ' \
      'whitespace. (HINT: You can double-check your API key from the ' \
      'Octobat web interface).')
  end

  request_opts = { :verify_ssl => true }

  #if ssl_preflight_passed?
  #  request_opts.update(:verify_ssl => OpenSSL::SSL::VERIFY_PEER,
  #                      :ssl_ca_file => @ssl_bundle_path)
  #end

  #if @verify_ssl_certs and !@CERTIFICATE_VERIFIED
  #  @CERTIFICATE_VERIFIED = CertificateBlacklist.check_ssl_cert(api_base_url, @ssl_bundle_path)
  #end

  params = Util.objects_to_ids(params)
  params = Util.expand_nested_objects(params)
  url = api_url(url, api_base_url)

  case method.to_s.downcase.to_sym
  when :get, :head, :delete
    # Make params into GET parameters
    url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any?
    payload = nil
  else
    if headers[:content_type] && headers[:content_type] == "multipart/form-data"
      payload = params
    else
      payload = uri_encode(params)
    end
  end

  request_opts.update(:headers => request_headers(api_key, method).update(headers),
                      :method => method, :open_timeout => 30,
                      :payload => payload, :url => url, :timeout => 80)

  response = execute_request_with_rescues(request_opts, api_base_url)
  [parse(response), api_key]
end