gentle 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/Gemfile +8 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +64 -0
  6. data/Rakefile +9 -0
  7. data/gentle.gemspec +29 -0
  8. data/lib/gentle.rb +8 -0
  9. data/lib/gentle/blackboard.rb +44 -0
  10. data/lib/gentle/client.rb +92 -0
  11. data/lib/gentle/documents/document.rb +15 -0
  12. data/lib/gentle/documents/request/hash_converter.rb +55 -0
  13. data/lib/gentle/documents/request/shipment_order.rb +167 -0
  14. data/lib/gentle/documents/response/shipment_order_result.rb +56 -0
  15. data/lib/gentle/error_message.rb +19 -0
  16. data/lib/gentle/message.rb +43 -0
  17. data/lib/gentle/phase_1_set.rb +53 -0
  18. data/lib/gentle/queue.rb +40 -0
  19. data/lib/gentle/request.rb +11 -0
  20. data/lib/gentle/response.rb +11 -0
  21. data/lib/gentle/version.rb +3 -0
  22. data/spec/fixtures/credentials.yml +12 -0
  23. data/spec/fixtures/documents/responses/shipment_order_result.xml +23 -0
  24. data/spec/fixtures/messages/error_message.xml +10 -0
  25. data/spec/fixtures/messages/purchase_order_message.xml +11 -0
  26. data/spec/remote/blackboard_spec.rb +43 -0
  27. data/spec/remote/queue_spec.rb +64 -0
  28. data/spec/spec_helper.rb +323 -0
  29. data/spec/unit/blackboard_spec.rb +102 -0
  30. data/spec/unit/client_spec.rb +97 -0
  31. data/spec/unit/documents/document_spec.rb +44 -0
  32. data/spec/unit/documents/request/hash_converter_spec.rb +42 -0
  33. data/spec/unit/documents/request/shipment_order_spec.rb +164 -0
  34. data/spec/unit/documents/response/shipment_order_result_spec.rb +26 -0
  35. data/spec/unit/error_message_spec.rb +31 -0
  36. data/spec/unit/message_spec.rb +71 -0
  37. data/spec/unit/phase_1_set_spec.rb +76 -0
  38. data/spec/unit/queue_spec.rb +57 -0
  39. metadata +196 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 918ee2ec0b03488dd47d7ddff5c409f213c04773
4
+ data.tar.gz: fb0f8a52ab418098695258d41835330414b2ec63
5
+ SHA512:
6
+ metadata.gz: 38aa3474fa3490a513fae74f594001836865a0735aa359d7fe7c18592c2348a4136b71abee70d75b313ae1cc467e5d5b50c94ca7acf94efa087992a31d42108b
7
+ data.tar.gz: f08640f2c2e6af32910c1bdc6d5cafbe72e1ab2dbe2a285c7da5b78831070c11c0cf7597b94bb95e1244196596701a837d3c7ff723d28f24b4acb11d9dc52316
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .byebug_history
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gentle.gemspec
4
+ group :test do
5
+ gem 'pry-byebug'
6
+ gem 'm', github: 'qrush/m'
7
+ end
8
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 DynamoMTL
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # Gentle
2
+
3
+ Client library for integrating with the [Quiet Logistics](http://quietlogistics.com) API
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'gentle'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gentle
18
+
19
+ ## Usage
20
+
21
+ Basic usage for Gentle goes as follows:
22
+
23
+
24
+ ```ruby
25
+ require 'gentle'
26
+ require 'gentle/documents/request/shipment_order'
27
+
28
+ credentials = {
29
+ access_key_id: 'AWS_ACCESS_KEY', secret_access_key: 'SECRET_ACCESS_KEY',
30
+ client_id: 'QUIET CLIENT ID', business_unit: 'QUIET BUSINESS UNIT',
31
+ warehouse: 'QUIET WAREHOUSE',
32
+ buckets: {
33
+ to: 'gentle-to-quiet',
34
+ from: 'gentle-from-quiet'
35
+ },
36
+ queues: {
37
+ to: 'http://queue.amazonaws/1234567890/gentle_to_quiet'
38
+ from: 'http://queue.amazonaws/1234567890/gentle_from_quiet'
39
+ inventory: 'http://queue.amazonaws/1234567890/gentle_inventory'
40
+ }
41
+ }
42
+ client = Gentle::Client.new(credentials)
43
+ # Orders are expected to have similar attributes as Spree/Solidus::Shipment
44
+ document = Gentle::Documents::Request::ShipmentOrder.new(client: client, shipment: shipment)
45
+
46
+ blackboard = Gentle::Blackboard.new(client)
47
+ queue = Gentle::Queue.new(client)
48
+
49
+ message = blackboard.post(document)
50
+ queue.send(message)
51
+
52
+ response_message = queue.receive
53
+ response_document = blackboard.fetch(message)
54
+ process_document(response_document)
55
+ blackboard.remove(message)
56
+ ```
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs = ['lib', 'spec']
6
+ t.ruby_opts << '-rubygems'
7
+ t.verbose = true
8
+ t.test_files = FileList['spec/**/*_spec.rb']
9
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gentle/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gentle"
8
+ spec.version = Gentle::VERSION
9
+ spec.authors = ["DynamoMTL"]
10
+ spec.email = [""]
11
+ spec.description = "API Client for Quiet Logistics Services"
12
+ spec.summary = "Integrates with QL Blackboard and work Queue"
13
+ spec.homepage = "http://github.com/DynamoMTL/qls"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency('aws-sdk', '< 2.0')
22
+ spec.add_dependency('nokogiri')
23
+ spec.add_dependency('activesupport')
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "minitest", ">= 5.0.0"
28
+ spec.add_development_dependency "mocha"
29
+ end
@@ -0,0 +1,8 @@
1
+ require "nokogiri"
2
+ require "gentle/documents/document"
3
+ require "gentle/version"
4
+ require "gentle/client"
5
+ require "gentle/blackboard"
6
+ require "gentle/queue"
7
+ require "gentle/message"
8
+ require "gentle/error_message"
@@ -0,0 +1,44 @@
1
+ require 'gentle/response'
2
+ require 'gentle/request'
3
+ module Gentle
4
+ class Blackboard
5
+ attr_reader :client
6
+
7
+ def initialize(client)
8
+ @client = client
9
+ end
10
+
11
+ def post(document)
12
+ bucket = client.to_quiet_bucket
13
+ if bucket.objects[document.filename].write(document.to_xml)
14
+ Message.new(:client => @client, :document => document)
15
+ end
16
+ end
17
+
18
+ def fetch(message)
19
+ bucket = client.from_quiet_bucket
20
+ contents = bucket.objects[message.document_name].read
21
+ build_document(message.document_type, contents)
22
+ end
23
+
24
+ def remove(message)
25
+ bucket = client.from_quiet_bucket
26
+ object = bucket.objects[message.document_name]
27
+ if object.exists?
28
+ object.delete
29
+ true
30
+ else
31
+ false
32
+ end
33
+ end
34
+
35
+ def build_document(type, contents)
36
+ namespace = if Response.valid_type?(type)
37
+ Documents::Response
38
+ elsif Request.valid_type?(type)
39
+ Documents::Request
40
+ end
41
+ namespace.const_get(type).new(:io => contents) if namespace
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,92 @@
1
+ require 'aws/s3'
2
+ require 'aws/sqs'
3
+ require 'active_support/core_ext/hash/slice'
4
+ require 'active_support/core_ext/hash/indifferent_access'
5
+
6
+ module Gentle
7
+ class Client
8
+
9
+ class InitializationError < StandardError; end
10
+ class InvalidBucketError < StandardError; end
11
+ class InvalidQueueError < StandardError; end
12
+
13
+ attr_reader :client_id, :business_unit, :warehouse
14
+
15
+ def initialize(options = {})
16
+ options = options.with_indifferent_access
17
+ @buckets = options[:buckets]
18
+ @queues = options[:queues]
19
+ @client_id = options[:client_id]
20
+ @warehouse = options[:warehouse]
21
+ @business_unit = options[:business_unit]
22
+ @credentials = options.slice(:access_key_id, :secret_access_key)
23
+ verify!
24
+ end
25
+
26
+ def to_quiet_bucket
27
+ @to_quiet_bucket ||= fetch_bucket(@buckets[:to])
28
+ end
29
+
30
+ def from_quiet_bucket
31
+ @from_quiet_bucket ||= fetch_bucket(@buckets[:from])
32
+ end
33
+
34
+ def to_quiet_queue
35
+ @to_quiet_queue ||= fetch_queue(@queues[:to])
36
+ end
37
+
38
+ def from_quiet_queue
39
+ @from_quiet_queue ||= fetch_queue(@queues[:from])
40
+ end
41
+
42
+ def quiet_inventory_queue
43
+ @quiet_inventory_queue ||= fetch_queue(@queues[:inventory])
44
+ end
45
+
46
+ private
47
+ def sqs_client
48
+ @sqs_client ||= AWS::SQS.new(@credentials)
49
+ end
50
+
51
+ def s3_client
52
+ @s3_client ||= AWS::S3.new(@credentials)
53
+ end
54
+
55
+ def fetch_bucket(name)
56
+ bucket = s3_client.buckets[name]
57
+ raise(InvalidBucketError.new("#{name} is not a valid bucket")) unless bucket.exists?
58
+ bucket
59
+ end
60
+
61
+ def fetch_queue(url)
62
+ queue = sqs_client.queues[url]
63
+ raise(InvalidQueueError.new("#{url} is not a valid queue")) unless queue.exists?
64
+ queue
65
+ end
66
+
67
+ def verify!
68
+ raise(InitializationError.new("Credentials cannot be missing")) unless all_credentials?
69
+ raise(InitializationError.new("Both to and from buckets need to be set")) unless all_buckets?
70
+ raise(InitializationError.new("To, From and Inventory queues need to be set")) unless all_queues?
71
+ raise(InitializationError.new("client_id needs to be set")) unless @client_id
72
+ raise(InitializationError.new("business_unit needs to be set")) unless @business_unit
73
+ raise(InitializationError.new("warehouse needs to be set")) unless @warehouse
74
+ end
75
+
76
+ def all_credentials?
77
+ has_keys?(@credentials, [:access_key_id, :secret_access_key])
78
+ end
79
+
80
+ def all_buckets?
81
+ has_keys?(@buckets, [:from, :to])
82
+ end
83
+
84
+ def all_queues?
85
+ has_keys?(@queues, [:from, :to, :inventory])
86
+ end
87
+
88
+ def has_keys?(hash, keys)
89
+ keys.reduce(hash) {|result, key| result && hash[key]}
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,15 @@
1
+ module Gentle
2
+ module Documents
3
+ module Document
4
+ DATEFORMAT = "%Y%m%d_%H%M%S"
5
+
6
+ def to_xml
7
+ raise NotImplementedError("To be implemented by subclasses")
8
+ end
9
+
10
+ def filename
11
+ @filename ||= "#{business_unit}_#{type}_#{document_number}_#{date.strftime(DATEFORMAT)}.xml"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,55 @@
1
+ module Gentle
2
+ module Documents
3
+ module Request
4
+ module HashConverter
5
+ def part_line_item_hash(part)
6
+ {
7
+ 'ItemNumber' => part.variant.sku,
8
+ 'Line' => part.variant.id,
9
+ 'QuantityOrdered' => part.line_item.quantity,
10
+ 'QuantityToShip' => part.line_item.quantity,
11
+ 'UOM' => 'EA',
12
+ 'Price' => part.variant.price
13
+ }
14
+ end
15
+
16
+ def line_item_hash(item)
17
+ {
18
+ 'ItemNumber' => item.sku,
19
+ 'Line' => item.id,
20
+ 'QuantityOrdered' => item.quantity,
21
+ 'QuantityToShip' => item.quantity,
22
+ 'UOM' => 'EA',
23
+ 'Price' => item.price
24
+ }
25
+ end
26
+
27
+ def ship_to_hash
28
+ {
29
+ 'Company' => ship_address.company,
30
+ 'Contact' => full_name,
31
+ 'Address1' => ship_address.address1,
32
+ 'Address2' => ship_address.address2,
33
+ 'City' => ship_address.city,
34
+ 'State' => ship_address.state.name,
35
+ 'PostalCode' => ship_address.zipcode,
36
+ 'Country' => ship_address.country.name
37
+ }
38
+ end
39
+
40
+ def bill_to_hash
41
+ {
42
+ 'Company' => bill_address.company,
43
+ 'Contact' => full_name,
44
+ 'Address1' => bill_address.address1,
45
+ 'Address2' => bill_address.address2,
46
+ 'City' => bill_address.city,
47
+ 'State' => bill_address.state.name,
48
+ 'PostalCode' => bill_address.zipcode,
49
+ 'Country' => bill_address.country.name
50
+ }
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,167 @@
1
+ require 'forwardable'
2
+ require 'gentle/documents/request/hash_converter'
3
+ require 'gentle/phase_1_set'
4
+
5
+ module Gentle
6
+ module Documents
7
+ module Request
8
+ class ShipmentOrder
9
+ include Gentle::Documents::Document
10
+ include Gentle::Documents::Request::HashConverter
11
+ extend Forwardable
12
+
13
+ NAMESPACE = "http://schemas.quietlogistics.com/V2/ShipmentOrder.xsd"
14
+
15
+ DATEFORMAT = "%Y%m%d_%H%M%S"
16
+
17
+ class MissingOrderError < StandardError; end
18
+ class MissingClientError < StandardError; end
19
+
20
+ attr_reader :shipment, :client
21
+
22
+ def_delegators :@client, :warehouse, :business_unit, :client_id
23
+
24
+ def initialize(options = {})
25
+ @client = options.fetch(:client)
26
+ @shipment = options.fetch(:shipment)
27
+ @shipment_number = @shipment.number
28
+ end
29
+
30
+ def to_xml
31
+ builder = Nokogiri::XML::Builder.new do |xml|
32
+ xml.ShipOrderDocument('xmlns' => 'http://schemas.quietlogistics.com/V2/ShipmentOrder.xsd') {
33
+
34
+ xml.ClientID client_id
35
+ xml.BusinessUnit business_unit
36
+
37
+ xml.OrderHeader('OrderNumber' => @shipment_number,
38
+ 'OrderType' => order_type,
39
+ 'OrderDate' => @shipment.created_at.utc.iso8601) {
40
+
41
+ xml.Extension @shipment.order.number
42
+
43
+ xml.Comments @shipment.order.special_instructions
44
+
45
+ xml.ShipMode('Carrier' => @shipment.shipping_method.carrier,
46
+ 'ServiceLevel' => @shipment.shipping_method.service_level)
47
+
48
+ xml.ShipTo(ship_to_hash)
49
+ xml.BillTo(bill_to_hash)
50
+
51
+ if @shipment.respond_to?(:value_added_services)
52
+ @shipment.value_added_services.each do |service|
53
+ xml.ValueAddedService('Service' => service[:service],
54
+ 'ServiceType' => service[:service_type])
55
+ end
56
+ end
57
+ }
58
+
59
+ add_items_to_shipment_order(order_items, xml)
60
+ }
61
+ end
62
+ builder.to_xml
63
+ end
64
+
65
+ def add_items_to_shipment_order(items, xml)
66
+ item_hashes = convert_to_hashes(items)
67
+ grouped_items = group_items_by_sku(item_hashes)
68
+ grouped_items.each_with_index do |hash, index|
69
+ hash['Line'] = index + 1
70
+ xml.OrderDetails(hash)
71
+ end
72
+ end
73
+
74
+ def convert_to_hashes(items)
75
+ items.map do |item|
76
+ if Phase1Set.match(item)
77
+ add_individual_phase_1_items(item)
78
+ elsif contain_parts? item
79
+ add_item_parts(item.part_line_items)
80
+ else
81
+ line_item_hash(item)
82
+ end
83
+ end.flatten
84
+ end
85
+
86
+ def group_items_by_sku(item_hashes)
87
+ grouped = item_hashes.group_by {|hash| hash['ItemNumber'] }
88
+ grouped.values.map do |hashes|
89
+ hash = hashes.first
90
+ update_quantity(hash, total_quantity(hashes))
91
+ end
92
+ end
93
+
94
+ def contain_parts?(item)
95
+ item.part_line_items && !item.part_line_items.empty?
96
+ end
97
+
98
+ def add_item_parts(part_line_items)
99
+ part_line_items.map { |part| part_line_item_hash(part) }
100
+ end
101
+
102
+ def add_individual_phase_1_items(phase_1_item)
103
+ phase_1 = Phase1Set.new(phase_1_item).included_items
104
+ phase_1.map { |item| line_item_hash(item) }
105
+ end
106
+
107
+ def update_quantity(hash, quantity)
108
+ hash['QuantityOrdered'] = hash['QuantityToShip'] = quantity
109
+ hash
110
+ end
111
+
112
+ def total_quantity(hashes)
113
+ hashes.map{ |hash| hash['QuantityOrdered'] }.reduce(:+)
114
+ end
115
+
116
+ def order_items
117
+ @shipment.order.line_items
118
+ end
119
+
120
+ def order_type
121
+ 'SO'
122
+ end
123
+
124
+ def ship_address
125
+ @shipment.address
126
+ end
127
+
128
+ def bill_address
129
+ @shipment.order.bill_address
130
+ end
131
+
132
+ def full_name
133
+ @shipment.address.full_name
134
+ end
135
+
136
+ def message
137
+ "Succesfully Sent Shipment #{@shipment_number} to Quiet Logistics"
138
+ end
139
+
140
+ def date_stamp
141
+ Time.now.strftime('%Y%m%d_%H%M%3N')
142
+ end
143
+
144
+ def type
145
+ 'ShipmentOrder'
146
+ end
147
+
148
+ def document_number
149
+ @shipment_number
150
+ end
151
+
152
+ def date
153
+ @shipment.created_at
154
+ end
155
+
156
+ def filename
157
+ "#{business_unit}_#{type}_#{document_number}_#{date.strftime(DATEFORMAT)}.xml"
158
+ end
159
+
160
+ def message_id
161
+ SecureRandom.uuid
162
+ end
163
+
164
+ end
165
+ end
166
+ end
167
+ end