omniorder 0.0.3 → 0.0.4

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: d7e7efc2ebd649ff319459de8a0760bcd6331001
4
- data.tar.gz: 711d3f5d76e2650a6a499926b0be94bd31f346ee
3
+ metadata.gz: 08acaaeabef190021f9adb0423a59faec3783c73
4
+ data.tar.gz: 8e77e461b790a6bb3ecbe8e133551eb5e3075c07
5
5
  SHA512:
6
- metadata.gz: 29329c7d41987a63e67eda25e248ec7655216f0d4dfbf65660b91b1af5d5f09084395247547f9f866272a7395e8d1483650fb2e0cb44f4611624071b5f026fbd
7
- data.tar.gz: 501165b13166f32f1b514a839fe4cc43e17693f2b94096c92f79add4b78e09edee7133a8f227043168b711929aa9f9a7160c32d9d6d0518b32d5d8f40816c6f8
6
+ metadata.gz: a2ee79b57210e4fedc5966a7ffd2b7d5cb3cde9db21bd37c456dadcdbca22c83ef93d7ed7650b9fb1cc9d4810f1f5831e4660c0110e2e805544ce3fa03c7367d
7
+ data.tar.gz: b8d070076a5600e7f03a9ea79737644c9e7808e7e0c3d79c53e617502b86aad411e232a1087e015f4997549bed21ef76c34957ba0574c8dd30da18c143a59dcc
@@ -3,6 +3,8 @@ module Omniorder
3
3
  # Groupon Import Strategy
4
4
  # See: https://scm.commerceinterface.com/api-doc/v2/
5
5
  class Groupon < Base
6
+ require 'json'
7
+
6
8
  API_URL = "https://scm.commerceinterface.com/api/v2/"
7
9
 
8
10
  attr_accessor :supplier_id, :access_token
@@ -33,6 +35,35 @@ module Omniorder
33
35
  end
34
36
  end
35
37
 
38
+ def update_order_tracking!(orders)
39
+ orders = [*orders].select do |order|
40
+ if order.respond_to?(:shipping_reference) && !order.shipping_reference.nil?
41
+ if order.external_carrier_reference.nil? || order.external_carrier_reference == ''
42
+ raise "Cannot send tracking info for Groupon order ##{order.order_number} since it has no external_carrier_reference"
43
+ end
44
+
45
+ if order.order_products.any? { |op| op.external_reference.to_i == 0 }
46
+ raise "Cannot send tracking info for Groupon order ##{order.order_number} since a line item has no external_reference"
47
+ end
48
+
49
+ true
50
+ end
51
+ end
52
+
53
+ unless orders.empty?
54
+ result = Crack::JSON.parse do_request(tracking_notification_url(orders), :post)
55
+ end
56
+
57
+ if result && result['success'].nil?
58
+ # Include order number too if there is only one order
59
+ if orders.count == 1
60
+ raise "Failed to update Groupon tracking data for order ##{orders.first.order_number} (#{result['reason']})"
61
+ else
62
+ raise "Failed to update Groupon tracking data (#{result['reason']})"
63
+ end
64
+ end
65
+ end
66
+
36
67
  def get_orders_url
37
68
  URI.join(API_URL, "get_orders?supplier_id=#{supplier_id}&token=#{access_token}")
38
69
  end
@@ -42,6 +73,20 @@ module Omniorder
42
73
  URI.join(API_URL, "mark_exported?supplier_id=#{supplier_id}&token=#{access_token}&ci_lineitem_ids=[#{lids.join(',')}]")
43
74
  end
44
75
 
76
+ def tracking_notification_url(orders)
77
+ tracking_info = orders.map do |order|
78
+ order.order_products.map do |line_item|
79
+ {
80
+ "carrier" => order.external_carrier_reference,
81
+ "ci_lineitem_id" => line_item.external_reference.to_i,
82
+ "tracking" => order.shipping_reference
83
+ }
84
+ end
85
+ end.flatten
86
+
87
+ File.join(API_URL, "tracking_notification?supplier_id=#{supplier_id}&token=#{access_token}&tracking_info=#{tracking_info.to_json}")
88
+ end
89
+
45
90
  private
46
91
 
47
92
  def create_order(order_info)
@@ -54,7 +99,11 @@ module Omniorder
54
99
  order.customer = create_customer(order, order_info['customer'])
55
100
 
56
101
  order_info['line_items'].each do |line_item_info|
57
- order.add_product_by_code(line_item_info['sku'].to_s, line_item_info['quantity'].to_i)
102
+ order.add_product_by_code(
103
+ line_item_info['sku'].to_s,
104
+ line_item_info['quantity'].to_i,
105
+ line_item_info['ci_lineitemid'].to_i
106
+ )
58
107
  end
59
108
 
60
109
  after_build_order order, order_info
@@ -82,7 +131,8 @@ module Omniorder
82
131
  private
83
132
 
84
133
  def do_request(url, type = :get)
85
- uri = URI(url)
134
+ host_and_path, query = url.to_s.split('?')
135
+ uri = URI(type == :get ? url : host_and_path)
86
136
  http = Net::HTTP.new(uri.host, 443)
87
137
  http.use_ssl = true
88
138
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
@@ -91,8 +141,8 @@ module Omniorder
91
141
  if type == :get
92
142
  http.request(Net::HTTP::Get.new(uri.request_uri)).body
93
143
  else
94
- request = Net::HTTP::Post.new(uri.path)
95
- request.body = uri.query
144
+ request = Net::HTTP::Post.new(host_and_path)
145
+ request.body = query
96
146
  http.request(request).body
97
147
  end
98
148
  end
@@ -2,7 +2,8 @@ module Omniorder
2
2
  class Order < Entity
3
3
  include Orderable
4
4
 
5
- attributes :customer, :order_products, :order_number, :total_price, :date
5
+ attributes :customer, :order_products, :order_number, :total_price, :date, :shipping_reference,
6
+ :external_carrier_reference
6
7
 
7
8
  def initialize(attributes = {})
8
9
  super
@@ -2,6 +2,6 @@ module Omniorder
2
2
  class OrderProduct < Entity
3
3
  include OrderProductable
4
4
 
5
- attributes :product, :quantity
5
+ attributes :product, :quantity, :external_reference
6
6
  end
7
7
  end
@@ -7,19 +7,23 @@ module Omniorder
7
7
  Omniorder.customer_type.new(attributes)
8
8
  end
9
9
 
10
- def add_product(product, quantity = 1)
10
+ def add_product(product, quantity = 1, external_reference = nil)
11
11
  order_product = order_products.to_a.find { |op| op.product == product }
12
12
 
13
13
  if order_product.nil?
14
- order_products << Omniorder.order_product_type.new(:product => product, :quantity => quantity)
14
+ order_products << Omniorder.order_product_type.new(
15
+ :product => product,
16
+ :quantity => quantity,
17
+ :external_reference => external_reference
18
+ )
15
19
  else
16
20
  order_product.quantity += quantity
17
21
  end
18
22
  end
19
23
 
20
- def add_product_by_code(code, quantity = 1)
24
+ def add_product_by_code(code, quantity = 1, external_reference = nil)
21
25
  Omniorder.product_type.find_by_code(code).tap do |product|
22
- add_product product, quantity unless product.nil?
26
+ add_product product, quantity, external_reference unless product.nil?
23
27
  end
24
28
  end
25
29
 
@@ -1,3 +1,3 @@
1
1
  module Omniorder
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -44,6 +44,7 @@ describe Omniorder::ImportStrategy::Groupon do
44
44
  order_product = order.order_products.first
45
45
  expect(order_product.quantity).to eq(3)
46
46
  expect(order_product.product.code).to eq('03658246')
47
+ expect(order_product.external_reference).to eq(54553918)
47
48
 
48
49
  customer = order.customer
49
50
  expect(customer.name).to eq("SOME BODY HERE")
@@ -87,4 +88,85 @@ describe Omniorder::ImportStrategy::Groupon do
87
88
  end
88
89
  end
89
90
  end
91
+
92
+ describe '#update_order_tracking!' do
93
+ let(:tracking_notification_result) { '{ "success": true }' }
94
+ let!(:tracking_notification_stub) do
95
+ stub_request(
96
+ :post,
97
+ "https://scm.commerceinterface.com/api/v2/tracking_notification"
98
+ ).with(
99
+ body: "supplier_id=1&token=xYRPKcoakMoiRzWgKLV5TqPSdNAaZQT&tracking_info=[{\"carrier\":\"4SL\",\"ci_lineitem_id\":54553918,\"tracking\":\"SR123451\"},{\"carrier\":\"DHL\",\"ci_lineitem_id\":54553920,\"tracking\":\"SR123452\"}]"
100
+ ).to_return(
101
+ body: tracking_notification_result,
102
+ status: 200
103
+ )
104
+ end
105
+
106
+ it 'updates Groupon tracking information from orders' do
107
+ orders = [
108
+ Omniorder::Order.new(
109
+ :shipping_reference => 'SR123451',
110
+ :external_carrier_reference => '4SL',
111
+ :order_products => [Omniorder::OrderProduct.new(:external_reference => '54553918')]
112
+ ),
113
+ Omniorder::Order.new(
114
+ :shipping_reference => 'SR123452',
115
+ :external_carrier_reference => 'DHL',
116
+ :order_products => [Omniorder::OrderProduct.new(:external_reference => '54553920')]
117
+ )
118
+ ]
119
+
120
+ strategy.update_order_tracking!(orders)
121
+ expect(tracking_notification_stub).to have_been_requested.once
122
+ end
123
+
124
+ it 'does nothing when an order has no shipping_reference' do
125
+ order = Omniorder::Order.new(
126
+ :order_number => 'ORD1',
127
+ :external_carrier_reference => '4SL',
128
+ :order_products => [Omniorder::OrderProduct.new(:external_reference => '54553918')]
129
+ )
130
+
131
+ # Exception would be raised on request as single-order stub does not exist
132
+ strategy.update_order_tracking!(order)
133
+ end
134
+
135
+ it 'raises an exception when an order has no external_carrier_reference' do
136
+ orders = [
137
+ Omniorder::Order.new(
138
+ :order_number => 'ORD1',
139
+ :shipping_reference => 'SR123451',
140
+ :order_products => [Omniorder::OrderProduct.new(:external_reference => '54553918')]
141
+ ),
142
+ Omniorder::Order.new(
143
+ :shipping_reference => 'SR123452',
144
+ :external_carrier_reference => 'DHL',
145
+ :order_products => [Omniorder::OrderProduct.new(:external_reference => '54553920')]
146
+ )
147
+ ]
148
+
149
+ expect { strategy.update_order_tracking!(orders) }.
150
+ to raise_exception "Cannot send tracking info for Groupon order #ORD1 since it has no external_carrier_reference"
151
+ end
152
+
153
+ it 'raises an exception when and order line item has no external_reference' do
154
+ orders = [
155
+ Omniorder::Order.new(
156
+ :order_number => 'ORD1',
157
+ :shipping_reference => 'SR123451',
158
+ :external_carrier_reference => '4SL',
159
+ :order_products => [Omniorder::OrderProduct.new]
160
+ ),
161
+ Omniorder::Order.new(
162
+ :shipping_reference => 'SR123452',
163
+ :external_carrier_reference => 'DHL',
164
+ :order_products => [Omniorder::OrderProduct.new(:external_reference => '54553920')]
165
+ )
166
+ ]
167
+
168
+ expect { strategy.update_order_tracking!(orders) }.
169
+ to raise_exception "Cannot send tracking info for Groupon order #ORD1 since a line item has no external_reference"
170
+ end
171
+ end
90
172
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniorder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Howard Wilson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-27 00:00:00.000000000 Z
11
+ date: 2014-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: crack