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/invoice.rb,
lib/octobat/product.rb,
lib/octobat/version.rb,
lib/octobat/checkout.rb,
lib/octobat/customer.rb,
lib/octobat/document.rb,
lib/octobat/credit_note.rb,
lib/octobat/list_object.rb,
lib/octobat/transaction.rb,
lib/octobat/api_resource.rb,
lib/octobat/tax_evidence.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/proforma_invoice.rb,
lib/octobat/document_language.rb,
lib/octobat/document_template.rb,
lib/octobat/payment_recipient.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/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/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/errors/invalid_request_error.rb,
lib/octobat/credit_note_numbering_sequence.rb

Defined Under Namespace

Modules: APIOperations, CertificateBlacklist, Util Classes: APIConnectionError, APIError, APIResource, AuthenticationError, BalanceTransaction, Checkout, Coupon, CreditNote, CreditNoteNumberingSequence, Customer, Document, DocumentEmailTemplate, DocumentLanguage, DocumentTemplate, EmailsSetting, ExportsSetting, InvalidRequestError, Invoice, InvoiceNumberingSequence, Item, ListObject, OctobatError, OctobatLibError, OctobatObject, Order, PaymentRecipient, PaymentRecipientReference, PaymentSource, Payout, Product, ProformaInvoice, SingletonAPIResource, TaxEvidence, TaxEvidenceRequest, TaxRegionSetting, Transaction

Constant Summary collapse

VERSION =
'2.0.15'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

Returns the value of attribute api_base.



78
79
80
# File 'lib/octobat.rb', line 78

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key.



78
79
80
# File 'lib/octobat.rb', line 78

def api_key
  @api_key
end

.api_versionObject

Returns the value of attribute api_version.



78
79
80
# File 'lib/octobat.rb', line 78

def api_version
  @api_version
end

.initial_network_retry_delayObject (readonly)

Returns the value of attribute initial_network_retry_delay.



79
80
81
# File 'lib/octobat.rb', line 79

def initial_network_retry_delay
  @initial_network_retry_delay
end

.max_network_retry_delayObject (readonly)

Returns the value of attribute max_network_retry_delay.



79
80
81
# File 'lib/octobat.rb', line 79

def max_network_retry_delay
  @max_network_retry_delay
end

.verify_ssl_certsObject

Returns the value of attribute verify_ssl_certs.



78
79
80
# File 'lib/octobat.rb', line 78

def verify_ssl_certs
  @verify_ssl_certs
end

Class Method Details

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



82
83
84
# File 'lib/octobat.rb', line 82

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/octobat.rb', line 145

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



136
137
138
# File 'lib/octobat.rb', line 136

def self.max_network_retries
  @max_network_retries
end

.max_network_retries=(val) ⇒ Object



140
141
142
# File 'lib/octobat.rb', line 140

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



86
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/octobat.rb', line 86

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