shipcloud 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shipcloud.gemspec
4
+ gemspec
5
+
6
+ gem "rspec"
7
+ gem "rake"
8
+ gem "webmock"
9
+ gem "pry"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 sthollmann
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # shipcloud
2
+
3
+ A Ruby wrapper for the shipcloud API
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'shipcloud'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install shipcloud
18
+
19
+ ## Usage
20
+
21
+
22
+ ## Contributing
23
+
24
+ 1. Fork it
25
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
26
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
27
+ 4. Push to the branch (`git push origin my-new-feature`)
28
+ 5. Create new Pull Request
29
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/lib/shipcloud.rb ADDED
@@ -0,0 +1,60 @@
1
+ require "net/http"
2
+ require "net/https"
3
+ require "json"
4
+ require "shipcloud/version"
5
+
6
+ module Shipcloud
7
+ API_BASE = "api.shipcloud.io"
8
+ API_VERSION = "v1"
9
+ ROOT_PATH = File.dirname(__FILE__)
10
+
11
+ API_HEADERS = {
12
+ "Content-Type" => "application/json",
13
+ "User-Agent" => "shipcloud-ruby v#{Shipcloud::VERSION}, API v#{Shipcloud::API_VERSION}, #{RUBY_VERSION}, #{RUBY_PLATFORM}, #{RUBY_PATCHLEVEL}"
14
+ }
15
+
16
+ @@api_key = nil
17
+
18
+ autoload :Base, "shipcloud/base"
19
+ autoload :Shipment, "shipcloud/shipment"
20
+
21
+ module Operations
22
+ autoload :Create, "shipcloud/operations/create"
23
+ end
24
+
25
+ module Request
26
+ autoload :Base, "shipcloud/request/base"
27
+ autoload :Connection, "shipcloud/request/connection"
28
+ autoload :Info, "shipcloud/request/info"
29
+ autoload :Validator, "shipcloud/request/validator"
30
+ end
31
+
32
+ class ShipcloudError < StandardError; end
33
+ class AuthenticationError < ShipcloudError; end
34
+ class APIError < ShipcloudError; end
35
+
36
+ # Returns the set api key
37
+ #
38
+ # @return [String] The api key
39
+ def self.api_key
40
+ @@api_key
41
+ end
42
+
43
+ # Sets the api key
44
+ #
45
+ # @param [String] api_key The api key
46
+ def self.api_key=(api_key)
47
+ @@api_key = api_key
48
+ end
49
+
50
+ # Makes a request against the shipcloud API
51
+ #
52
+ # @param [Symbol] http_method The http method to use, must be one of :get, :post, :put and :delete
53
+ # @param [String] api_url The API url to use
54
+ # @param [Hash] data The data to send, e.g. used when creating new objects.
55
+ # @return [Array] The parsed JSON response.
56
+ def self.request(http_method, api_url, data)
57
+ info = Request::Info.new(http_method, api_url, data)
58
+ Request::Base.new(info).perform
59
+ end
60
+ end
@@ -0,0 +1,21 @@
1
+ module Shipcloud
2
+ class Base
3
+ include Shipcloud::Operations::Create
4
+
5
+ # Initializes an object using the given attributes
6
+ #
7
+ # @param [Hash] attributes The attributes to use for initialization
8
+ def initialize(attributes = {})
9
+ set_attributes(attributes)
10
+ end
11
+
12
+ # Sets the attributes
13
+ #
14
+ # @param [Hash] attributes The attributes to initialize
15
+ def set_attributes(attributes)
16
+ attributes.each_pair do |key, value|
17
+ instance_variable_set("@#{key}", value)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module Shipcloud
2
+ module Operations
3
+ module Create
4
+ module ClassMethods
5
+ # Creates a new object
6
+ #
7
+ # @param [Hash] attributes The attributes of the created object
8
+ def create(attributes)
9
+ response = Shipcloud.request(:post, "#{self.name.split("::").last.downcase}s", attributes)
10
+ self.new(response)
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,33 @@
1
+ module Shipcloud
2
+ module Request
3
+ class Base
4
+ attr_reader :info
5
+ attr_accessor :response
6
+
7
+ def initialize(info)
8
+ @info = info
9
+ end
10
+
11
+ def perform
12
+ raise AuthenticationError if Shipcloud.api_key.nil?
13
+ connection.setup_https
14
+ send_request
15
+ validator.validated_data_for(response)
16
+ end
17
+
18
+ protected
19
+
20
+ def send_request
21
+ self.response = connection.request
22
+ end
23
+
24
+ def connection
25
+ @connection ||= Connection.new(info)
26
+ end
27
+
28
+ def validator
29
+ @validator ||= Validator.new(info)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,43 @@
1
+ module Shipcloud
2
+ module Request
3
+ class Connection
4
+ attr_reader :https
5
+
6
+ def initialize(request_info)
7
+ @info = request_info
8
+ end
9
+
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
16
+ end
17
+
18
+ def request
19
+ https.start do |connection|
20
+ https.request(https_request)
21
+ end
22
+ end
23
+
24
+ protected
25
+
26
+ def https_request
27
+ https_request = case @info.http_method
28
+ when :post
29
+ Net::HTTP::Post.new(@info.url, API_HEADERS)
30
+ when :put
31
+ Net::HTTP::Put.new(@info.url, API_HEADERS)
32
+ when :delete
33
+ Net::HTTP::Delete.new(@info.url, API_HEADERS)
34
+ else
35
+ Net::HTTP::Get.new(@info.path_with_params(@info.url, @info.data), API_HEADERS)
36
+ end
37
+ https_request.basic_auth(Shipcloud.api_key, "")
38
+ https_request.body = @info.data.to_json if [:post, :put].include?(@info.http_method)
39
+ https_request
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,27 @@
1
+ module Shipcloud
2
+ module Request
3
+ class Info
4
+ attr_accessor :http_method, :api_url, :data
5
+
6
+ def initialize(http_method, api_url, data)
7
+ @http_method = http_method
8
+ @api_url = api_url
9
+ @data = data
10
+ end
11
+
12
+ def url
13
+ url = "/#{API_VERSION}/#{api_url}"
14
+ url
15
+ end
16
+
17
+ def path_with_params(path, params)
18
+ unless params.empty?
19
+ encoded_params = URI.encode_www_form(params)
20
+ [path, encoded_params].join("?")
21
+ else
22
+ path
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+ module Shipcloud
2
+ module Request
3
+ class Validator
4
+ attr_reader :info
5
+ attr_accessor :response
6
+
7
+ def initialize(info)
8
+ @info = info
9
+ end
10
+
11
+ def validated_data_for(incoming_response)
12
+ self.response = incoming_response
13
+ verify_response_code
14
+ info.data = JSON.parse(response.body)
15
+ validate_response_data
16
+ info.data
17
+ end
18
+
19
+ protected
20
+
21
+ def verify_response_code
22
+ raise AuthenticationError if response.code.to_i == 401
23
+ raise APIError if response.code.to_i >= 500
24
+ end
25
+
26
+ def validate_response_data
27
+ raise APIError.new(info.data["error"]) if info.data["error"]
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ module Shipcloud
2
+ class Shipment < Base
3
+
4
+ attr_accessor :to, :carrier, :package
5
+ attr_reader :id, :tracking_url, :label_url, :price
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Shipcloud
2
+ VERSION = "0.1.0"
3
+ end
data/shipcloud.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'shipcloud/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "shipcloud"
8
+ spec.version = Shipcloud::VERSION
9
+ spec.authors = ["sthollmann"]
10
+ spec.email = ["stefan@webionate.de"]
11
+ spec.description = %q{A wrapper for the shipcloud API}
12
+ spec.summary = %q{A wrapper for the shipcloud API}
13
+ spec.homepage = "https://github.com/webionate/shipcloud-ruby"
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 "json"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+
3
+ describe Shipcloud::Request::Base do
4
+ context "#perform" do
5
+ it "checks for an api key" do
6
+ Shipcloud.stub(:api_key).and_return(nil)
7
+
8
+ expect{
9
+ Shipcloud::Request::Base.new(nil).perform
10
+ }.to raise_error Shipcloud::AuthenticationError
11
+ end
12
+
13
+ it "performs an https request" do
14
+ Shipcloud.stub(:api_key).and_return("some key")
15
+ connection = stub
16
+ validator = stub
17
+ Shipcloud::Request::Connection.stub(:new).and_return(connection)
18
+ Shipcloud::Request::Validator.stub(:new).and_return(validator)
19
+
20
+ connection.should_receive(:setup_https)
21
+ connection.should_receive(:request)
22
+ validator.should_receive(:validated_data_for)
23
+
24
+ Shipcloud::Request::Base.new(nil).perform
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe Shipcloud::Request::Connection do
4
+ describe "#setup_https" do
5
+ it "creates a https object" do
6
+ connection = Shipcloud::Request::Connection.new(nil)
7
+
8
+ connection.setup_https
9
+
10
+ connection.https.should_not be_nil
11
+ end
12
+ end
13
+
14
+ describe "#request" do
15
+ it "performs the actual request" do
16
+ connection = Shipcloud::Request::Connection.new(nil)
17
+ connection.setup_https
18
+ connection.stub(:https_request)
19
+
20
+ connection.https.should_receive(:request)
21
+
22
+ connection.request
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe Shipcloud::Request::Validator do
4
+ describe "#validated_data_for" do
5
+ it "validates the data" do
6
+ info = Shipcloud::Request::Info.new(:get, "random", OpenStruct.new(id: 1))
7
+ validator = Shipcloud::Request::Validator.new info
8
+ response = OpenStruct.new(body: '{"response":"ok"}', code: 200)
9
+
10
+ validator.validated_data_for(response).should eq "response" => "ok"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shipcloud::Shipment do
4
+ let(:valid_attributes) do
5
+ {
6
+ to: {
7
+ company: 'shipcloud GmbH',
8
+ first_name: 'Max',
9
+ last_name: 'Mustermann',
10
+ street: 'Musterallee',
11
+ street_no: '43',
12
+ city: 'Berlin',
13
+ zip_code: '10000',
14
+ },
15
+ carrier: 'dhl',
16
+ package: {
17
+ weight: 2.5,
18
+ length: 40,
19
+ width: 20,
20
+ height: 20
21
+ }
22
+ }
23
+ end
24
+
25
+ let(:shipment) {
26
+ Shipcloud::Shipment.new(valid_attributes)
27
+ }
28
+
29
+ describe "#initialize" do
30
+ it "initializes all attributes correctly" do
31
+ expect(shipment.to[:company]).to eq 'shipcloud GmbH'
32
+ expect(shipment.to[:first_name]).to eq 'Max'
33
+ expect(shipment.to[:last_name]).to eq 'Mustermann'
34
+ expect(shipment.to[:street]).to eq 'Musterallee'
35
+ expect(shipment.to[:street_no]).to eq '43'
36
+ expect(shipment.to[:city]).to eq 'Berlin'
37
+ expect(shipment.to[:zip_code]).to eq '10000'
38
+
39
+ expect(shipment.carrier).to eq 'dhl'
40
+
41
+ expect(shipment.package[:weight]).to eq 2.5
42
+ expect(shipment.package[:length]).to eq 40
43
+ expect(shipment.package[:width]).to eq 20
44
+ expect(shipment.package[:height]).to eq 20
45
+ end
46
+ end
47
+
48
+ describe ".create" do
49
+ it "makes a new POST request using the correct API endpoint" do
50
+ Shipcloud.should_receive(:request).with(:post, "shipments", valid_attributes).and_return("data" => {})
51
+ Shipcloud::Shipment.create(valid_attributes)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,33 @@
1
+ require "spec_helper"
2
+
3
+ describe Shipcloud do
4
+ describe ".request" do
5
+ context "given no api key exists" do
6
+ it "raises an authentication error" do
7
+ expect { Shipcloud.request(:get, "clients", {}) }.to raise_error(Shipcloud::AuthenticationError)
8
+ end
9
+ end
10
+
11
+ context "with an invalid api key" do
12
+ before(:each) do
13
+ WebMock.stub_request(:any, /#{Shipcloud::API_BASE}/).to_return(:body => "{}")
14
+ Shipcloud.api_key = "your-api-key"
15
+ end
16
+
17
+ it "attempts to get a url with one param" do
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")
20
+ end
21
+
22
+ it "attempts to get a url with more than one param" do
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")
25
+ end
26
+
27
+ it "doesn't add a question mark if no params" do
28
+ Shipcloud.request(:get, "transactions", {})
29
+ WebMock.should have_requested(:get, "https://#{Shipcloud::api_key}:@#{Shipcloud::API_BASE}/#{Shipcloud::API_VERSION}/transactions")
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+ require 'shipcloud'
4
+ require 'rspec'
5
+ require "webmock/rspec"
6
+ require "pry"
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shipcloud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - sthollmann
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: A wrapper for the shipcloud API
79
+ email:
80
+ - stefan@webionate.de
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - lib/shipcloud.rb
92
+ - lib/shipcloud/base.rb
93
+ - lib/shipcloud/operations/create.rb
94
+ - lib/shipcloud/request/base.rb
95
+ - lib/shipcloud/request/connection.rb
96
+ - lib/shipcloud/request/info.rb
97
+ - lib/shipcloud/request/validator.rb
98
+ - lib/shipcloud/shipment.rb
99
+ - lib/shipcloud/version.rb
100
+ - shipcloud.gemspec
101
+ - spec/shipcloud/request/base_spec.rb
102
+ - spec/shipcloud/request/connection_spec.rb
103
+ - spec/shipcloud/request/validator_spec.rb
104
+ - spec/shipcloud/shipment_spec.rb
105
+ - spec/shipcloud_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: https://github.com/webionate/shipcloud-ruby
108
+ licenses:
109
+ - MIT
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 1.8.25
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: A wrapper for the shipcloud API
132
+ test_files:
133
+ - spec/shipcloud/request/base_spec.rb
134
+ - spec/shipcloud/request/connection_spec.rb
135
+ - spec/shipcloud/request/validator_spec.rb
136
+ - spec/shipcloud/shipment_spec.rb
137
+ - spec/shipcloud_spec.rb
138
+ - spec/spec_helper.rb
139
+ has_rdoc: