shipcloud 0.2.0 → 0.3.0

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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZjQxNjJjMmI2ODIwMzQ5MjE3NGZmMzE0MDlhNGRlZmIyYWM0OTU2ZA==
5
+ data.tar.gz: !binary |-
6
+ ZTk2OTc3ZDM0MmZhYTFiM2U4MTY5NjZjMGVmYzJiZjgzMWNiYjJmYw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MWU3YzRmZDBmZWQyOTI5ZDJhMDYyMjU2ZTI2NDI0YjI0YTU0OGZjMDczZmIy
10
+ OTJhZWEyNzMwNDhlYTJjNTgzOTc4NzFkNTlmYmViODU4OTgzZjMzZDkzZmZk
11
+ N2EzMzdmMjI3ZWUwY2Y3M2Q0M2VjZDhhNGE1Y2NhMTFkZDYwNzc=
12
+ data.tar.gz: !binary |-
13
+ ZjljMjBjMWIzMjU2ZjgyMTAyZGYzZjg4OTA2ZWJmMGY5ZGVjNzE0OTcyNDBh
14
+ OTRkYzA3NTZkNmJjNTIxODU4MmNlMzZjODVlMjYzNDFhYzAyOGI4ZTFjMmU0
15
+ NThlYzdiMWIzOGY2YjY0NTRmZGFhZmM4ODEwMTg4ZjUzNTFjMGY=
data/README.md CHANGED
@@ -19,6 +19,42 @@ Or install it yourself as:
19
19
 
20
20
  ## Usage
21
21
 
22
+ Before using the shipcloud API, you need to set the API access key:
23
+
24
+ ```
25
+ Shipcloud.api_key = 'your-api-key-goes-here'
26
+ ```
27
+
28
+ Since Version 0.3.0, you can also do this via a configuration block, e.g. in an initializer:
29
+
30
+ ```
31
+ Shipcloud.configure do |config|
32
+ config.api_key = 'your-api-key-goes-here'
33
+ end
34
+ ```
35
+
36
+ You can sign up for a developer account at *[shipcloud.io](http://www.shipcloud.io)*
37
+
38
+ ### Create a new shipment
39
+
40
+ To create a new Shipment on the shipclod platform, you need to provide the name of the carrier, to- and from-address, and the package dimensions.
41
+ For details, see *[shipcloud API documentation on Shipments](http://docs.shipcloud.apiary.io/#shipmentresources)*
42
+ ```
43
+ Shipcloud::Shipment.create(
44
+ carrier: 'ups',
45
+ from: from-address-params,
46
+ to: to-address-params,
47
+ package: package-params,
48
+ create_shipping_label: true
49
+ )
50
+ ```
51
+
52
+ `Shipment#create` will return shipping label and tracking information, encapsulated in a `Shipcloud::Shipment` object:
53
+
54
+ ```
55
+ shipment = Shipcloud::Shipment.create(...) # parameters ommitted
56
+ shipment.tracking_url # -> http://track.shipcloud.io/uzdgu22z3ed12
57
+ ```
22
58
 
23
59
  ## Contributing
24
60
 
data/lib/shipcloud.rb CHANGED
@@ -4,13 +4,12 @@ require "json"
4
4
  require "shipcloud/version"
5
5
 
6
6
  module Shipcloud
7
- API_BASE = "api.shipcloud.io"
8
7
  API_VERSION = "v1"
9
8
  ROOT_PATH = File.dirname(__FILE__)
10
9
 
11
10
  API_HEADERS = {
12
11
  "Content-Type" => "application/json",
13
- "User-Agent" => "shipcloud-ruby v#{Shipcloud::VERSION}, API v#{Shipcloud::API_VERSION}, #{RUBY_VERSION}, #{RUBY_PLATFORM}, #{RUBY_PATCHLEVEL}"
12
+ "User-Agent" => "shipcloud-ruby v#{Shipcloud::VERSION}, API #{Shipcloud::API_VERSION}, #{RUBY_VERSION}, #{RUBY_PLATFORM}, #{RUBY_PATCHLEVEL}"
14
13
  }
15
14
 
16
15
  @@api_key = nil
@@ -21,6 +20,8 @@ module Shipcloud
21
20
  module Operations
22
21
  autoload :Create, "shipcloud/operations/create"
23
22
  autoload :Find, "shipcloud/operations/find"
23
+ autoload :Delete, "shipcloud/operations/delete"
24
+ autoload :Update, "shipcloud/operations/update"
24
25
  end
25
26
 
26
27
  module Request
@@ -34,11 +35,34 @@ module Shipcloud
34
35
  class AuthenticationError < ShipcloudError; end
35
36
  class APIError < ShipcloudError; end
36
37
 
38
+ class << self
39
+ attr_accessor :configuration
40
+ end
41
+
42
+ def self.configuration
43
+ @configuration ||= Configuration.new
44
+ end
45
+
46
+ def self.configure
47
+ yield(configuration)
48
+ end
49
+
50
+
51
+ class Configuration
52
+ attr_accessor :api_key, :api_base, :use_ssl
53
+
54
+ def initialize
55
+ @api_key = nil
56
+ @api_base = 'api.shipcloud.io'
57
+ @use_ssl = true
58
+ end
59
+ end
60
+
37
61
  # Returns the set api key
38
62
  #
39
63
  # @return [String] The api key
40
64
  def self.api_key
41
- @@api_key
65
+ configuration.api_key
42
66
  end
43
67
 
44
68
  # Sets the api key
@@ -46,6 +70,7 @@ module Shipcloud
46
70
  # @param [String] api_key The api key
47
71
  def self.api_key=(api_key)
48
72
  @@api_key = api_key
73
+ configuration.api_key = api_key
49
74
  end
50
75
 
51
76
  # Makes a request against the shipcloud API
@@ -0,0 +1,19 @@
1
+ module Shipcloud
2
+ module Operations
3
+ module Delete
4
+ module ClassMethods
5
+ # Deletes the given object
6
+ #
7
+ # @param [String] id The id of the object that gets deleted
8
+ def delete(id)
9
+ response = Shipcloud.request(:delete, "#{self.name.split("::").last.downcase}s/#{id}", {})
10
+ true
11
+ end
12
+ end
13
+
14
+ def self.included(base)
15
+ base.extend(ClassMethods)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ module Shipcloud
2
+ module Operations
3
+ module Update
4
+
5
+ module ClassMethods
6
+ # Updates a object
7
+ # @param [String] id The id of the object that should be updated
8
+ # @param [Hash] attributes The attributes that should be updated
9
+ def update(id, attributes)
10
+ response = Shipcloud.request(:put, "#{self.name.split("::").last.downcase}s/#{id}", attributes)
11
+ self.new(response)
12
+ end
13
+ end
14
+
15
+ def self.included(base)
16
+ base.extend(ClassMethods)
17
+ end
18
+
19
+ # Updates a object
20
+ #
21
+ # @param [Hash] attributes The attributes that should be updated
22
+ def update(attributes)
23
+ response = Shipcloud.request(:put, "#{self.class.name.split("::").last.downcase}s/#{id}", attributes)
24
+ set_attributes(response)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -8,11 +8,16 @@ module Shipcloud
8
8
  end
9
9
 
10
10
  def setup_https
11
- @https = Net::HTTP.new(API_BASE, Net::HTTP.https_default_port)
12
- @https.use_ssl = true
13
- @https.verify_mode = OpenSSL::SSL::VERIFY_PEER
14
- # @https.ca_file = File.join(ROOT_PATH, "data/shipcloud.crt")
15
- # @https.set_debug_output $stdout
11
+ if Shipcloud.configuration.use_ssl
12
+ @https = Net::HTTP.new(Shipcloud.configuration.api_base, Net::HTTP.https_default_port)
13
+ @https.use_ssl = true
14
+ @https.verify_mode = OpenSSL::SSL::VERIFY_PEER
15
+ # @https.ca_file = File.join(ROOT_PATH, "data/shipcloud.crt")
16
+ else
17
+ @https = Net::HTTP.new(Shipcloud.configuration.api_base, Net::HTTP.http_default_port)
18
+ @https.use_ssl = false
19
+ end
20
+ @https.set_debug_output $stdout
16
21
  end
17
22
 
18
23
  def request
@@ -11,8 +11,10 @@ module Shipcloud
11
11
  def validated_data_for(incoming_response)
12
12
  self.response = incoming_response
13
13
  verify_response_code
14
- info.data = JSON.parse(response.body)
15
- validate_response_data
14
+ if response.body
15
+ info.data = JSON.parse(response.body)
16
+ validate_response_data
17
+ end
16
18
  info.data
17
19
  end
18
20
 
@@ -24,7 +26,7 @@ module Shipcloud
24
26
  end
25
27
 
26
28
  def validate_response_data
27
- raise APIError.new(info.data["error"]) if info.data["error"]
29
+ raise APIError.new(info.data["errors"]) if info.data["errors"]
28
30
  end
29
31
  end
30
32
  end
@@ -1,5 +1,7 @@
1
1
  module Shipcloud
2
2
  class Shipment < Base
3
+ include Shipcloud::Operations::Delete
4
+ include Shipcloud::Operations::Update
3
5
 
4
6
  attr_accessor :from, :to, :carrier, :package, :reference_number
5
7
  attr_reader :id, :created_at, :carrier_tracking_no, :tracking_url, :label_url, :packages, :price
@@ -1,3 +1,3 @@
1
1
  module Shipcloud
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -58,4 +58,18 @@ describe Shipcloud::Shipment do
58
58
  Shipcloud::Shipment.find("123")
59
59
  end
60
60
  end
61
+
62
+ describe ".update" do
63
+ it "makes a new PUT request using the correct API endpoint" do
64
+ Shipcloud.should_receive(:request).with(:put, "shipments/123", {:carrier => 'ups' }).and_return("data" => {})
65
+ Shipcloud::Shipment.update("123", {:carrier => 'ups' })
66
+ end
67
+ end
68
+
69
+ describe ".delete" do
70
+ it "makes a new DELETE request using the correct API endpoint" do
71
+ Shipcloud.should_receive(:request).with(:delete, "shipments/123", {}).and_return(true)
72
+ Shipcloud::Shipment.delete("123")
73
+ end
74
+ end
61
75
  end
@@ -10,24 +10,69 @@ describe Shipcloud do
10
10
 
11
11
  context "with an invalid api key" do
12
12
  before(:each) do
13
- WebMock.stub_request(:any, /#{Shipcloud::API_BASE}/).to_return(:body => "{}")
13
+ WebMock.stub_request(:any, /#{Shipcloud.configuration.api_base}/).to_return(:body => "{}")
14
14
  Shipcloud.api_key = "your-api-key"
15
15
  end
16
16
 
17
17
  it "attempts to get a url with one param" do
18
18
  Shipcloud.request(:get, "transactions", { param_name: "param_value" })
19
- WebMock.should have_requested(:get, "https://#{Shipcloud::api_key}:@#{Shipcloud::API_BASE}/#{Shipcloud::API_VERSION}/transactions?param_name=param_value")
19
+ WebMock.should have_requested(:get, "https://#{Shipcloud::api_key}:@#{Shipcloud.configuration.api_base}/#{Shipcloud::API_VERSION}/transactions?param_name=param_value")
20
20
  end
21
21
 
22
22
  it "attempts to get a url with more than one param" do
23
23
  Shipcloud.request(:get, "transactions", { client: "client_id", order: "created_at_desc" })
24
- WebMock.should have_requested(:get, "https://#{Shipcloud::api_key}:@#{Shipcloud::API_BASE}/#{Shipcloud::API_VERSION}/transactions?client=client_id&order=created_at_desc")
24
+ WebMock.should have_requested(:get, "https://#{Shipcloud::api_key}:@#{Shipcloud.configuration.api_base}/#{Shipcloud::API_VERSION}/transactions?client=client_id&order=created_at_desc")
25
25
  end
26
26
 
27
27
  it "doesn't add a question mark if no params" do
28
28
  Shipcloud.request(:get, "transactions", {})
29
- WebMock.should have_requested(:get, "https://#{Shipcloud::api_key}:@#{Shipcloud::API_BASE}/#{Shipcloud::API_VERSION}/transactions")
29
+ WebMock.should have_requested(:get, "https://#{Shipcloud::api_key}:@#{Shipcloud.configuration.api_base}/#{Shipcloud::API_VERSION}/transactions")
30
30
  end
31
31
  end
32
32
  end
33
+
34
+ describe '.configure' do
35
+ before :each do
36
+ Shipcloud.configuration = nil
37
+ end
38
+
39
+ it 'defaults api_key to nil' do
40
+ expect(Shipcloud.configuration.api_key).to be_nil
41
+ end
42
+
43
+ it 'sets the api_key' do
44
+ Shipcloud.configure do |config|
45
+ config.api_key = 'your-api-key'
46
+ end
47
+ expect(Shipcloud.configuration.api_key).to eq 'your-api-key'
48
+ end
49
+
50
+ it 'gets the api key, set as a class variable (DEPRECATED)' do
51
+ Shipcloud.api_key = 'old-school-api-key'
52
+ expect(Shipcloud.api_key).to eq 'old-school-api-key'
53
+ expect(Shipcloud.configuration.api_key).to eq 'old-school-api-key'
54
+ end
55
+
56
+ it "defaults api_base to 'api.shipcloud.io'" do
57
+ expect(Shipcloud.configuration.api_base).to eq 'api.shipcloud.io'
58
+ end
59
+
60
+ it 'overwrites the default api base' do
61
+ Shipcloud.configure do |config|
62
+ config.api_base = 'api.shipcloud.dev'
63
+ end
64
+ expect(Shipcloud.configuration.api_base).to eq 'api.shipcloud.dev'
65
+ end
66
+
67
+ it 'defaults use_ssl to true' do
68
+ expect(Shipcloud.configuration.use_ssl).to be_true
69
+ end
70
+
71
+ it 'overwrites the default ssl mode' do
72
+ Shipcloud.configure do |config|
73
+ config.use_ssl = false
74
+ end
75
+ expect(Shipcloud.configuration.use_ssl).to be_false
76
+ end
77
+ end
33
78
  end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shipcloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - sthollmann
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-30 00:00:00.000000000 Z
11
+ date: 2013-09-12 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: json
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: bundler
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rake
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ! '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ! '>='
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rspec
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ! '>='
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ! '>='
76
67
  - !ruby/object:Gem::Version
@@ -91,7 +82,9 @@ files:
91
82
  - lib/shipcloud.rb
92
83
  - lib/shipcloud/base.rb
93
84
  - lib/shipcloud/operations/create.rb
85
+ - lib/shipcloud/operations/delete.rb
94
86
  - lib/shipcloud/operations/find.rb
87
+ - lib/shipcloud/operations/update.rb
95
88
  - lib/shipcloud/request/base.rb
96
89
  - lib/shipcloud/request/connection.rb
97
90
  - lib/shipcloud/request/info.rb
@@ -108,27 +101,26 @@ files:
108
101
  homepage: https://github.com/webionate/shipcloud-ruby
109
102
  licenses:
110
103
  - MIT
104
+ metadata: {}
111
105
  post_install_message:
112
106
  rdoc_options: []
113
107
  require_paths:
114
108
  - lib
115
109
  required_ruby_version: !ruby/object:Gem::Requirement
116
- none: false
117
110
  requirements:
118
111
  - - ! '>='
119
112
  - !ruby/object:Gem::Version
120
113
  version: '0'
121
114
  required_rubygems_version: !ruby/object:Gem::Requirement
122
- none: false
123
115
  requirements:
124
116
  - - ! '>='
125
117
  - !ruby/object:Gem::Version
126
118
  version: '0'
127
119
  requirements: []
128
120
  rubyforge_project:
129
- rubygems_version: 1.8.25
121
+ rubygems_version: 2.0.7
130
122
  signing_key:
131
- specification_version: 3
123
+ specification_version: 4
132
124
  summary: A wrapper for the shipcloud API
133
125
  test_files:
134
126
  - spec/shipcloud/request/base_spec.rb