qbo_api 1.8.0 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c9489158fdcc87c75c395c4e51c4f6faa6850c5
4
- data.tar.gz: a4ed41891faaa78efd78a540937b0e444287e88e
3
+ metadata.gz: 67f39d1f93ec40aaaf360187cd576f68d9cc5c06
4
+ data.tar.gz: 37b06114ee6d7f18b4fd18fc795804ed252d0176
5
5
  SHA512:
6
- metadata.gz: 8f35f5150430bab828dc43853cd2640fd81ec1ce175e08600e286489abf79e85f9939e1cebde2ae825874865cd343b312f0b5d94d7e0e8d9be837301ccc16347
7
- data.tar.gz: a58e610b9fef2f7f0756534b1898549beb66df6dabc955d75c349db6c391f17b33844fb9ece8b7c9c431d3eddf4178e7f9147ec4cd438d0af3f8edaa1d284768
6
+ metadata.gz: 11d28f4225f8565b01dc097cc602a77f2249d54879204536a9372fc67b0a344110c9da63323605c6e7703fa90ac9714bcd4d947fc653879268b3f88743fc5d82
7
+ data.tar.gz: 172082e7b1e7772eae2d5450ec9e726a37178fc5c3ff357ba6fd9a0facc953177ac758568094f5e41c53ceb70b8ea942038ecacf5da6eb38d8be79b8de308ea3
@@ -22,42 +22,46 @@ class QboApi
22
22
  include Attachment
23
23
  include ApiMethods
24
24
 
25
- attr_reader :realm_id
25
+ attr_accessor :realm_id
26
+ attr_accessor :endpoint
26
27
 
27
- REQUEST_TOKEN_URL = 'https://oauth.intuit.com/oauth/v1/get_request_token'
28
- ACCESS_TOKEN_URL = 'https://oauth.intuit.com/oauth/v1/get_access_token'
29
- APP_CENTER_BASE = 'https://appcenter.intuit.com'
30
- APP_CENTER_URL = APP_CENTER_BASE + '/Connect/Begin?oauth_token='
31
28
  V3_ENDPOINT_BASE_URL = 'https://sandbox-quickbooks.api.intuit.com/v3/company/'
32
29
  PAYMENTS_API_BASE_URL = 'https://sandbox.api.intuit.com/quickbooks/v4/payments'
33
- APP_CONNECTION_URL = APP_CENTER_BASE + '/api/v1/connection'
34
30
  LOG_TAG = "[QuickBooks]"
35
31
 
36
- def initialize(token: nil, token_secret: nil, access_token: nil, realm_id:,
37
- consumer_key: nil, consumer_secret: nil, endpoint: :accounting)
38
- @consumer_key = consumer_key || (defined?(CONSUMER_KEY) ? CONSUMER_KEY : nil)
39
- @consumer_secret = consumer_secret || (defined?(CONSUMER_SECRET) ? CONSUMER_SECRET : nil)
40
- @token = token
41
- @token_secret = token_secret
42
- @access_token = access_token
43
- @realm_id = realm_id
44
- @endpoint = endpoint
32
+ # @param attributes [Hash<Symbol,String>]
33
+ def initialize(attributes = {})
34
+ raise ArgumentError, "missing keyword: realm_id" unless attributes.key?(:realm_id)
35
+ attributes = default_attributes.merge!(attributes)
36
+ attributes.each do |attribute, value|
37
+ public_send("#{attribute}=", value)
38
+ end
45
39
  @endpoint_url = get_endpoint
46
40
  end
47
41
 
48
- def connection(url: @endpoint_url)
42
+ def default_attributes
43
+ {
44
+ endpoint: :accounting
45
+ }
46
+ end
47
+
48
+ def connection(url: endpoint_url)
49
49
  @connection ||= authorized_json_connection(url)
50
50
  end
51
51
 
52
+ def endpoint_url
53
+ @endpoint_url.dup
54
+ end
55
+
52
56
  private
53
57
 
54
58
  def get_endpoint
55
59
  prod = self.class.production
56
- case @endpoint
57
- when :accounting
58
- prod ? V3_ENDPOINT_BASE_URL.sub("sandbox-", '') : V3_ENDPOINT_BASE_URL
59
- when :payments
60
- prod ? PAYMENTS_API_BASE_URL.sub("sandbox.", '') : PAYMENTS_API_BASE_URL
60
+ {
61
+ accounting: prod ? V3_ENDPOINT_BASE_URL.sub("sandbox-", '') : V3_ENDPOINT_BASE_URL,
62
+ payments: prod ? PAYMENTS_API_BASE_URL.sub("sandbox.", '') : PAYMENTS_API_BASE_URL
63
+ }.fetch(endpoint) do
64
+ raise KeyError, "Invalid endpoint: #{endpoint.inspect}"
61
65
  end
62
66
  end
63
67
  end
@@ -17,7 +17,7 @@ class QboApi
17
17
  end
18
18
 
19
19
  def attachment_connection
20
- @attachment_connection ||= authorized_multipart_connection(@endpoint_url)
20
+ @attachment_connection ||= authorized_multipart_connection(endpoint_url)
21
21
  end
22
22
 
23
23
  end
@@ -4,6 +4,11 @@ require 'faraday/detailed_logger'
4
4
 
5
5
  class QboApi
6
6
  module Connection
7
+ AUTHORIZATION_MIDDLEWARES = []
8
+
9
+ def Connection.add_authorization_middleware(strategy_name)
10
+ Connection::AUTHORIZATION_MIDDLEWARES << strategy_name
11
+ end
7
12
 
8
13
  def authorized_json_connection(url, headers: nil)
9
14
  headers ||= {}
@@ -81,20 +86,6 @@ class QboApi
81
86
  end
82
87
  end
83
88
 
84
- # Part of the OAuth1 API
85
- # https://developer.intuit.com/docs/0100_quickbooks_online/0100_essentials/0085_develop_quickbooks_apps/0004_authentication_and_authorization/oauth_management_api#/Reconnect
86
- def disconnect
87
- path = "#{APP_CONNECTION_URL}/disconnect"
88
- request(:get, path: path)
89
- end
90
-
91
- # Part of the OAuth1 API
92
- # https://developer.intuit.com/docs/0100_quickbooks_online/0100_essentials/0085_develop_quickbooks_apps/0004_authentication_and_authorization/oauth_management_api#/Reconnect
93
- def reconnect
94
- path = "#{APP_CONNECTION_URL}/reconnect"
95
- request(:get, path: path)
96
- end
97
-
98
89
  private
99
90
 
100
91
  def entity_response(data, entity)
@@ -125,33 +116,22 @@ class QboApi
125
116
  end
126
117
 
127
118
  def add_authorization_middleware(conn)
128
- if @token != nil
129
- # Part of the OAuth1 API
130
- gem 'simple_oauth'
131
- require 'simple_oauth'
132
- conn.request :oauth, oauth_data
133
- elsif @access_token != nil
134
- conn.request :oauth2, @access_token, token_type: 'bearer'
135
- else
136
- raise QboApi::Error.new error_body: 'Must set either the token or access_token'
119
+ Connection::AUTHORIZATION_MIDDLEWARES.find(proc do
120
+ raise QboApi::Error.new error_body: 'Add a configured authorization_middleware'
121
+ end) do |strategy_name|
122
+ next unless public_send("use_#{strategy_name}_middleware?")
123
+ public_send("add_#{strategy_name}_authorization_middleware", conn)
124
+ true
137
125
  end
138
126
  end
139
127
 
140
- # Part of the OAuth1 API
141
- # Use with simple_oauth OAuth1 middleware
142
- # @see #add_authorization_middleware
143
- def oauth_data
144
- {
145
- consumer_key: @consumer_key,
146
- consumer_secret: @consumer_secret,
147
- token: @token,
148
- token_secret: @token_secret
149
- }
150
- end
151
-
152
128
  def entity_name(entity)
153
129
  singular(entity)
154
130
  end
155
131
 
132
+ require_relative 'connection/oauth1'
133
+ include OAuth1
134
+ require_relative 'connection/oauth2'
135
+ include OAuth2
156
136
  end
157
137
  end
@@ -0,0 +1,60 @@
1
+ class QboApi
2
+ APP_CENTER_BASE = 'https://appcenter.intuit.com'
3
+ APP_CENTER_URL = APP_CENTER_BASE + '/Connect/Begin?oauth_token='
4
+ APP_CONNECTION_URL = APP_CENTER_BASE + '/api/v1/connection'
5
+
6
+ attr_accessor :token, :token_secret
7
+ attr_accessor :consumer_key, :consumer_secret
8
+
9
+ module Connection::OAuth1
10
+
11
+ def self.included(*)
12
+ QboApi::Connection.add_authorization_middleware :oauth1
13
+ super
14
+ end
15
+
16
+ def default_attributes
17
+ super.merge!(
18
+ token: nil, token_secret: nil,
19
+ consumer_key: defined?(CONSUMER_KEY) ? CONSUMER_KEY : nil,
20
+ consumer_secret: defined?(CONSUMER_SECRET) ? CONSUMER_SECRET : nil,
21
+ )
22
+ end
23
+
24
+ def add_oauth1_authorization_middleware(conn)
25
+ gem 'simple_oauth'
26
+ require 'simple_oauth'
27
+ conn.request :oauth, oauth_data
28
+ end
29
+
30
+ def use_oauth1_middleware?
31
+ token != nil
32
+ end
33
+
34
+ # https://developer.intuit.com/docs/0100_quickbooks_online/0100_essentials/0085_develop_quickbooks_apps/0004_authentication_and_authorization/oauth_management_api#/Reconnect
35
+ def disconnect
36
+ path = "#{APP_CONNECTION_URL}/disconnect"
37
+ request(:get, path: path)
38
+ end
39
+
40
+ # https://developer.intuit.com/docs/0100_quickbooks_online/0100_essentials/0085_develop_quickbooks_apps/0004_authentication_and_authorization/oauth_management_api#/Reconnect
41
+ def reconnect
42
+ path = "#{APP_CONNECTION_URL}/reconnect"
43
+ request(:get, path: path)
44
+ end
45
+
46
+ private
47
+
48
+ # Use with simple_oauth OAuth1 middleware
49
+ # @see #add_authorization_middleware
50
+ def oauth_data
51
+ {
52
+ consumer_key: @consumer_key,
53
+ consumer_secret: @consumer_secret,
54
+ token: @token,
55
+ token_secret: @token_secret
56
+ }
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,24 @@
1
+ class QboApi
2
+ attr_accessor :access_token
3
+
4
+ module Connection::OAuth2
5
+
6
+ def self.included(*)
7
+ QboApi::Connection.add_authorization_middleware :oauth2
8
+ super
9
+ end
10
+
11
+ def default_attributes
12
+ super.merge!(
13
+ access_token: nil
14
+ )
15
+ end
16
+ def add_oauth2_authorization_middleware(conn)
17
+ conn.request :oauth2, access_token, token_type: 'bearer'
18
+ end
19
+
20
+ def use_oauth2_middleware?
21
+ access_token != nil
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  class QboApi
2
- VERSION = "1.8.0"
2
+ VERSION = "1.8.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qbo_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Pelczarski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-24 00:00:00.000000000 Z
11
+ date: 2018-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -276,6 +276,8 @@ files:
276
276
  - lib/qbo_api/attachment.rb
277
277
  - lib/qbo_api/configuration.rb
278
278
  - lib/qbo_api/connection.rb
279
+ - lib/qbo_api/connection/oauth1.rb
280
+ - lib/qbo_api/connection/oauth2.rb
279
281
  - lib/qbo_api/entity.rb
280
282
  - lib/qbo_api/error.rb
281
283
  - lib/qbo_api/raise_http_exception.rb