lightspeed_pos 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3960f7c39629798ffae0406f84fb2cbcf897390
4
- data.tar.gz: f900f23a42324bf61268b0ab4f43ac0528971c38
3
+ metadata.gz: 3e50edadd52d55b1ed3b2def3b6b10b765d8ac2a
4
+ data.tar.gz: b59ae04766b48a27126491e94a4b88fbf7a71902
5
5
  SHA512:
6
- metadata.gz: 7e7e1f10edaa984d5e232c9706f32e3b6d13bb8cae0cb3c5e00f773335493a3372085e7d85c7411ccb425f20fe8ab341d98075273d39f5e418d839b32cf99478
7
- data.tar.gz: 856a6628c9c7c4dd36b10e25a253bc394230600e5632c3e961242a285e9154d34b99564fb063e06ebf376feb7ffd74ddd7397faaa9a2e20a08206f4e7a02e474
6
+ metadata.gz: 7951c24a7ee73b3840a1145b8f8c8d797e074a1b03277ea4fe814618fd7045116a41d06ae9ea1c8c723f1561e1b84547c0799a5a3d0d13f74fe0bf500a9841bd
7
+ data.tar.gz: f1c67e762048bfc0a08660027e2742ed6d3d85b33467062cb558b0569fc094498f2490ee41c6e3f250cc39b0316ffd971b3a1162181a8b943b8ccb512beee387
@@ -49,8 +49,8 @@ IndentHash:
49
49
  IndentationWidth:
50
50
  Enabled: false
51
51
 
52
- CaseIndentation:
53
- IndentWhenRelativeTo: end
52
+ Layout/CaseIndentation:
53
+ EnforcedStyle: end
54
54
 
55
55
  FormatString:
56
56
  Enabled: false
@@ -65,5 +65,8 @@ SignalException:
65
65
  DoubleNegation:
66
66
  Enabled: false
67
67
 
68
- TrailingComma:
68
+ Style/TrailingCommaInLiteral:
69
+ Enabled: false
70
+
71
+ Style/TrailingCommaInArguments:
69
72
  Enabled: false
@@ -9,9 +9,11 @@ Not all endpoints are implemented yet, but you can help by submitting pull reque
9
9
  First, intialize a new client:
10
10
 
11
11
  ```ruby
12
- client = Lightspeed::Client.new(oauth_token: "YOUR_ACCESS_TOKEN_HERE")
12
+ client = Lightspeed::Client.new(oauth_token_holder: yourOAuthAwareObjectHere)
13
13
  ```
14
14
 
15
+ `yourOAuthAwareObjectHere` must be an object that responds to `oauth_token` and `refresh_oauth_token`. `oauth_token` must return an access token, and `refresh_oauth_token` must handle the refreshing of expired access tokens, so that a subsequent call to `oauth_token` will return a new, unexpired access token.
16
+
15
17
  Next, make a request for your accounts:
16
18
 
17
19
  ```ruby
@@ -5,13 +5,31 @@ require 'dotenv'
5
5
  require 'lightspeed_pos'
6
6
  Lightspeed::Request.verbose = true
7
7
 
8
+ class TokenHolder
9
+ def initialize(token: nil, refresh_token: nil)
10
+ @token = token
11
+ @refresh_token = refresh_token
12
+ end
13
+
14
+ def oauth_token
15
+ @token
16
+ end
17
+
18
+ def refresh_oauth_token
19
+ raise "Token expired, refresh not yet implemented"
20
+ end
21
+ end
22
+
8
23
  # require "pry"
9
24
  # Pry.start
10
25
  Dotenv.load
11
26
  def client
12
27
  token = ENV['LIGHTSPEED_OAUTH_TOKEN']
28
+ refresh_token = ENV['LIGHTSPEED_OAUTH_REFRESH_TOKEN']
13
29
  raise 'set LIGHTSPEED_OAUTH_TOKEN as an envorinment variable to use this' unless token
14
- @client ||= Lightspeed::Client.new(oauth_token: token)
30
+ raise 'set LIGHTSPEED_OAUTH_REFRESH_TOKEN as an envorinment variable to use this' unless refresh_token
31
+ token_holder = Tokenholder.new(token: token, refresh_token: refresh_token)
32
+ @client ||= Lightspeed::Client.new(oauth_token_holder: token_holder)
15
33
  end
16
34
 
17
35
  def account
@@ -7,6 +7,7 @@ require_relative 'item_attribute_sets'
7
7
  require_relative 'images'
8
8
  require_relative 'inventories'
9
9
  require_relative 'orders'
10
+ require_relative 'price_levels'
10
11
  require_relative 'sales'
11
12
  require_relative 'shops'
12
13
  require_relative 'special_orders'
@@ -28,6 +29,7 @@ module Lightspeed
28
29
  :ItemAttributeSets,
29
30
  :Items,
30
31
  :Orders,
32
+ :PriceLevels,
31
33
  :Sales,
32
34
  :Shops,
33
35
  :SpecialOrders,
@@ -6,11 +6,13 @@ require_relative 'request_throttler'
6
6
 
7
7
  module Lightspeed
8
8
  class Client
9
- attr_accessor :oauth_token, :throttler
9
+ attr_accessor :throttler
10
10
 
11
- def initialize(oauth_token: nil)
12
- @oauth_token = oauth_token
11
+ def initialize(oauth_token_holder: nil, oauth_token: nil)
12
+ @oauth_token_holder = oauth_token_holder
13
13
  @throttler = Lightspeed::RequestThrottler.new
14
+
15
+ raise "Passing an oauth token is no longer supported. Pass a token holder instead." if oauth_token
14
16
  end
15
17
 
16
18
  # Returns a list of accounts that you have access to.
@@ -18,14 +20,6 @@ module Lightspeed
18
20
  @accounts ||= Lightspeed::Accounts.new(context: self)
19
21
  end
20
22
 
21
- def load_json(json)
22
- data = JSON.parse(json)
23
- Array.wrap(data).map do |resource|
24
- resource = resource_class.new(context: self, attributes: resource)
25
- @resources[resource.id] = resource
26
- end
27
- end
28
-
29
23
  def get(**args)
30
24
  perform_request(args.merge(method: :get))
31
25
  end
@@ -42,6 +36,14 @@ module Lightspeed
42
36
  perform_request(args.merge(method: :delete))
43
37
  end
44
38
 
39
+ def oauth_token
40
+ @oauth_token_holder.oauth_token
41
+ end
42
+
43
+ def refresh_oauth_token
44
+ @oauth_token_holder.refresh_oauth_token
45
+ end
46
+
45
47
  private
46
48
 
47
49
  def perform_request(**args)
@@ -51,6 +53,5 @@ module Lightspeed
51
53
  def request **args
52
54
  Lightspeed::Request.new(self, **args)
53
55
  end
54
-
55
56
  end
56
57
  end
@@ -1,7 +1,5 @@
1
1
  require 'active_support/core_ext/string'
2
2
  require 'active_support/core_ext/array/wrap'
3
- require 'active_support/json'
4
- require 'active_support/core_ext/object/json'
5
3
 
6
4
  module Lightspeed
7
5
  class Collection
@@ -22,10 +20,6 @@ module Lightspeed
22
20
  @resources = {}
23
21
  end
24
22
 
25
- def load_json(json)
26
- instantiate(JSON.parse(json))
27
- end
28
-
29
23
  def client
30
24
  return context if context.is_a?(Lightspeed::Client)
31
25
  account.client
@@ -94,11 +88,11 @@ module Lightspeed
94
88
  end
95
89
 
96
90
  def create(attributes = {})
97
- instantiate(post(body: attributes.to_json)).first
91
+ instantiate(post(body: Yajl::Encoder.encode(attributes))).first
98
92
  end
99
93
 
100
94
  def update(id, attributes = {})
101
- instantiate(put(id, body: attributes.to_json)).first
95
+ instantiate(put(id, body: Yajl::Encoder.encode(attributes))).first
102
96
  end
103
97
 
104
98
  def destroy(id)
@@ -125,14 +119,14 @@ module Lightspeed
125
119
  "#<#{self.class.name} API#{base_path}>"
126
120
  end
127
121
 
128
- def as_json(*args)
122
+ def as_json
129
123
  return if all_loaded.empty?
130
- { resource_name => all_loaded.as_json(*args) }
124
+ { resource_name => all_loaded.as_json }
131
125
  end
132
126
  alias_method :to_h, :as_json
133
127
 
134
- def to_json(*args)
135
- as_json.to_json(*args)
128
+ def to_json
129
+ Yajl::Encoder.encode(as_json)
136
130
  end
137
131
 
138
132
  def page(n, per_page: PER_PAGE, params: {})
@@ -1,9 +1,10 @@
1
1
  module Lightspeed
2
2
  class Error < Exception
3
- class BadRequest < Exception; end # 400
4
- class Unauthorized < Exception; end # 401
5
- class NotFound < Exception; end # 404
6
- class InternalServerError < Exception; end # 500
7
- class Throttled < Exception; end # 503
3
+ class BadRequest < Lightspeed::Error; end # 400
4
+ class Unauthorized < Lightspeed::Error; end # 401
5
+ class NotAuthorized < Lightspeed::Error; end # 403
6
+ class NotFound < Lightspeed::Error; end # 404
7
+ class InternalServerError < Lightspeed::Error; end # 500
8
+ class Throttled < Lightspeed::Error; end # 503
8
9
  end
9
10
  end
@@ -0,0 +1,14 @@
1
+ require_relative 'resource'
2
+
3
+ module Lightspeed
4
+ class PriceLevel < Lightspeed::Resource
5
+ fields(
6
+ priceLevelID: :id,
7
+ name: :string,
8
+ archived: :boolean,
9
+ canBeArchived: :boolean,
10
+ type: :string,
11
+ Calculation: :String
12
+ )
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ require_relative 'collection'
2
+
3
+ require_relative 'price_level'
4
+
5
+ module Lightspeed
6
+ class PriceLevels < Lightspeed::Collection
7
+ end
8
+ end
@@ -17,7 +17,7 @@ module Lightspeed
17
17
  alias_method :to_h, :as_json
18
18
 
19
19
  def to_json
20
- as_json.to_json
20
+ Yajl::Encoder.encode(as_json)
21
21
  end
22
22
 
23
23
  def [](key)
@@ -34,10 +34,15 @@ module Lightspeed
34
34
  @raw_request = request_class.new(uri)
35
35
  @raw_request.body = body if body
36
36
  @raw_request.set_form_data(@params) if @params && @method != :get
37
- @raw_request["Authorization"] = "OAuth #{client.oauth_token}" if client.oauth_token
37
+ @client = client
38
+ set_authorization_header
38
39
  end
39
40
 
40
- def perform
41
+ def set_authorization_header
42
+ @raw_request["Authorization"] = "Bearer #{@client.oauth_token}" if @client.oauth_token
43
+ end
44
+
45
+ def perform_raw
41
46
  response = @http.request(raw_request)
42
47
  extract_rate_limits(response)
43
48
  if response.code == "200"
@@ -45,14 +50,22 @@ module Lightspeed
45
50
  else
46
51
  handle_error(response)
47
52
  end
53
+ end
54
+
55
+ def perform
56
+ perform_raw
48
57
  rescue Lightspeed::Error::Throttled
49
58
  retry_throttled_request
59
+ rescue Lightspeed::Error::Unauthorized
60
+ @client.refresh_oauth_token
61
+ set_authorization_header
62
+ perform_raw
50
63
  end
51
64
 
52
65
  private
53
66
 
54
67
  def handle_success(response)
55
- json = JSON.parse(response.body)
68
+ json = Yajl::Parser.parse(response.body)
56
69
  pp json if self.class.verbose?
57
70
  json
58
71
  end
@@ -64,10 +77,11 @@ module Lightspeed
64
77
  end
65
78
 
66
79
  def handle_error(response)
67
- data = JSON.parse(response.body)
80
+ data = Yajl::Parser.parse(response.body)
68
81
  error = case response.code.to_s
69
82
  when '400' then Lightspeed::Error::BadRequest
70
83
  when '401' then Lightspeed::Error::Unauthorized
84
+ when '403' then Lightspeed::Error::NotAuthorized
71
85
  when '404' then Lightspeed::Error::NotFound
72
86
  when '429' then Lightspeed::Error::Throttled
73
87
  when /5../ then Lightspeed::Error::InternalServerError
@@ -1,8 +1,6 @@
1
1
  require 'bigdecimal'
2
2
  require 'active_support/core_ext/string'
3
3
  require 'active_support/core_ext/hash/slice'
4
- require 'active_support/json'
5
- require 'active_support/core_ext/object/json'
6
4
 
7
5
  require_relative 'collection'
8
6
 
@@ -74,7 +72,7 @@ module Lightspeed
74
72
  end
75
73
 
76
74
  def update(attributes = {})
77
- self.attributes = put(body: attributes.to_json)[resource_name]
75
+ self.attributes = put(body: Yajl::Encoder.encode(attributes))[resource_name]
78
76
  end
79
77
 
80
78
  def destroy
@@ -115,12 +113,12 @@ module Lightspeed
115
113
  end
116
114
 
117
115
 
118
- def to_json(*args)
119
- as_json.to_json(*args)
116
+ def to_json
117
+ Yajl::Encoder.encode(as_json)
120
118
  end
121
119
 
122
- def as_json(*args)
123
- fields_to_h.merge(relationships_to_h).reject { |_, v| v.nil? || v == {} }.as_json(*args)
120
+ def as_json
121
+ fields_to_h.merge(relationships_to_h).reject { |_, v| v.nil? || v == {} }
124
122
  end
125
123
  alias_method :to_h, :as_json
126
124
 
@@ -1,3 +1,3 @@
1
1
  module Lightspeed
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,4 +1,4 @@
1
- require 'json'
1
+ require 'yajl'
2
2
 
3
3
  module Lightspeed
4
4
  end
@@ -19,13 +19,14 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
  spec.license = "MIT"
21
21
 
22
- spec.add_dependency "activesupport", "~> 4.2.0"
22
+ spec.add_dependency "activesupport"
23
+ spec.add_dependency "yajl-ruby"
23
24
 
24
- spec.add_development_dependency "bundler", "~> 1.9"
25
- spec.add_development_dependency "rake", "~> 10.0"
26
- spec.add_development_dependency "rspec", "3.2.0"
27
- spec.add_development_dependency "webmock", "1.21.0"
28
- spec.add_development_dependency "vcr", "2.9.3"
25
+ spec.add_development_dependency "bundler"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "webmock"
29
+ spec.add_development_dependency "vcr"
29
30
  spec.add_development_dependency "rubocop"
30
31
  spec.add_development_dependency "dotenv"
31
32
  spec.add_development_dependency "pry"
@@ -3,9 +3,22 @@
3
3
  set -e
4
4
  set -x
5
5
 
6
+ AWS_ACCOUNT='847437976396'
7
+ REGION='us-west-2'
8
+ REGISTRY="${AWS_ACCOUNT}.dkr.ecr.${REGION}.amazonaws.com"
9
+ REPOSITORY='teg-ci'
10
+ TAG='ci' # change this if you have pushed an image with a new tag
11
+ IMAGE="${REGISTRY}/${REPOSITORY}:${TAG}"
12
+
6
13
  set
7
14
  echo $PWD
8
15
 
9
16
  REAL_CORES=`cat /proc/cpuinfo | grep "core id" | sort | uniq | wc -l`
10
17
 
11
- docker run --rm=true -e "PARALLEL_TEST_PROCESSORS=$REAL_CORES" -v $PWD:/source -v ~/.buildbox/be-bundle-cache:/cache -v /tmp/ramdisk:/tmp/ramdisk -t marketplacer/ci /source/script/docker_tests
18
+ # Pull image if we don't have a locally cached version
19
+ if ! docker images "$IMAGE" | grep --quiet "$IMAGE"; then
20
+ eval $(aws --region "$REGION" ecr get-login)
21
+ docker pull "$IMAGE"
22
+ fi
23
+
24
+ docker run --rm=true -e "PARALLEL_TEST_PROCESSORS=$REAL_CORES" -v $PWD:/source -v ~/.buildbox/be-bundle-cache:/cache -v /tmp/ramdisk:/tmp/ramdisk -t "$IMAGE" /source/script/docker_tests
metadata CHANGED
@@ -1,99 +1,113 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lightspeed_pos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Bigg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-12 00:00:00.000000000 Z
11
+ date: 2017-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.0
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 4.2.0
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yajl-ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - "~>"
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: '1.9'
47
+ version: '0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - "~>"
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: '1.9'
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - "~>"
59
+ - - ">="
46
60
  - !ruby/object:Gem::Version
47
- version: '10.0'
61
+ version: '0'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - "~>"
66
+ - - ">="
53
67
  - !ruby/object:Gem::Version
54
- version: '10.0'
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rspec
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - '='
73
+ - - ">="
60
74
  - !ruby/object:Gem::Version
61
- version: 3.2.0
75
+ version: '0'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - '='
80
+ - - ">="
67
81
  - !ruby/object:Gem::Version
68
- version: 3.2.0
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: webmock
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
- - - '='
87
+ - - ">="
74
88
  - !ruby/object:Gem::Version
75
- version: 1.21.0
89
+ version: '0'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
- - - '='
94
+ - - ">="
81
95
  - !ruby/object:Gem::Version
82
- version: 1.21.0
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: vcr
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
- - - '='
101
+ - - ">="
88
102
  - !ruby/object:Gem::Version
89
- version: 2.9.3
103
+ version: '0'
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
- - - '='
108
+ - - ">="
95
109
  - !ruby/object:Gem::Version
96
- version: 2.9.3
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rubocop
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -174,6 +188,8 @@ files:
174
188
  - lib/lightspeed/items.rb
175
189
  - lib/lightspeed/order.rb
176
190
  - lib/lightspeed/orders.rb
191
+ - lib/lightspeed/price_level.rb
192
+ - lib/lightspeed/price_levels.rb
177
193
  - lib/lightspeed/prices.rb
178
194
  - lib/lightspeed/request.rb
179
195
  - lib/lightspeed/request_throttler.rb
@@ -213,7 +229,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
213
229
  version: '0'
214
230
  requirements: []
215
231
  rubyforge_project:
216
- rubygems_version: 2.4.5
232
+ rubygems_version: 2.6.14
217
233
  signing_key:
218
234
  specification_version: 4
219
235
  summary: A gem for interacting with Lightspeed's Point of Sale system