lightspeed_ruby 0.1.8 → 0.1.9
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 +4 -4
- data/lib/lightspeed/client.rb +39 -5
- data/lib/lightspeed/configuration.rb +9 -0
- data/lib/lightspeed/version.rb +1 -1
- data/lightspeed.gemspec +1 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9cee427c44e3a97df29a3fdaf4209bdfcc00daa
|
4
|
+
data.tar.gz: 742e83a6c3d50913075bbd939f02f843b5f89c19
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dee56f0f7a79fa8d002591935debcdf19ed7a1c8dfa56da3aa10eeff7a3e9f6a8c64080172ad1fd82df8bf03a415e06b5acb4fdcbb980892a151241cd7c841c8
|
7
|
+
data.tar.gz: ecb8fc57d401871aff0bc76180921e9a29c0fe629c0c6b34c9e09487185f8a7f3a74d6009f95054c96eee7d7b5e7231af3829e5d63b3ea6ea06323de4f8bd4a6
|
data/lib/lightspeed/client.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require "lightspeed/configuration"
|
1
2
|
require "lightspeed/api/tokens"
|
2
3
|
require "lightspeed/api/accounts"
|
3
4
|
require "lightspeed/api/sales"
|
@@ -6,6 +7,14 @@ require "lightspeed/api/items"
|
|
6
7
|
require "lightspeed/api/categories"
|
7
8
|
|
8
9
|
module Lightspeed
|
10
|
+
def self.configuration
|
11
|
+
@_configuration ||= Configuration.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configure
|
15
|
+
yield configuration
|
16
|
+
end
|
17
|
+
|
9
18
|
class Client
|
10
19
|
API_BASE = "https://api.merchantos.com/API/"
|
11
20
|
|
@@ -15,25 +24,34 @@ module Lightspeed
|
|
15
24
|
API::Tokens.new(client_id, client_secret, refresh_token)
|
16
25
|
end
|
17
26
|
|
18
|
-
def initialize(access_token:, account_id:)
|
27
|
+
def initialize(access_token:, account_id:, configuration:)
|
19
28
|
@access_token = access_token
|
20
29
|
@account_id = account_id
|
30
|
+
@configuration = configuration
|
21
31
|
end
|
22
32
|
|
23
33
|
def get(url, params: {}, relations: [])
|
24
|
-
conn.get(url, params.merge(load_relations: "[#{relation_names(relations)}]"))
|
34
|
+
response = conn.get(url, params.merge(load_relations: "[#{relation_names(relations)}]"))
|
35
|
+
rate_limit_wait(response)
|
36
|
+
response
|
25
37
|
end
|
26
38
|
|
27
39
|
def post(url, body: {})
|
28
|
-
conn.post(url, body)
|
40
|
+
response = conn.post(url, body)
|
41
|
+
rate_limit_wait(response)
|
42
|
+
response
|
29
43
|
end
|
30
44
|
|
31
45
|
def delete(url)
|
32
|
-
conn.delete(url)
|
46
|
+
response = conn.delete(url)
|
47
|
+
rate_limit_wait(response)
|
48
|
+
response
|
33
49
|
end
|
34
50
|
|
35
51
|
def put(url, body: {})
|
36
|
-
conn.put(url, body)
|
52
|
+
response = conn.put(url, body)
|
53
|
+
rate_limit_wait(response)
|
54
|
+
response
|
37
55
|
end
|
38
56
|
|
39
57
|
def accounts; API::Accounts.new(self); end
|
@@ -44,6 +62,8 @@ module Lightspeed
|
|
44
62
|
|
45
63
|
private
|
46
64
|
|
65
|
+
attr_reader :configuration
|
66
|
+
|
47
67
|
def conn
|
48
68
|
@conn ||= Faraday.new(API_BASE) do |f|
|
49
69
|
f.authorization :Bearer, access_token if access_token
|
@@ -58,5 +78,19 @@ module Lightspeed
|
|
58
78
|
"\"#{relation.to_s.camelize}\""
|
59
79
|
end.join(",")
|
60
80
|
end
|
81
|
+
|
82
|
+
def rate_limit_wait(response, points_expense: 10, points_threshold_percentage: 0.5)
|
83
|
+
return unless Lightspeed.configuration.rate_limit || configuration.rate_limit
|
84
|
+
|
85
|
+
bucket_level, bucket_limit = response.headers["x-ls-api-bucket-level"].split("/").map(&:to_f)
|
86
|
+
drip_level = response.headers["x-ls-api-drip-rate"].to_f
|
87
|
+
threshold = bucket_limit * points_threshold_percentage
|
88
|
+
|
89
|
+
return if bucket_level + points_expense <= threshold
|
90
|
+
|
91
|
+
points_over = bucket_level + points_expense - threshold
|
92
|
+
wait_for_points = points_over / drip_level
|
93
|
+
sleep(wait_for_points)
|
94
|
+
end
|
61
95
|
end
|
62
96
|
end
|
data/lib/lightspeed/version.rb
CHANGED
data/lightspeed.gemspec
CHANGED
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_development_dependency "bundler", "~> 1.14"
|
25
25
|
spec.add_development_dependency "rake", "~> 10.0"
|
26
26
|
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
spec.add_development_dependency "webmock", "~> 3.1.1"
|
27
28
|
|
28
29
|
spec.add_dependency "faraday", "~> 0.12"
|
29
30
|
spec.add_dependency "faraday_middleware", "~> 0.11"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lightspeed_ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zamith
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: webmock
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.1.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.1.1
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: faraday
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -107,6 +121,7 @@ files:
|
|
107
121
|
- lib/lightspeed/api/sales/sale_lines.rb
|
108
122
|
- lib/lightspeed/api/tokens.rb
|
109
123
|
- lib/lightspeed/client.rb
|
124
|
+
- lib/lightspeed/configuration.rb
|
110
125
|
- lib/lightspeed/errors/missing_refresh_token.rb
|
111
126
|
- lib/lightspeed/version.rb
|
112
127
|
- lightspeed.gemspec
|