finale 0.1.6 → 0.1.7

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: aab60ee3c1c411ef73d148bc561a23c3d7d1e2db
4
- data.tar.gz: 372763b00c8f227d42a3a9c5d2c8032637308487
3
+ metadata.gz: 5fa139a0bb80e328411781783d3e9d3cc1eb7bb2
4
+ data.tar.gz: def2cb334d8cd13b655a8131b2e6f2550b73d9ff
5
5
  SHA512:
6
- metadata.gz: b987fb78f477c1c9fc151e35d01ed0f7d8d08f475afe8e41010b1f3d6f053d6f7233df63c3d3f473d361fbd2dc2d53c33b7f99d3768840cf7647aacfcfdff350
7
- data.tar.gz: b24311f5cbbedb43cc41f719458369509b546ed8093af80b64c4effdaaee2ec6816480b20667babb83ad40b6e19fdf63ca5dfd13c1f2239f7459aeac81a5f71e
6
+ metadata.gz: f9fdaa648e335fb494e3e428528cc38e6b68bc350ac40a3e62378cc00aa8d40d27b0153f2d319b3b3201c4535a4aebb73711f3efe670d6a7886935cdd8fd23b3
7
+ data.tar.gz: 69f7458b9a2eec11fe4150301839c8543c68810d7530b95326690b158457f430eb0d2954cd1161bfec86f1d2a5deedfcb15d9eb795daafee8543999b398c353f
@@ -1,111 +1,2 @@
1
- require "rest-client"
2
- require "json"
3
- require "base64"
4
- require "active_support/all"
5
-
6
1
  require "finale/version"
7
- require "finale/errors"
8
- require "finale/order"
9
- require "finale/shipment"
10
-
11
- module Finale
12
- class Client
13
- MAX_REQUESTS = 100 # Finale API Usage: 'https://support.finaleinventory.com/hc/en-us/articles/115007830648-Getting-Started'
14
- BASE_URL = 'https://app.finaleinventory.com'
15
-
16
- def initialize(account)
17
- @cookies = nil
18
- @request_count = 0
19
- @account = account
20
- @login_url = construct_url(:auth)
21
- @order_url = construct_url(:order)
22
- @shipment_url = construct_url(:shipment)
23
- end
24
-
25
- def login(username: nil, password: nil)
26
- payload = {
27
- username: username || ENV['FINALE_USERNAME'],
28
- password: password || ENV['FINALE_PASSWORD']
29
- }
30
-
31
- request(verb: :LOGIN, payload: payload)
32
- end
33
-
34
- def get_order(id)
35
- response = request(verb: :GET, url: "#{@order_url}/#{id}")
36
- Order.new(response)
37
- end
38
-
39
- def get_orders(filter: nil)
40
- resp_orders = request(verb: :GET, url: @order_url, filter: filter )
41
- rows = column_major_to_row_major(resp_orders)
42
- orders = rows.map { |r| Order.new(r) }
43
- orders
44
- end
45
-
46
- def get_shipments(filter: nil)
47
- resp_shipments = request(verb: :GET, url: @shipment_url, filter: filter )
48
- rows = column_major_to_row_major(resp_shipments)
49
- shipments = rows.map { |r| Shipment.new(r) }
50
- shipments
51
- end
52
-
53
- def get_order_from_shipment(shipment)
54
- get_order(shipment.order_id)
55
- end
56
-
57
- def get_shipments_from_order(order)
58
- order.shipmentUrlList.map do |suffix_url|
59
- url = "#{BASE_URL}#{suffix_url}"
60
- response = request(verb: :GET, url: url)
61
- Shipment.new(response)
62
- end
63
- end
64
-
65
- private
66
-
67
- def column_major_to_row_major(column_major)
68
- row_major = []
69
- keys = column_major.keys
70
- values = column_major.values || [[]]
71
- num_cols = values.count == 0 ? 0 : values.first.count
72
- num_cols.times do
73
- rowvals = keys.map { |key| column_major[key].shift }
74
- row = Hash[keys.zip(rowvals)]
75
- row_major << row
76
- end
77
- row_major
78
- end
79
-
80
- def construct_url(resource)
81
- "#{BASE_URL}/#{@account}/api/#{resource}"
82
- end
83
-
84
- def request(verb: nil, url: nil, payload: nil, filter: nil)
85
- raise MaxRequests.new(MAX_REQUESTS) if @request_count >= MAX_REQUESTS
86
- raise NotLoggedIn.new(verb: verb, url: url) unless verb == :LOGIN || !@cookies.nil?
87
-
88
- case verb
89
- when :LOGIN
90
- response = RestClient.post(@login_url, payload)
91
- @cookies = response.cookies
92
- when :GET
93
- params = {}
94
-
95
- if filter.present?
96
- encoded_filter = Base64.encode64(filter.to_json)
97
- params.merge!(filter: encoded_filter)
98
- end
99
-
100
- response = RestClient.get(url, cookies: @cookies, params: params)
101
- when :POST
102
- response = RestClient.post(url, cookies: @cookies)
103
- end
104
-
105
- @request_count += 1
106
- body = JSON.parse(response.body, symbolize_names: true)
107
- body
108
- end
109
- end
110
- end
111
-
2
+ require "finale/client"
@@ -0,0 +1,110 @@
1
+ require "rest-client"
2
+ require "json"
3
+ require "base64"
4
+ require "active_support/all"
5
+
6
+ require_relative"errors"
7
+ require_relative"order"
8
+ require_relative"shipment"
9
+
10
+ module Finale
11
+ class Client
12
+ MAX_REQUESTS = 100 # Finale API Usage: 'https://support.finaleinventory.com/hc/en-us/articles/115007830648-Getting-Started'
13
+ BASE_URL = 'https://app.finaleinventory.com'
14
+
15
+ def initialize(account)
16
+ @cookies = nil
17
+ @request_count = 0
18
+ @account = account
19
+ @login_url = construct_url(:auth)
20
+ @order_url = construct_url(:order)
21
+ @shipment_url = construct_url(:shipment)
22
+ end
23
+
24
+ def login(username: nil, password: nil)
25
+ payload = {
26
+ username: username || ENV['FINALE_USERNAME'],
27
+ password: password || ENV['FINALE_PASSWORD']
28
+ }
29
+
30
+ request(verb: :LOGIN, payload: payload)
31
+ end
32
+
33
+ def get_order(id)
34
+ response = request(verb: :GET, url: "#{@order_url}/#{id}")
35
+ Order.new(response)
36
+ end
37
+
38
+ def get_orders(filter: nil)
39
+ resp_orders = request(verb: :GET, url: @order_url, filter: filter )
40
+ rows = column_major_to_row_major(resp_orders)
41
+ orders = rows.map { |r| Order.new(r) }
42
+ orders
43
+ end
44
+
45
+ def get_shipments(filter: nil)
46
+ resp_shipments = request(verb: :GET, url: @shipment_url, filter: filter )
47
+ rows = column_major_to_row_major(resp_shipments)
48
+ shipments = rows.map { |r| Shipment.new(r) }
49
+ shipments
50
+ end
51
+
52
+ def get_order_from_shipment(shipment)
53
+ get_order(shipment.order_id)
54
+ end
55
+
56
+ def get_shipments_from_order(order)
57
+ order.shipmentUrlList.map do |suffix_url|
58
+ url = "#{BASE_URL}#{suffix_url}"
59
+ response = request(verb: :GET, url: url)
60
+ Shipment.new(response)
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def column_major_to_row_major(column_major)
67
+ row_major = []
68
+ keys = column_major.keys
69
+ values = column_major.values || [[]]
70
+ num_cols = values.count == 0 ? 0 : values.first.count
71
+ num_cols.times do
72
+ rowvals = keys.map { |key| column_major[key].shift }
73
+ row = Hash[keys.zip(rowvals)]
74
+ row_major << row
75
+ end
76
+ row_major
77
+ end
78
+
79
+ def construct_url(resource)
80
+ "#{BASE_URL}/#{@account}/api/#{resource}"
81
+ end
82
+
83
+ def request(verb: nil, url: nil, payload: nil, filter: nil)
84
+ raise MaxRequests.new(MAX_REQUESTS) if @request_count >= MAX_REQUESTS
85
+ raise NotLoggedIn.new(verb: verb, url: url) unless verb == :LOGIN || !@cookies.nil?
86
+
87
+ case verb
88
+ when :LOGIN
89
+ response = RestClient.post(@login_url, payload)
90
+ @cookies = response.cookies
91
+ when :GET
92
+ params = {}
93
+
94
+ if filter.present?
95
+ encoded_filter = Base64.encode64(filter.to_json)
96
+ params.merge!(filter: encoded_filter)
97
+ end
98
+
99
+ response = RestClient.get(url, cookies: @cookies, params: params)
100
+ when :POST
101
+ response = RestClient.post(url, cookies: @cookies)
102
+ end
103
+
104
+ @request_count += 1
105
+ body = JSON.parse(response.body, symbolize_names: true)
106
+ body
107
+ end
108
+ end
109
+ end
110
+
@@ -1,7 +1,7 @@
1
1
  module Finale
2
2
  class Shipment < OpenStruct
3
3
  def lot_ids
4
- shipmentItemList.map { |item| item[:lotId] }.compact
4
+ (shipmentItemList || []).map { |item| item[:lotId] }.compact
5
5
  end
6
6
 
7
7
  def order_id
@@ -1,3 +1,3 @@
1
1
  module Finale
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finale
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Poppler
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-29 00:00:00.000000000 Z
11
+ date: 2018-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -159,6 +159,7 @@ extra_rdoc_files: []
159
159
  files:
160
160
  - README.md
161
161
  - lib/finale.rb
162
+ - lib/finale/client.rb
162
163
  - lib/finale/errors.rb
163
164
  - lib/finale/order.rb
164
165
  - lib/finale/shipment.rb