Class: QboApi

Inherits:
Object
  • Object
show all
Extended by:
Configuration
Includes:
Entity, 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/version.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. 500 Internal Server Error An error occured 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: Configuration, Entity, Util Classes: BadRequest, Error, Forbidden, InternalServerError, NotFound, NotImplementedError, ServiceUnavailable, 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.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configuration

log, log=, logger, logger=, production, production=

Methods included from Util

#cdc_time

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

Constructor Details

#initialize(token:, token_secret:, realm_id:, consumer_key: CONSUMER_KEY, consumer_secret: CONSUMER_SECRET, endpoint: :accounting) ⇒ QboApi

Returns a new instance of QboApi.



28
29
30
31
32
33
34
35
36
# File 'lib/qbo_api.rb', line 28

def initialize(token:, token_secret:, realm_id:, consumer_key: CONSUMER_KEY, 
               consumer_secret: CONSUMER_SECRET, endpoint: :accounting)
  @consumer_key = consumer_key
  @consumer_secret = consumer_secret
  @token = token
  @token_secret = token_secret
  @realm_id = realm_id
  @endpoint = endpoint
end

Instance Attribute Details

#realm_idObject (readonly)

Returns the value of attribute realm_id.



18
19
20
# File 'lib/qbo_api.rb', line 18

def realm_id
  @realm_id
end

Instance Method Details

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



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/qbo_api.rb', line 92

def all(entity, max: 1000, select: nil, &block)
  select ||= "SELECT * FROM #{singular(entity)}"
  pos = 0
  begin
    pos = pos == 0 ? pos + 1 : pos + max
    results = query("#{select} MAXRESULTS #{max} STARTPOSITION #{pos}")
    results.each do |entry|
      yield(entry)
    end if results
  end while (results ? results.size == max : false)
end

#cdc(entities:, changed_since:) ⇒ Object



56
57
58
59
# File 'lib/qbo_api.rb', line 56

def cdc(entities:, changed_since:)
  path = "#{realm_id}/cdc?entities=#{entities}&changedSince=#{cdc_time(changed_since)}"
  request(:get, path: path)
end

#connection(url: get_endpoint) ⇒ Object



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

def connection(url: get_endpoint)
  Faraday.new(url: url) do |faraday|
    faraday.headers['Content-Type'] = 'application/json;charset=UTF-8'
    faraday.headers['Accept'] = "application/json"
    faraday.request :oauth, oauth_data 
    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:) ⇒ Object



66
67
68
# File 'lib/qbo_api.rb', line 66

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

#delete(entity, id:) ⇒ Object



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

def delete(entity, id:)
  raise QboApi::NotImplementedError unless is_transaction_entity?(entity)
  path = "#{entity_path(entity)}?operation=delete"
  payload = set_update(entity, id)
  request(:post, entity: entity, path: path, payload: payload)
end

#disconnectObject



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

def disconnect
  response = connection(url: APP_CONNECTION_URL).get('/disconnect')
end

#esc(query) ⇒ Object



129
130
131
# File 'lib/qbo_api.rb', line 129

def esc(query)
  query.gsub("'", "\\\\'")
end

#get(entity, id) ⇒ Object



61
62
63
64
# File 'lib/qbo_api.rb', line 61

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

#query(query) ⇒ Object



50
51
52
53
54
# File 'lib/qbo_api.rb', line 50

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

#reconnectObject



88
89
90
# File 'lib/qbo_api.rb', line 88

def reconnect
  response = connection(url: APP_CONNECTION_URL).get('/reconnect')
end

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



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/qbo_api.rb', line 104

def request(method, path:, entity: nil, payload: nil)
  raw_response = connection.send(method) do |req|
    case method
    when :get, :delete
      req.url URI.encode(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



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/qbo_api.rb', line 117

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:) ⇒ Object



70
71
72
73
# File 'lib/qbo_api.rb', line 70

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