Class: QboApi

Inherits:
Object
  • Object
show all
Extended by:
Configuration
Includes:
Attachment, Builder, Entity, Setter, Supporting, Util
Defined in:
lib/qbo_api/error.rb,
lib/qbo_api.rb,
lib/qbo_api/util.rb,
lib/qbo_api/entity.rb,
lib/qbo_api/setter.rb,
lib/qbo_api/builder.rb,
lib/qbo_api/version.rb,
lib/qbo_api/attachment.rb,
lib/qbo_api/supporting.rb,
lib/qbo_api/configuration.rb

Overview

200 OK The request succeeded. However, the response body may contain a <Fault> element, indicating an error. 400 Bad request Generally, the request cannot be fulfilled due to bad syntax. In some cases, this response code is returned for a request with bad authorization data. 401 Unauthorized Authentication or authorization has failed. 403 Forbidden The resource is forbidden. 404 Not Found The resource is not found. 429 Too Many Requests API Throttling/ Rate limiting 500 Internal Server Error An error occurred on the server while processing the request. Resubmit request once; if it persists, contact developer support. 503 Service Unavailable The service is temporarily unavailable. Custom error class for rescuing from all QuickBooks Online errors

Defined Under Namespace

Modules: Attachment, Builder, Configuration, Entity, Setter, Supporting, Util Classes: BadRequest, Error, Forbidden, InternalServerError, NotFound, NotImplementedError, ServiceUnavailable, TooManyRequests, Unauthorized

Constant Summary collapse

REQUEST_TOKEN_URL =
'https://oauth.intuit.com/oauth/v1/get_request_token'
ACCESS_TOKEN_URL =
'https://oauth.intuit.com/oauth/v1/get_access_token'
APP_CENTER_BASE =
'https://appcenter.intuit.com'
APP_CENTER_URL =
APP_CENTER_BASE + '/Connect/Begin?oauth_token='
V3_ENDPOINT_BASE_URL =
'https://sandbox-quickbooks.api.intuit.com/v3/company/'
PAYMENTS_API_BASE_URL =
'https://sandbox.api.intuit.com/quickbooks/v4/payments'
APP_CONNECTION_URL =
APP_CENTER_BASE + '/api/v1/connection'
VERSION =
"1.6.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configuration

log, log=, logger, logger=, minor_version, minor_version=, production, production=, request_id, request_id=

Methods included from Attachment

#upload_attachment

Methods included from Util

#add_minor_version_to, #add_params_to_path, #add_request_id_to, #build_all_query, #cdc_time, #esc, #finalize_path, #join_or_start_where_clause!, #uuid

Methods included from Entity

#entity_path, #extract_entity_from_query, #is_name_list_entity?, #is_transaction_entity?, #name_list_entities, #singular, #snake_to_camel, #supporting_entities, #transaction_entities

Methods included from Supporting

#batch, #cdc, #reports

Constructor Details

#initialize(token: nil, token_secret: nil, access_token: nil, realm_id:, consumer_key: nil, consumer_secret: nil, endpoint: :accounting) ⇒ QboApi

Returns a new instance of QboApi.



38
39
40
41
42
43
44
45
46
47
# File 'lib/qbo_api.rb', line 38

def initialize(token: nil, token_secret: nil, access_token: nil, realm_id:,
               consumer_key: nil, consumer_secret: nil, endpoint: :accounting)
  @consumer_key = consumer_key || (defined?(CONSUMER_KEY) ? CONSUMER_KEY : nil)
  @consumer_secret = consumer_secret || (defined?(CONSUMER_SECRET) ? CONSUMER_SECRET : nil)
  @token = token
  @token_secret = token_secret
  @access_token = access_token
  @realm_id = realm_id
  @endpoint = endpoint
end

Instance Attribute Details

#realm_idObject (readonly)

Returns the value of attribute realm_id.



28
29
30
# File 'lib/qbo_api.rb', line 28

def realm_id
  @realm_id
end

Instance Method Details

#all(entity, max: 1000, select: nil, inactive: false, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/qbo_api.rb', line 113

def all(entity, max: 1000, select: nil, inactive: false, &block)
  enumerator = create_all_enumerator(entity, max: max, select: select, inactive: inactive)

  if block_given?
    enumerator.each(&block)
  else
    enumerator
  end
end

#connection(url: get_endpoint) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/qbo_api.rb', line 49

def connection(url: get_endpoint)
  @connection ||= Faraday.new(url: url) do |faraday|
    faraday.headers['Content-Type'] = 'application/json;charset=UTF-8'
    faraday.headers['Accept'] = 'application/json'
    if @token != nil
      faraday.request :oauth, oauth_data
    elsif @access_token != nil
      faraday.request :oauth2, @access_token, token_type: 'bearer'
    else
      raise QboApi::Error.new error_body: 'Must set either the token or access_token'
    end
    faraday.request :url_encoded
    faraday.use FaradayMiddleware::RaiseHttpException
    faraday.response :detailed_logger, QboApi.logger if QboApi.log
    faraday.adapter  Faraday.default_adapter
  end
end

#create(entity, payload:, params: nil) ⇒ Object



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

def create(entity, payload:, params: nil)
  request(:post, entity: entity, path: entity_path(entity), payload: payload, params: params)
end

#deactivate(entity, id:) ⇒ Object



95
96
97
98
99
100
# File 'lib/qbo_api.rb', line 95

def deactivate(entity, id:)
  err_msg = "Deactivate is only for name list entities. Use .delete instead"
  raise QboApi::NotImplementedError.new, err_msg unless is_name_list_entity?(entity)
  payload = set_deactivate(entity, id)
  request(:post, entity: entity, path: entity_path(entity), payload: payload)
end

#delete(entity, id:) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/qbo_api.rb', line 87

def delete(entity, id:)
  err_msg = "Delete is only for transaction entities. Use .deactivate instead"
  raise QboApi::NotImplementedError.new, err_msg unless is_transaction_entity?(entity)
  path = add_params_to_path(path: entity_path(entity), params: { operation: :delete })
  payload = set_update(entity, id)
  request(:post, entity: entity, path: path, payload: payload)
end

#disconnectObject



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

def disconnect
  path = "#{APP_CONNECTION_URL}/disconnect"
  request(:get, path: path)
end

#get(entity, id, params: nil) ⇒ Object



73
74
75
76
# File 'lib/qbo_api.rb', line 73

def get(entity, id, params: nil)
  path = "#{entity_path(entity)}/#{id}"
  request(:get, entity: entity, path: path, params: params)
end

#query(query, params: nil) ⇒ Object



67
68
69
70
71
# File 'lib/qbo_api.rb', line 67

def query(query, params: nil)
  path = "#{realm_id}/query?query=#{CGI.escape(query)}"
  entity = extract_entity_from_query(query, to_sym: true)
  request(:get, entity: entity, path: path, params: params)
end

#reconnectObject



108
109
110
111
# File 'lib/qbo_api.rb', line 108

def reconnect
  path = "#{APP_CONNECTION_URL}/reconnect"
  request(:get, path: path)
end

#request(method, path:, entity: nil, payload: nil, params: nil) ⇒ Object



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

def request(method, path:, entity: nil, payload: nil, params: nil)
  raw_response = connection.send(method) do |req|
    path = finalize_path(path, method: method, params: params)
    case method
    when :get, :delete
      req.url path
    when :post, :put
      req.url path
      req.body = JSON.generate(payload)
    end
  end
  response(raw_response, entity: entity)
end

#response(resp, entity: nil) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/qbo_api.rb', line 137

def response(resp, entity: nil)
  data = JSON.parse(resp.body)
  if entity
    entity_response(data, entity)
  else
    data
  end
rescue => e
  # Catch fetch key errors and just return JSON
  data
end

#update(entity, id:, payload:, params: nil) ⇒ Object



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

def update(entity, id:, payload:, params: nil)
  payload.merge!(set_update(entity, id))
  request(:post, entity: entity, path: entity_path(entity), payload: payload, params: params)
end