smartsend-ruby 0.1.1 → 0.2.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
- SHA1:
3
- metadata.gz: 71c53eea4d23cd5fdbb44d988a4aa80d1b971ddb
4
- data.tar.gz: e370c38f2f6c95faedc889116356278e835b405a
2
+ SHA256:
3
+ metadata.gz: 5e338e7c9a7df4d21d802d3add16f25ff4f9215b01e251cac1f0cf0f6895325c
4
+ data.tar.gz: 694b44590d3806a8b4be161adbfcfd40bf32489325cb09d913a7b1974fa4bec7
5
5
  SHA512:
6
- metadata.gz: b2baf4fd35f72dc9cf5e4773aaaebc74d5d52597e1a373f7c9aebc6f241e4078baed5e256421d854c81dc62b6b403e091e109c55132351323fdca95b62bcb0e1
7
- data.tar.gz: 768aa3279001727ec340bc0dd162d46c1e3c9c76253fb7bfc2e5d186542e145bbebdecf55b701b32fd5d8867c78f77ef66452c4efcd09befc72fb897989999fd
6
+ metadata.gz: 0ae5e3227403d982d6a9f590af15cd654d93d0b10627ff301d3315ce13d666b19b8fd7123a6e4fd9751c3fe06df7d1ea95d9eb7a1d98617df8b078c025dd94c7
7
+ data.tar.gz: b4aa12989d2d520920a39e3b0c6d8b1a0485991f8e621dcfadf3cc06b0901cf057aa8d3c94b172733cfa4dbb4f2545cceb849ed6911e4b0d61126624d1401b02
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- smartsend-ruby (0.0.1)
4
+ smartsend-ruby (0.1.1)
5
5
  http (~> 2.2)
6
6
 
7
7
  GEM
@@ -9,6 +9,7 @@ GEM
9
9
  specs:
10
10
  addressable (2.5.2)
11
11
  public_suffix (>= 2.0.2, < 4.0)
12
+ coderay (1.1.2)
12
13
  domain_name (0.5.20170404)
13
14
  unf (>= 0.0.5, < 1.0.0)
14
15
  http (2.2.2)
@@ -20,6 +21,10 @@ GEM
20
21
  domain_name (~> 0.5)
21
22
  http-form_data (1.0.3)
22
23
  http_parser.rb (0.6.0)
24
+ method_source (0.9.0)
25
+ pry (0.11.3)
26
+ coderay (~> 1.1.0)
27
+ method_source (~> 0.9.0)
23
28
  public_suffix (3.0.1)
24
29
  unf (0.1.4)
25
30
  unf_ext
@@ -29,7 +34,8 @@ PLATFORMS
29
34
  ruby
30
35
 
31
36
  DEPENDENCIES
37
+ pry
32
38
  smartsend-ruby!
33
39
 
34
40
  BUNDLED WITH
35
- 1.14.6
41
+ 1.16.1
data/README.md CHANGED
@@ -15,67 +15,60 @@ gem install 'smartsend-ruby'
15
15
 
16
16
  ```ruby
17
17
  Smartsend.configure(
18
- api_key: 'smartsend-api-key',
19
- email: 'smartsend-username',
20
- license: 'smartsend-license-key',
21
- cms_system: 'Ruby on Rails',
22
- cms_version: '5.0.0'
18
+ api_token: 'smart-send-api-token'
23
19
  )
24
20
  ```
25
21
 
26
22
  ## Usage
27
23
 
28
- ### Creating an order
24
+ ### Creating a shipment
29
25
 
30
26
  This creates a label.
31
27
 
32
28
  ```ruby
33
- # initalize a new order
34
- order = Smartsend::Order.new(
35
- id: "123123123", # your internal order id
36
- order_number: "AC12345789",
37
- carrier: "postnord", # postnord/gls/bring
38
- method: "private",
39
- return: false,
40
- total_price: 199.75,
41
- shipping_price: 49,
42
- currency: "DKK",
43
- sms_notification: "12345678",
44
- email_notification: "contact@smartsend.io"
29
+ # initalize a new shipment
30
+ shipment = Smartsend::Shipment.new(
31
+ internal_id: "123123123", # your internal order id
32
+ internal_reference: "AC12345789", # your order number
33
+ shipping_carrier: "postnord", # postnord/gls/bring/dao
34
+ shipping_method: "agent",
35
+ shipping_date: "2018-02-28"
45
36
  )
46
37
 
47
- # set the receiver of the order
48
- order.receiver = Smartsend::Receiver.new(
49
- id: "123456", # your internal customer id
38
+ # set the receiver of the shipment
39
+ shipment.receiver = Smartsend::Receiver.new(
40
+ internal_id: "123456", # your internal customer id
41
+ internal_reference: "123456",
50
42
  company: "Smart Send",
51
- name1: "Henrik Hansen",
52
- name2: "C/O Vivian Hansen",
53
- address1: "Willemoesgade 42",
54
- address2: "3.th.",
55
- zip: "2100",
43
+ name_line1: "Henrik Hansen",
44
+ name_line2: "C/O Vivian Hansen",
45
+ address_line1: "Willemoesgade 42",
46
+ address_line2: "3.th.",
47
+ postal_code: "2100",
56
48
  city: "Copenhagen",
57
49
  country: "DK",
58
- phone: "12345678",
59
- mail: "contact@smartsend.io"
50
+ sms: "12345678",
51
+ email: "contact@smartsend.io"
60
52
  )
61
53
 
62
- # optionally set the sender of the order
63
- order.sender = Smartsend::Sender.new(
64
- id: "123456",
54
+ # optionally set the sender of the shipment
55
+ shipment.sender = Smartsend::Sender.new(
56
+ internal_id: "123456",
57
+ internal_reference: "123456",
65
58
  company: "Smart Send",
66
- name1: "Henrik Hansen",
67
- name2: "C/O Vivian Hansen",
68
- address1: "Willemoesgade 42",
69
- address2: "3.th.",
70
- zip: "2100",
59
+ name_line1: "Henrik Hansen",
60
+ name_line2: "C/O Vivian Hansen",
61
+ address_line1: "Willemoesgade 42",
62
+ address_line2: "3.th.",
63
+ postal_code: "2100",
71
64
  city: "Copenhagen",
72
65
  country: "DK",
73
- phone: "12345678",
74
- mail: "contact@smartsend.io"
66
+ sms: "12345678",
67
+ email: "contact@smartsend.io"
75
68
  )
76
69
 
77
70
  # optionally ship to a droppoint by setting an agent
78
- order.agent = Smartsend::Agent.new(
71
+ shipment.agent = Smartsend::Agent.new(
79
72
  id: "2103", # droppoint id
80
73
  type: "PDK", # droppoint provider
81
74
  company: "Smart Send",
@@ -90,94 +83,68 @@ order.agent = Smartsend::Agent.new(
90
83
  mail: "contact@smartsend.io"
91
84
  )
92
85
 
93
- # add one or more parcels/fulfillments to the order
86
+ # add one or more parcels/fulfillments to the shipment
94
87
  parcel = Smartsend::Parcel.new(
95
88
  shipped_at: DateTime.now,
96
- reference: "123456789", # your internal parcel id
89
+ internal_id: "123456789", # your internal parcel id
90
+ internal_reference: "123456789",
97
91
  weight: 1.25,
98
92
  height: 21,
99
93
  width: 27,
100
94
  length: 35,
101
- size: "large",
102
- freetext_lines: [ # you can add up to 3 lines of freetext to be printed on the label
103
- "Brians birthsday gift",
104
- "Don't open this before your birthsday Brian",
105
- "We look forward to seeing you soon Brian"
106
- ]
95
+ freetext1: "Brians birthday gift",
96
+ freetext2: "Don't open this before your birthday Brian",
97
+ freetext3: "We look forward to seeing you soon Brian",
107
98
  )
108
99
 
109
100
  # add items to the parcel
110
101
  parcel_item = Smartsend::ParcelItem.new(
102
+ internal_id: "ABC123",
103
+ internal_reference: "ABC123",
111
104
  sku: "ABC123",
112
- title: "ABC123",
105
+ name: "Product name",
113
106
  quantity: 2,
114
- unit_weight: 1.25,
115
- unit_price: 144.5,
116
- currency: "DKK"
107
+ unit_weight: 1.25
117
108
  )
118
109
 
119
110
  parcel.items << parcel_item
120
- order.parcels << parcel
111
+ shipment.parcels << parcel
121
112
 
122
- # send the order to Smartsend.io
123
- order.save!
113
+ # send the shipment to Smartsend.io
114
+ shipment.save!
124
115
  ```
125
116
 
126
- After saving, the order it is updated with a url for the label pdf and tracking codes for each parcel.
117
+ After saving, the shipment it is updated with a url for the label pdf and tracking codes for each parcel.
127
118
 
128
119
  ```ruby
129
- order.label_url
120
+ shipment.label_url
130
121
  => 'http://v70.api.smartsend.dk/...'
131
122
 
132
- order.parcels.first.tracking_code
123
+ shipment.parcels.first.tracking_code
133
124
  => '1234...'
134
125
 
135
- order.parcels.first.tracking_url
126
+ shipment.parcels.first.tracking_url
136
127
  => 'https://tracking.postnord.com/...'
137
128
  ```
138
129
 
139
- ### Creating orders in batch
140
-
141
- You can create up to 10 orders in batch.
142
-
143
- This updates all orders with a label_url and also gives you a labels_url with the combined labels
144
-
145
- ```ruby
146
-
147
- orders = Smartsend::Orders.new
148
- orders << Smartsend::Order.new(...)
149
- orders << Smartsend::Order.new(...)
150
-
151
- # send the orders to Smartsend.io
152
- orders.save_all!
153
-
154
- orders.labels_url
155
- => 'http://v70.api.smartsend.dk/...'
156
-
157
- orders.first.label_url
158
- => 'http://v70.api.smartsend.dk/...'
159
- ```
160
-
161
130
  ### Using multiple accounts
162
131
 
163
132
  If your system requires multiple accounts you can pass an account instance when saving orders.
164
133
 
165
134
  ```ruby
166
- order = Smartsend::Order.new(...)
135
+ shipment = Smartsend::Order.new(...)
167
136
 
168
137
  account = Smartsend::Account.new(
169
- email: 'smartsend-username',
170
- license: 'smartsend-license-key'
138
+ api_token: 'smartsend-api-token'
171
139
  )
172
140
 
173
- order.save!(account: account)
141
+ shipment.save!(account: account)
174
142
  ```
175
143
 
176
144
  ### Validating account
177
145
  ```ruby
178
146
  account = Smartsend::Account.new(
179
- email: 'wrong-username',
180
- license: 'wrong-license-key'
147
+ api_token: 'wrong-smartsend-api-token'
181
148
  )
182
149
 
183
150
  account.valid?
@@ -1,18 +1,12 @@
1
1
  class Smartsend::Account
2
- attr_accessor :email, :license
2
+ attr_accessor :api_token
3
3
 
4
- def initialize(email:, license:)
5
- @email = email
6
- @license = license
4
+ def initialize(api_token:)
5
+ @api_token = api_token
7
6
  end
8
7
 
9
8
  def valid?
10
- begin
11
- response = Smartsend::Client.new(self).get_plain('verify_user')
12
- response.code.to_s == '200'
13
- rescue Smartsend::AuthorizationError => e
14
- false
15
- end
9
+ raise NotImplementedError
16
10
  end
17
11
 
18
12
  end
@@ -1,5 +1,7 @@
1
1
  class Smartsend::Address
2
- attr_accessor :id, :company ,:name1 ,:name2 ,:address1 ,:address2 ,:zip ,:city ,:country ,:phone ,:mail
2
+ attr_accessor :internal_id, :internal_reference, :company, :name_line1,
3
+ :name_line2, :address_line1, :address_line2, :postal_code,
4
+ :city, :country, :sms, :email
3
5
 
4
6
  def initialize(args={})
5
7
  args.each do |k, v|
@@ -9,16 +11,18 @@ class Smartsend::Address
9
11
 
10
12
  def serialize
11
13
  {
12
- :company => @company,
13
- :name1 => @name1,
14
- :name2 => @name2,
15
- :address1 => @address1,
16
- :address2 => @address2,
17
- :zip => @zip,
18
- :city => @city,
19
- :country => @country,
20
- :sms => @phone,
21
- :mail => @mail
14
+ :internal_id => internal_id,
15
+ :internal_reference => internal_reference,
16
+ :company => company,
17
+ :name_line1 => name_line1,
18
+ :name_line2 => name_line2,
19
+ :address_line1 => address_line1,
20
+ :address_line2 => address_line2,
21
+ :postal_code => postal_code,
22
+ :city => city,
23
+ :country => country,
24
+ :sms => sms,
25
+ :email => email
22
26
  }
23
27
  end
24
28
 
@@ -1,12 +1,28 @@
1
- class Smartsend::Agent < Smartsend::Address
2
- attr_accessor :type
1
+ class Smartsend::Agent
2
+ attr_accessor :internal_id, :internal_reference, :agent_no, :company,
3
+ :name_line1, :name_line2, :address_line1, :address_line2,
4
+ :postal_code, :city, :country
5
+
6
+ def initialize(args={})
7
+ args.each do |k, v|
8
+ instance_variable_set "@#{k}", v
9
+ end
10
+ end
3
11
 
4
12
  def serialize
5
- super.merge(
6
- id: @id,
7
- agentno: @id,
8
- agenttype: @type
9
- )
13
+ {
14
+ :internal_id => internal_id,
15
+ :internal_reference => internal_reference,
16
+ :agent_no => agent_no,
17
+ :company => company,
18
+ :name_line1 => name_line1,
19
+ :name_line2 => name_line2,
20
+ :address_line1 => address_line1,
21
+ :address_line2 => address_line2,
22
+ :postal_code => postal_code,
23
+ :city => city,
24
+ :country => country,
25
+ }
10
26
  end
11
27
 
12
28
  end
@@ -2,51 +2,55 @@ require 'http'
2
2
 
3
3
  class Smartsend::Client
4
4
 
5
- BASE_URL = 'http://smartsend-prod.apigee.net/v7'.freeze
6
-
7
5
  def initialize(account=nil)
8
6
  @account = account || Smartsend.account
9
7
  end
10
8
 
11
9
  def post(path, params)
12
10
  request do
13
- http.post("#{BASE_URL}/#{path}", json: params)
11
+ http.post(url(path), json: params)
14
12
  end
15
13
  end
16
14
 
17
15
  def get(path)
18
16
  request do
19
- http.get("#{BASE_URL}/#{path}")
17
+ http.get(url(path))
20
18
  end
21
19
  end
22
20
 
23
21
  def get_plain(path)
24
- http.get("#{BASE_URL}/#{path}")
22
+ http.get(url(path))
25
23
  end
26
24
 
27
25
  def request
28
26
  response = yield
29
27
 
30
- Rails.logger.debug(response.to_s) if defined?(Rails)
28
+ logger.debug(response)
29
+ logger.debug(response.body.to_s)
31
30
 
32
- case response.code.to_s
33
- when '200'
34
- Response.new(JSON.parse(response)).successful!
35
- when '401'
31
+ case response.code
32
+ when (200..299)
33
+ Response.new(JSON.parse(response)).successful!(response.code)
34
+ when 401
36
35
  raise Smartsend::AuthorizationError, 'Unable to authorize'
36
+ when 404
37
+ raise Smartsend::NotFoundError, 'Resource not found'
37
38
  else
38
- Response.new(response).failed!
39
+ Response.new(JSON.parse(response)).failed!(response.code)
39
40
  end
40
41
  end
41
42
 
42
43
  class Response < SimpleDelegator
44
+ attr_reader :http_code
43
45
 
44
- def successful!
46
+ def successful!(http_code)
47
+ @http_code = http_code
45
48
  @success = true
46
49
  self
47
50
  end
48
51
 
49
- def failed!
52
+ def failed!(http_code)
53
+ @http_code = http_code
50
54
  @success = false
51
55
  self
52
56
  end
@@ -63,20 +67,23 @@ class Smartsend::Client
63
67
 
64
68
  private
65
69
 
70
+ BASE_URL = 'http://smartsend-test.apigee.net/api/v1'.freeze
71
+
72
+ def url(path)
73
+ "#{BASE_URL}/#{path}"
74
+ end
75
+
76
+ def logger
77
+ Logger.new($stdout)
78
+ end
79
+
66
80
  def http
67
- raise Smartsend::MissingConfigError, 'Missing api_key' if Smartsend.api_key.nil?
68
- raise Smartsend::MissingConfigError, 'Missing email' if @account.email.nil?
69
- raise Smartsend::MissingConfigError, 'Missing license' if @account.license.nil?
70
-
71
- HTTP.headers(
72
- apikey: Smartsend.api_key,
73
- smartsendmail: @account.email,
74
- smartsendlicence: @account.license,
75
- cmssystem: Smartsend.cms_system,
76
- cmsversion: Smartsend.cms_version,
77
- appversion: Smartsend.app_version,
78
- test: Smartsend.test?
79
- )
81
+ raise Smartsend::MissingConfigError, 'Missing api_token' if @account.api_token.nil?
82
+
83
+ HTTP.headers({
84
+ accept: 'application/json',
85
+ api_token: @account.api_token
86
+ })
80
87
  end
81
88
 
82
89
  end
@@ -1,5 +1,8 @@
1
1
  class Smartsend::Parcel
2
- attr_accessor :id, :tracking_code, :tracking_url, :shipped_at, :reference, :weight, :height, :width, :length, :size, :freetext_lines, :items
2
+ attr_accessor :internal_id, :internal_reference, :weight, :height, :width,
3
+ :length, :freetext1, :freetext2, :freetext3,
4
+ :total_price_excluding_tax, :total_price_including_tax,
5
+ :total_tax_amount, :items
3
6
 
4
7
  def initialize(args={})
5
8
  args.each do |k, v|
@@ -10,23 +13,21 @@ class Smartsend::Parcel
10
13
  end
11
14
 
12
15
  def serialize
13
- params = {
14
- :shipdate => @shipped_at,
15
- :reference => @reference,
16
- :weight => @weight,
17
- :height => @height,
18
- :width => @width,
19
- :length => @length,
20
- :size => @size,
21
- :items => @items.map(&:serialize)
16
+ {
17
+ :internal_id => internal_id,
18
+ :internal_reference => internal_reference,
19
+ :weight => weight,
20
+ :height => height,
21
+ :width => width,
22
+ :length => length,
23
+ :freetext1 => freetext1,
24
+ :freetext2 => freetext2,
25
+ :freetext3 => freetext3,
26
+ :total_price_excluding_tax => total_price_excluding_tax,
27
+ :total_price_including_tax => total_price_including_tax,
28
+ :total_tax_amount => total_tax_amount,
29
+ :items => items.map(&:serialize)
22
30
  }
23
-
24
- freetext_lines.to_a.each_with_index do |line, i|
25
- params["freetext#{i.next}".to_sym] = line
26
- end
27
-
28
- params
29
-
30
31
  end
31
32
 
32
33
  end
@@ -1,5 +1,9 @@
1
1
  class Smartsend::ParcelItem
2
- attr_accessor :sku, :title, :quantity, :unit_weight, :unit_price, :currency
2
+ attr_accessor :internal_id, :internal_reference, :sku, :name, :description,
3
+ :hs_code, :country_of_origin, :image_url, :unit_weight,
4
+ :unit_price_excluding_tax, :unit_price_including_tax, :quantity,
5
+ :total_price_excluding_tax, :total_price_including_tax,
6
+ :total_tax_amount
3
7
 
4
8
  def initialize(args={})
5
9
  args.each do |k, v|
@@ -9,12 +13,21 @@ class Smartsend::ParcelItem
9
13
 
10
14
  def serialize
11
15
  {
12
- :sku => @sku,
13
- :title => @title,
14
- :quantity => @quantity,
15
- :unitweight => @unit_weight,
16
- :unitprice => @unit_price,
17
- :currency => @currency
16
+ :internal_id => internal_id,
17
+ :internal_reference => internal_reference,
18
+ :sku => sku,
19
+ :name => name,
20
+ :description => description,
21
+ :hs_code => hs_code,
22
+ :country_of_origin => country_of_origin,
23
+ :image_url => image_url,
24
+ :unit_weight => unit_weight,
25
+ :unit_price_excluding_tax => unit_price_excluding_tax,
26
+ :unit_price_including_tax => unit_price_including_tax,
27
+ :quantity => quantity,
28
+ :total_price_excluding_tax => total_price_excluding_tax,
29
+ :total_price_including_tax => total_price_including_tax,
30
+ :total_tax_amount => total_tax_amount,
18
31
  }
19
32
  end
20
33
 
@@ -1,5 +1,2 @@
1
1
  class Smartsend::Receiver < Smartsend::Address
2
- def serialize
3
- super.merge(receiverid: @id)
4
- end
5
2
  end
@@ -0,0 +1,13 @@
1
+ class Smartsend::RequestError
2
+ attr_accessor :links, :errors, :message, :code, :id
3
+
4
+ def self.build(response)
5
+ self.new.tap do |error|
6
+ error.id = response['id']
7
+ error.code = response['code']
8
+ error.message = response['message']
9
+ error.links = response['links']
10
+ error.errors = response['errors']
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,2 @@
1
1
  class Smartsend::Sender < Smartsend::Address
2
- def serialize
3
- super.merge(senderid: @id)
4
- end
5
2
  end
@@ -0,0 +1,18 @@
1
+ class Smartsend::Services
2
+ attr_accessor :email_notification, :sms_notification, :flex_delivery
3
+
4
+ def initialize(args={})
5
+ args.each do |k, v|
6
+ instance_variable_set "@#{k}", v
7
+ end
8
+ end
9
+
10
+ def serialize
11
+ {
12
+ :email_notification => email_notification,
13
+ :sms_notification => sms_notification,
14
+ :flex_delivery => flex_delivery
15
+ }
16
+ end
17
+
18
+ end
@@ -0,0 +1,56 @@
1
+ class Smartsend::Shipment
2
+ attr_accessor :internal_id, :internal_reference, :shipping_carrier,
3
+ :shipping_method, :shipping_date, :sender, :receiver, :agent,
4
+ :parcels, :services, :sub_total_price_excluding_tax,
5
+ :sub_total_price_including_tax, :shipping_price_excluding_tax,
6
+ :shipping_price_including_tax, :total_price_excluding_tax,
7
+ :total_price_including_tax, :total_tax_amount, :currency,
8
+ :success, :label_url, :error
9
+
10
+ def initialize(args={})
11
+ args.each do |k, v|
12
+ instance_variable_set "@#{k}", v
13
+ end
14
+
15
+ @parcels ||= []
16
+ end
17
+
18
+ def success?
19
+ success
20
+ end
21
+
22
+ def save!(account: nil)
23
+ response = Smartsend::Client.new(account).post('shipments/labels', self.serialize)
24
+
25
+ if response.success?
26
+ @success = true
27
+ @label_url = response.dig('data', 'pdf', 'link')
28
+ else
29
+ self.error = Smartsend::RequestError.build(response)
30
+ end
31
+ end
32
+
33
+ def serialize
34
+ {
35
+ :internal_id => internal_id,
36
+ :internal_reference => internal_reference,
37
+ :shipping_carrier => shipping_carrier,
38
+ :shipping_method => shipping_method,
39
+ :shipping_date => shipping_date,
40
+ :sender => sender&.serialize,
41
+ :receiver => receiver&.serialize,
42
+ :agent => agent&.serialize,
43
+ :parcels => parcels.map(&:serialize),
44
+ :services => services&.serialize,
45
+ :sub_total_price_excluding_tax => sub_total_price_excluding_tax,
46
+ :sub_total_price_including_tax => sub_total_price_including_tax,
47
+ :shipping_price_excluding_tax => shipping_price_excluding_tax,
48
+ :shipping_price_including_tax => shipping_price_including_tax,
49
+ :total_price_excluding_tax => total_price_excluding_tax,
50
+ :total_price_including_tax => total_price_including_tax,
51
+ :total_tax_amount => total_tax_amount,
52
+ :currency => currency
53
+ }
54
+ end
55
+
56
+ end
@@ -1,3 +1,3 @@
1
1
  module Smartsend
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end