smartsend-ruby 0.0.2 → 0.0.3

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: 31f5d50cd758e048368ed4208cb76d4bf015b24d
4
- data.tar.gz: 521b79b970af201b0def8cadc432b50ca4d592b4
3
+ metadata.gz: 1daea641d267754bf37d492b13ece1a513f6225e
4
+ data.tar.gz: 201cf9674744dbd1eb80118e310485814dbcc308
5
5
  SHA512:
6
- metadata.gz: 304eeeca29e60ff755246261364455fbeb90fe15f8b8943a6cbb9125fff69d2fd908df91f80a68c84f786f8fedcec9e34dfb8ed504a07071918782afa9e14360
7
- data.tar.gz: 65a5658a830cb9fd620baa76513a9d7994fd41abe9361665d537175db877d9872263972ffb6f61a11d716613e3ba7ec3bbe025393fc0c3f2b32d4991ca196148
6
+ metadata.gz: 97a78073a953cee9b2b2719dbaaa91dff9dbc42a6c5a0c19a4dec070bc22f58cca68766ba8ec39414849fe207b4a6854e67223b84a156fe7aba5aac856c2637c
7
+ data.tar.gz: a075eb79b103e433468a56d7db5bd6040ea5f47b0f1f023c1086367c2c5568e053d70957fb9e096bcdbef5587c86ae2dcd011aa51b4911867e1905c3705d95bf
data/MIT-LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2017 Aske Behrsin Hansen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -10,9 +10,8 @@ Before beginning you need a Smartsend account.
10
10
  gem install 'smartsend-ruby'
11
11
  ```
12
12
 
13
- ## Usage
14
13
 
15
- ### Configuring the client
14
+ ## Configuration
16
15
 
17
16
  ```ruby
18
17
  Smartsend.configure(
@@ -24,16 +23,18 @@ Smartsend.configure(
24
23
  )
25
24
  ```
26
25
 
26
+ ## Usage
27
+
27
28
  ### Creating an order
28
29
 
29
30
  This creates a label.
30
31
 
31
32
  ```ruby
32
-
33
33
  # initalize a new order
34
34
  order = Smartsend::Order.new(
35
+ id: "123123123", # your internal order id
35
36
  order_number: "AC12345789",
36
- carrier: "postdanmark", # postdanmark/gls/bring
37
+ carrier: "postnord", # postnord/gls/bring
37
38
  method: "private",
38
39
  return: false,
39
40
  total_price: 199.75,
@@ -58,8 +59,9 @@ order.receiver = Smartsend::Receiver.new(
58
59
  mail: "contact@smartsend.io"
59
60
  )
60
61
 
61
- # set the receiver of the order
62
+ # optionally set the sender of the order
62
63
  order.sender = Smartsend::Sender.new(
64
+ id: "123456",
63
65
  company: "Smart Send",
64
66
  name1: "Henrik Hansen",
65
67
  name2: "C/O Vivian Hansen",
@@ -72,9 +74,9 @@ order.sender = Smartsend::Sender.new(
72
74
  mail: "contact@smartsend.io"
73
75
  )
74
76
 
75
- # you can ship to a droppoint by seting an agent
77
+ # optionally ship to a droppoint by setting an agent
76
78
  order.agent = Smartsend::Agent.new(
77
- id: "7224", # droppoint id
79
+ id: "2103", # droppoint id
78
80
  type: "PDK", # droppoint provider
79
81
  company: "Smart Send",
80
82
  name1: "Henrik Hansen",
@@ -114,39 +116,59 @@ parcel_item = Smartsend::ParcelItem.new(
114
116
  currency: "DKK"
115
117
  )
116
118
 
117
- parcel.items = [parcel_item]
118
- order.parcels = [parcel]
119
+ parcel.items << parcel_item
120
+ order.parcels << parcel
119
121
 
120
122
  # send the order to Smartsend.io
121
123
  order.save!
122
124
  ```
123
125
 
124
- After saving an order it is updated with a url for the label pdf and tracking codes for each parcel.
126
+ After saving, the order it is updated with a url for the label pdf and tracking codes for each parcel.
125
127
 
126
128
  ```ruby
127
129
  order.label_url
128
- => 'https://www.pacsoftonline.com/...'
130
+ => 'http://v70.api.smartsend.dk/...'
129
131
 
130
132
  order.parcels.first.tracking_code
131
133
  => '1234...'
132
134
 
133
135
  order.parcels.first.tracking_url
134
- => 'https://...'
136
+ => 'https://tracking.postnord.com/...'
135
137
  ```
136
138
 
137
139
  ### Creating orders in batch
138
140
 
139
141
  You can create up to 10 orders in batch.
140
142
 
143
+ This updates all orders with a label_url and also gives you a labels_url with the combined labels
144
+
141
145
  ```ruby
142
- order1 = Smartsend::Order.new(...)
143
- order2 = Smartsend::Order.new(...)
144
146
 
145
- orders = Smartsend::Orders.new(order1, order2)
147
+ orders = Smartsend::Orders.new
148
+ orders << Smartsend::Order.new(...)
149
+ orders << Smartsend::Order.new(...)
146
150
 
147
151
  # send the orders to Smartsend.io
148
152
  orders.save_all!
149
153
 
154
+ orders.labels_url
155
+ => 'http://v70.api.smartsend.dk/...'
156
+
150
157
  orders.first.label_url
151
- => 'https://www.pacsoftonline.com/...'
158
+ => 'http://v70.api.smartsend.dk/...'
159
+ ```
160
+
161
+ ### Using multiple accounts
162
+
163
+ If your system requires multiple accounts you can pass an account instance when saving orders.
164
+
165
+ ```ruby
166
+ order = Smartsend::Order.new(...)
167
+
168
+ account = Smartsend::Account.new(
169
+ email: 'smartsend-username',
170
+ license: 'smartsend-license-key'
171
+ )
172
+
173
+ order.save!(account: account)
152
174
  ```
@@ -0,0 +1,9 @@
1
+ class Smartsend::Account
2
+ attr_accessor :email, :license
3
+
4
+ def initialize(email:, license:)
5
+ @email = email
6
+ @license = license
7
+ end
8
+
9
+ end
@@ -4,6 +4,10 @@ class Smartsend::Client
4
4
 
5
5
  BASE_URL = 'http://smartsend-prod.apigee.net/v7/booking'.freeze
6
6
 
7
+ def initialize(account=nil)
8
+ @account = account || Smartsend.account
9
+ end
10
+
7
11
  def post(path, params)
8
12
  request do
9
13
  http.post("#{BASE_URL}/#{path}", json: params)
@@ -15,8 +19,11 @@ class Smartsend::Client
15
19
 
16
20
  Rails.logger.debug(response.to_s) if defined?(Rails)
17
21
 
18
- if /^2/ === response.code.to_s
22
+ case response.code.to_s
23
+ when '200'
19
24
  Response.new(JSON.parse(response)).successful!
25
+ when '401'
26
+ raise Smartsend::AuthorizationError, 'Unable to authorize'
20
27
  else
21
28
  Response.new(response).failed!
22
29
  end
@@ -48,13 +55,13 @@ class Smartsend::Client
48
55
 
49
56
  def http
50
57
  raise Smartsend::MissingConfigError, 'Missing api_key' if Smartsend.api_key.nil?
51
- raise Smartsend::MissingConfigError, 'Missing email' if Smartsend.email.nil?
52
- raise Smartsend::MissingConfigError, 'Missing license' if Smartsend.license.nil?
58
+ raise Smartsend::MissingConfigError, 'Missing email' if @account.email.nil?
59
+ raise Smartsend::MissingConfigError, 'Missing license' if @account.license.nil?
53
60
 
54
61
  HTTP.headers(
55
62
  apikey: Smartsend.api_key,
56
- smartsendmail: Smartsend.email,
57
- smartsendlicence: Smartsend.license,
63
+ smartsendmail: @account.email,
64
+ smartsendlicence: @account.license,
58
65
  cmssystem: Smartsend.cms_system,
59
66
  cmsversion: Smartsend.cms_version,
60
67
  appversion: Smartsend.app_version,
@@ -6,10 +6,12 @@ class Smartsend::Order
6
6
  args.each do |k, v|
7
7
  instance_variable_set "@#{k}", v
8
8
  end
9
+
10
+ @parcels ||= []
9
11
  end
10
12
 
11
- def save!
12
- response = Smartsend::Client.new.post('order', self.serialize)
13
+ def save!(account: nil)
14
+ response = Smartsend::Client.new(account).post('order', self.serialize)
13
15
 
14
16
  if response.success?
15
17
  update_label_url_tracking_codes(response)
@@ -20,8 +22,7 @@ class Smartsend::Order
20
22
  end
21
23
 
22
24
  def update_label_url_tracking_codes(response)
23
- @id = response['id']
24
- @label_url = response['link']
25
+ @label_url = response['pdflink']
25
26
 
26
27
  response['parcels'].each do |parcel_response|
27
28
  if parcel = @parcels.select { |x| x.reference.to_s == parcel_response['reference'].to_s }.first
@@ -35,6 +36,7 @@ class Smartsend::Order
35
36
  def serialize
36
37
  {
37
38
  orderno: @order_number,
39
+ reference: @id,
38
40
  carrier: @carrier,
39
41
  method: @method,
40
42
  return: @return,
@@ -46,7 +48,7 @@ class Smartsend::Order
46
48
  sender: @sender&.serialize,
47
49
  receiver: @receiver&.serialize,
48
50
  agent: @agent&.serialize,
49
- parcels: @parcels.to_a.map(&:serialize),
51
+ parcels: @parcels.map(&:serialize),
50
52
  service: service.serialize
51
53
  }
52
54
  end
@@ -1,14 +1,15 @@
1
1
  class Smartsend::Orders
2
2
  include Enumerable
3
+ attr_accessor :labels_url
3
4
 
4
5
  def initialize(*orders)
5
6
  @orders = orders
6
7
  end
7
8
 
8
- def save_all!
9
+ def save_all!(account: nil)
9
10
  raise Smartsend::TooManyOrdersError, "You can save a maximum of 10 orders in batch" if count > 10
10
11
 
11
- response = Smartsend::Client.new.post('orders', self.serialize)
12
+ response = Smartsend::Client.new(account).post('orders', self.serialize)
12
13
 
13
14
  update_label_url_tracking_codes(response)
14
15
 
@@ -16,13 +17,19 @@ class Smartsend::Orders
16
17
  end
17
18
 
18
19
  def update_label_url_tracking_codes(response)
20
+ @labels_url = response['combine_pdf']
21
+
19
22
  response['orders'].each do |response_order|
20
- if order = @orders.select { |x| x.order_number.to_s == response.orderno.to_s }.first
21
- order.update_label_url_tracking_codes(response)
23
+ if order = @orders.select { |x| x.id.to_s == response_order['reference'].to_s }.first
24
+ order.update_label_url_tracking_codes(response_order)
22
25
  end
23
26
  end
24
27
  end
25
28
 
29
+ def <<(order)
30
+ @orders << order
31
+ end
32
+
26
33
  def serialize
27
34
  @orders.map(&:serialize)
28
35
  end
@@ -5,6 +5,8 @@ class Smartsend::Parcel
5
5
  args.each do |k, v|
6
6
  instance_variable_set "@#{k}", v
7
7
  end
8
+
9
+ @items ||= []
8
10
  end
9
11
 
10
12
  def serialize
@@ -16,7 +18,7 @@ class Smartsend::Parcel
16
18
  :width => @width,
17
19
  :length => @length,
18
20
  :size => @size,
19
- :items => @items.to_a.map(&:serialize)
21
+ :items => @items.map(&:serialize)
20
22
  }
21
23
 
22
24
  freetext_lines.to_a.each_with_index do |line, i|
@@ -1,5 +1,5 @@
1
1
  class Smartsend::Receiver < Smartsend::Address
2
2
  def serialize
3
- super.merge(reciverid: @id)
3
+ super.merge(receiverid: @id)
4
4
  end
5
5
  end
@@ -1,5 +1,5 @@
1
1
  class Smartsend::Sender < Smartsend::Address
2
2
  def serialize
3
- super.merge(senderid: nil)
3
+ super.merge(senderid: @id)
4
4
  end
5
5
  end
@@ -10,13 +10,7 @@ class Smartsend::Service
10
10
  def serialize
11
11
  {
12
12
  "notemail": @email,
13
- "notesms": @phone,
14
- "prenote": true,
15
- "prenote_from": "contact@smartsend.io",
16
- "prenote_to": "contact@smartsend.io",
17
- "prenote_message": "Your order is now on the way.",
18
- "flex": "string",
19
- "waybillid": "string"
13
+ "notesms": @phone
20
14
  }
21
15
  end
22
16
 
@@ -1,3 +1,3 @@
1
1
  module Smartsend
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/smartsend.rb CHANGED
@@ -13,6 +13,10 @@ module Smartsend
13
13
  end
14
14
  end
15
15
 
16
+ def self.account
17
+ Smartsend::Account.new(email: @@email, license: @@license)
18
+ end
19
+
16
20
  def self.api_key
17
21
  @@api_key
18
22
  end
@@ -52,16 +56,20 @@ module Smartsend
52
56
  class MissingConfigError < StandardError
53
57
  end
54
58
 
59
+ class AuthorizationError < StandardError
60
+ end
55
61
 
56
62
  end
57
63
 
58
- require 'smartsend/order'
59
- require 'smartsend/orders'
60
- require 'smartsend/address'
61
- require 'smartsend/receiver'
62
- require 'smartsend/sender'
63
- require 'smartsend/agent'
64
- require 'smartsend/service'
65
- require 'smartsend/parcel'
66
- require 'smartsend/parcel_item'
67
- require 'smartsend/client'
64
+ require_relative 'smartsend/order'
65
+ require_relative 'smartsend/orders'
66
+ require_relative 'smartsend/address'
67
+ require_relative 'smartsend/receiver'
68
+ require_relative 'smartsend/sender'
69
+ require_relative 'smartsend/agent'
70
+ require_relative 'smartsend/service'
71
+ require_relative 'smartsend/parcel'
72
+ require_relative 'smartsend/parcel_item'
73
+ require_relative 'smartsend/client'
74
+ require_relative 'smartsend/account'
75
+ require_relative 'smartsend/version'
@@ -7,10 +7,10 @@ Gem::Specification.new do |s|
7
7
  s.version = Smartsend::VERSION
8
8
  s.date = '2017-11-13'
9
9
  s.summary = "Smartsend.io api client"
10
- s.description = "Create orders in smartsend for smartsend"
10
+ s.description = "Create orders in smartsend"
11
11
  s.authors = ["Aske Hansen"]
12
12
  s.email = 'aske@deeco.dk'
13
- s.homepage = 'https://github.com/askehansen/smartsend-ruby'
13
+ s.homepage = 'https://github.com/smartsendio/smartsend-ruby'
14
14
  s.license = 'MIT'
15
15
 
16
16
  s.files = `git ls-files`.split("\n")
@@ -7,7 +7,7 @@ class AddressesTest < Minitest::Test
7
7
  sender = Smartsend::Sender.new(address)
8
8
 
9
9
  assert_equal({
10
- "senderid": nil,
10
+ "senderid": 1,
11
11
  "company": "Smart Send",
12
12
  "name1": "Henrik Hansen",
13
13
  "name2": "C/O Vivian Hansen",
@@ -25,7 +25,7 @@ class AddressesTest < Minitest::Test
25
25
  receiver = Smartsend::Receiver.new(address)
26
26
 
27
27
  assert_equal({
28
- "reciverid": 1,
28
+ "receiverid": 1,
29
29
  "company": "Smart Send",
30
30
  "name1": "Henrik Hansen",
31
31
  "name2": "C/O Vivian Hansen",
data/test/test_orders.rb CHANGED
@@ -25,6 +25,7 @@ class OrdersTest < Minitest::Test
25
25
  def params
26
26
  {
27
27
  orderno: 'AC12345789',
28
+ reference: '1234567',
28
29
  carrier: 'postdanmark',
29
30
  method: 'private',
30
31
  return: false,
@@ -37,12 +38,13 @@ class OrdersTest < Minitest::Test
37
38
  receiver: nil,
38
39
  agent: nil,
39
40
  parcels: [],
40
- service: {:notemail=>"contact@smartsend.io", :notesms=>"12345678", :prenote=>true, :prenote_from=>"contact@smartsend.io", :prenote_to=>"contact@smartsend.io", :prenote_message=>"Your order is now on the way.", :flex=>"string", :waybillid=>"string"}
41
+ service: { :notemail=>"contact@smartsend.io", :notesms=>"12345678" }
41
42
  }
42
43
  end
43
44
 
44
45
  def order
45
46
  Smartsend::Order.new(
47
+ :id => "1234567",
46
48
  :order_number => "AC12345789",
47
49
  :carrier => "postdanmark",
48
50
  :method => "private",
data/test/test_service.rb CHANGED
@@ -8,13 +8,7 @@ class SerivceTest < Minitest::Test
8
8
 
9
9
  assert_equal({
10
10
  "notemail": "contact@smartsend.io",
11
- "notesms": "12345678",
12
- "prenote": true,
13
- "prenote_from": "contact@smartsend.io",
14
- "prenote_to": "contact@smartsend.io",
15
- "prenote_message": "Your order is now on the way.",
16
- "flex": "string",
17
- "waybillid": "string"
11
+ "notesms": "12345678"
18
12
  }, service.serialize)
19
13
  end
20
14
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smartsend-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aske Hansen
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.2'
27
- description: Create orders in smartsend for smartsend
27
+ description: Create orders in smartsend
28
28
  email: aske@deeco.dk
29
29
  executables: []
30
30
  extensions: []
@@ -33,10 +33,12 @@ files:
33
33
  - ".gitignore"
34
34
  - Gemfile
35
35
  - Gemfile.lock
36
+ - MIT-LICENSE
36
37
  - README.md
37
38
  - Rakefile
38
39
  - lib/smartsend-ruby.rb
39
40
  - lib/smartsend.rb
41
+ - lib/smartsend/account.rb
40
42
  - lib/smartsend/address.rb
41
43
  - lib/smartsend/agent.rb
42
44
  - lib/smartsend/client.rb
@@ -56,7 +58,7 @@ files:
56
58
  - test/test_parcels.rb
57
59
  - test/test_service.rb
58
60
  - test/test_smartsend.rb
59
- homepage: https://github.com/askehansen/smartsend-ruby
61
+ homepage: https://github.com/smartsendio/smartsend-ruby
60
62
  licenses:
61
63
  - MIT
62
64
  metadata: {}