shiphawk 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 736c27fe2f846958539936391e0a082fa381e96e
4
+ data.tar.gz: ae41dfee86cd09abea89dab786e99195fe28f332
5
+ SHA512:
6
+ metadata.gz: 461e92ec608a618284836d16e23d687238c13f2dcdce32235f3fc8831fe4af925a12565921e17840b5e69e320a6ed0de50fcc3c785338dd169d82a6c3a79bfe2
7
+ data.tar.gz: f4f20d8e2ede02b7a4658813afca83b305dab6db75676a7559d3607b853f1fd93900d256a91cf07d261f3c49d50b27aebf449daa7129441b31e11a50896564ea
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ /.idea
16
+ .ruby-version
17
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
@@ -0,0 +1,155 @@
1
+ # Shiphawk::Client Gem Examples
2
+
3
+ ## Client Initialization
4
+
5
+ To make a connection to a ShipHawk server, create a Client connection:
6
+
7
+ ```ruby
8
+ require 'rubygems'
9
+ require 'shiphawk'
10
+
11
+ # get an item based on item id
12
+ client = Shiphawk::Client.new(api_token: 'api_token')
13
+ ```
14
+
15
+ Shiphawk::Client initialize takes a parameter hash to configure a connection. The parameters hash can consistent of:
16
+
17
+ ```ruby
18
+ PRODUCTION_API_HOST = 'https://shiphawk.com'
19
+ DEFAULT_API_VERSION = 'v3'
20
+
21
+ @api_token = @options.delete(:api_token) { |key| Shiphawk.api_token }
22
+ @api_version = @options.delete(:api_version) { |key| DEFAULT_API_VERSION }
23
+ @host_url = @options.delete(:host_url) { |key| PRODUCTION_API_HOST }
24
+ @adapter = @options.delete(:adapter) { |key| Faraday.default_adapter }
25
+ ```
26
+
27
+ ## Items retrieval and searching
28
+
29
+ ### Item retrieval
30
+
31
+ Here's an example of retrieving an Item given the Item ID using the Shiphawk::Client API
32
+
33
+ ```ruby
34
+ require 'rubygems'
35
+ require 'shiphawk'
36
+
37
+ # get an item based on item id
38
+ client = Shiphawk::Client.new(api_token: 'api_token')
39
+ client.items_show 2335
40
+ ```
41
+
42
+ ## Notifications
43
+
44
+ Currently undocumented on Shiphawk's api guide, so please disregard for now.
45
+
46
+ ```ruby
47
+ require 'rubygems'
48
+ require 'shiphawk'
49
+
50
+ # get an item based on item id
51
+ client = Shiphawk::Client.new(api_token: 'api_token')
52
+ ```
53
+
54
+ ## Products
55
+
56
+ Implemented but undocumented. Not sure how this is supposed to work.
57
+
58
+ ```ruby
59
+ require 'rubygems'
60
+ require 'shiphawk'
61
+
62
+ # get an item based on item id
63
+ client = Shiphawk::Client.new(api_token: 'api_token')
64
+ ```
65
+
66
+ ## Rates
67
+
68
+ Get a shipping quote. Documentation here: https://shiphawk.com/api-docs#!/rates/POST_api_version_rates_format
69
+
70
+ Creating a quote
71
+ ```ruby
72
+ require 'rubygems'
73
+ require 'shiphawk'
74
+
75
+ client = Shiphawk::Client.new(api_token: 'api_token')
76
+
77
+ custom_attributes = {
78
+ value: declared_value_per_lot, quantity: lot_size, id: shiphawk_item_id,
79
+ width: width, length: depth, height: height, packed: is_packed?,
80
+ weight: shipping_weight_in_pounds, name: name, xid: barcode,
81
+ }
82
+
83
+ client.create_rate(data.merge(custom_attributes))
84
+
85
+ ```
86
+
87
+ ## Shipment Notes
88
+
89
+ Create customer note about a shipment
90
+
91
+ ```ruby
92
+ require 'rubygems'
93
+ require 'shiphawk'
94
+
95
+ Shiphawk.notes_update(shipment_id, {body: "This is a classy note"})
96
+ ```
97
+
98
+ ## Shipment Tracking
99
+
100
+ Subscribe to tracking notifications for a shipment.
101
+
102
+ ```ruby
103
+ require 'rubygems'
104
+ require 'shiphawk'
105
+
106
+ Shiphawk.shipments_status_update(shipment_id, {callback_url: "www.someawesomecallback.com/shipment-tracking-callback/#{shipment_id}"})
107
+ ```
108
+
109
+ ## Shipments
110
+
111
+ Create a shipment
112
+
113
+ ```ruby
114
+ require 'rubygems'
115
+ require 'shiphawk'
116
+
117
+ data = {
118
+ xid: buyer_invoice.to_s,
119
+ rate_id: shiphawk_quote_id,
120
+ destination_address:
121
+ {
122
+ first_name: user.first_name, last_name: user.last_name,
123
+ company: name, phone_num: user.phone, email: user.email,
124
+ address_line_1: address, address_line_2: floor_suite,
125
+ city: city, state: state, zipcode: zip
126
+ },
127
+ billing_address: shiphawk_region_rep,
128
+ origin_contact: shiphawk_region_rep,
129
+ accessorials: [],
130
+ order_email: shiphawk_region_rep[:email],
131
+ pickup_data: pickup_data
132
+ }
133
+
134
+ Shiphawk.shipments_create(data)
135
+ ```
136
+
137
+ ## Shipments Status
138
+
139
+ Update a number of shipments at once (need to confirm)
140
+
141
+ ```ruby
142
+ require 'rubygems'
143
+ require 'shiphawk'
144
+ Shiphawk.shipments_status_update({shipment_ids: [1,2,3], status: "in_transit"})
145
+ ```
146
+
147
+ ## Zip Codes
148
+
149
+ Get all zip codes using pagination.
150
+
151
+ ```ruby
152
+ require 'rubygems'
153
+ require 'shiphawk'
154
+ Shiphawk.zip_codes_index
155
+ ```
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shiphawk-ruby.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Hawk Applications Corp.
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.
@@ -0,0 +1,42 @@
1
+ # Shiphawk::Client
2
+
3
+ Ruby wrapper for the ShipHawk V3 API. The ShipHawk::Client gem provides an easy-to-use wrapper for ShipHawk's REST APIs.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'shiphawk'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install shiphawk
20
+
21
+ ## Usage
22
+
23
+ [View the Examples](EXAMPLES.md)
24
+
25
+ ## End points
26
+
27
+ ## Errors and Exceptions
28
+
29
+ When an *error status* is received from the server, the gem will raise an error on the client side:
30
+
31
+ * 400 raises Shiphawk::Errors::GeneralError
32
+ * 401 raises Shiphawk::Errors::UnauthorizedError
33
+ * 403 raises Shiphawk::Errors::AccessDeniedError
34
+ * 404 raises Shiphawk::Errors::NotFoundError
35
+ * 422 raises Shiphawk::Errors::UnprocessableEntityError
36
+ * 500 raises Shiphawk::Errors::InformShiphawkError
37
+ * 502 raises Shiphawk::Errors::UnavailableError
38
+ * 503 raises Shiphawk::Errors::UnavailableError
39
+
40
+ ## Copyright
41
+
42
+ Copyright (c) 2015 Hawk Applications Corp. See LICENSE for details.
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,5 @@
1
+ === 0.8.1 2015-11-03
2
+
3
+ * Remove .gem file from repo (it's redundant and could get out of sync with the releases).
4
+ * Fix README and EXAMPLES from referencing shiphawk-ruby to just shiphawk.
5
+ * Fix copyright.
@@ -0,0 +1,43 @@
1
+ module Shiphawk
2
+
3
+ class << self
4
+ # config/initializers/shiphawk.rb (for instance)
5
+ #
6
+ # ```ruby
7
+ # Shiphawk.configure do |config|
8
+ # config.token = 'api_token'
9
+ # end
10
+ # ```
11
+ # elsewhere
12
+ #
13
+ # ```ruby
14
+ # client = ShiphawkClient::Client.new
15
+ # ```
16
+ #
17
+ # You can also chose to use the class as a singleton
18
+ #
19
+ # ```ruby
20
+ # Shiphawk.{method_name}
21
+ # ```
22
+
23
+ def configure
24
+ yield self
25
+ true
26
+ end
27
+
28
+ def method_missing(method, *args, &block)
29
+ return super unless self::Client.new.respond_to?(method)
30
+ self::Client.new.send(method, *args, &block)
31
+ end
32
+
33
+ attr_accessor :api_token, :sandbox
34
+ end
35
+
36
+ autoload :Api, 'shiphawk/api'
37
+ autoload :Client, 'shiphawk/client'
38
+ autoload :Mash, 'shiphawk/mash'
39
+ autoload :Errors, 'shiphawk/errors'
40
+ autoload :Helpers, 'shiphawk/helpers'
41
+ autoload :Version, 'shiphawk/version'
42
+
43
+ end
@@ -0,0 +1,20 @@
1
+ module Shiphawk
2
+ module Api
3
+
4
+ attr_accessor :params
5
+
6
+ # load dependent code
7
+ autoload :QueryHelpers, 'shiphawk/api/query_helpers'
8
+ autoload :Items, 'shiphawk/api/items' # get /, get /search, get /:id
9
+ autoload :Categories, 'shiphawk/api/categories' # get /, get /:id, post /, put /, delete /
10
+ autoload :Products, 'shiphawk/api/products' # get /:id, post /, put /, delete /
11
+ autoload :Rates, 'shiphawk/api/rates' # get :id, post /
12
+ autoload :ShipmentNotes, 'shiphawk/api/shipment_notes' # get /:id/notes, post /:id/notes
13
+ autoload :ShipmentTracking, 'shiphawk/api/shipment_tracking' # get /:id/tracking, put /:id/tracking
14
+ autoload :Shipments, 'shiphawk/api/shipments' # get /, get :id, get :id/bol, get :id/bol.pdf, post /, put :id, delete :id
15
+ autoload :ShipmentsStatus, 'shiphawk/api/shipments_status' # put /
16
+ autoload :ZipCodes, 'shiphawk/api/zip_codes' # get /, get /search
17
+ autoload :Notifications, 'shiphawk/api/notifications' # post /
18
+
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module Shiphawk
2
+ module Api
3
+
4
+ # ApiKeys API
5
+ #
6
+ # @see https://shiphawk.com/api-docs
7
+ #
8
+ # The following API actions provide the CRUD interface to managing api keys.
9
+ #
10
+ module ApiKeys
11
+
12
+ # Get all API keys for an account using an API key
13
+ def api_keys_index
14
+ get_request api_keys_path, {}
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,34 @@
1
+ module Shiphawk
2
+ module Api
3
+
4
+ # Categories API
5
+ #
6
+ # @see https://shiphawk.com/api-docs
7
+ #
8
+ # The following API actions provide the CRUD interface to managing categories.
9
+ #
10
+ module Categories
11
+
12
+ def categories_index
13
+ collection_request categories_path, 500
14
+ end
15
+
16
+ def categories_show category_id
17
+ entity_request_with_id categories_path, category_id
18
+ end
19
+
20
+ def categories_create options
21
+ post_request categories_path, options
22
+ end
23
+
24
+ def categories_update category_id, options
25
+ put_request categories_path(category_id), options
26
+ end
27
+
28
+ def categories_destroy category_id
29
+ delete_request categories_path, id: category_id
30
+ end
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,27 @@
1
+ module Shiphawk
2
+ module Api
3
+
4
+ # Items API
5
+ #
6
+ # @see https://shiphawk.com/api-docs
7
+ #
8
+ # The following API actions provide the CRUD interface to managing items.
9
+ #
10
+ module Items
11
+
12
+ def items_index
13
+ collection_request items_path, 500
14
+ end
15
+
16
+ def items_show item_id
17
+ entity_request_with_id items_path, item_id
18
+ end
19
+
20
+ def items_search options
21
+ entity_request_with_options items_path('search'), q: options.fetch(:q, '')
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ module Shiphawk
2
+ module Api
3
+
4
+ # Items API
5
+ #
6
+ # @see https://shiphawk.com/api-docs
7
+ #
8
+ # The following API actions provide the CRUD interface to managing items.
9
+ #
10
+ module Notifications
11
+
12
+ def catalog_sale_create options
13
+ post_request catalog_sale_path, options
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ module Shiphawk
2
+ module Api
3
+
4
+ # Products API
5
+ #
6
+ # @see https://shiphawk.com/api-docs
7
+ #
8
+ # The following API actions provide the CRUD interface to managing products.
9
+ #
10
+ module Products
11
+
12
+ def products_show product_sku
13
+ entity_request_with_id products_path, product_sku
14
+ end
15
+
16
+ def products_create options
17
+ post_request products_path, options
18
+ end
19
+
20
+ def products_update product_sku, options
21
+ put_request products_path(product_sku), options
22
+ end
23
+
24
+ def products_destroy product_sku
25
+ delete_request products_path(product_sku), {}
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,74 @@
1
+ module Shiphawk
2
+ module Api
3
+
4
+ module QueryHelpers
5
+
6
+ def items_path sub_path=nil
7
+ "items/#{sub_path}"
8
+ end
9
+
10
+ def categories_path sub_path=nil
11
+ "categories/#{sub_path}"
12
+ end
13
+
14
+ def products_path sub_path=nil
15
+ "products/#{sub_path}"
16
+ end
17
+
18
+ def rates_path sub_path=nil
19
+ "rates/#{sub_path}"
20
+ end
21
+
22
+ def notes_path id
23
+ shipments_path "#{id}/notes"
24
+ end
25
+
26
+ def tracking_path id
27
+ shipments_path "#{id}/tracking"
28
+ end
29
+
30
+ def shipments_path sub_path=nil
31
+ "shipments/#{sub_path}"
32
+ end
33
+
34
+ def status_path
35
+ shipments_path "status"
36
+ end
37
+
38
+ def notifications_path sub_path=nil
39
+ "notifications/#{sub_path}"
40
+ end
41
+
42
+ def catalog_sale_path
43
+ notifications_path "catalog_sale"
44
+ end
45
+
46
+ def zip_codes_path sub_path=nil
47
+ "zip_codes/#{sub_path}"
48
+ end
49
+
50
+ def collection_request path, count=100
51
+ page = 1
52
+ entities = []
53
+ loop do
54
+ response = get_request path, {page: page, per_page: count, api_token: self.api_token}
55
+ entities << response.body
56
+ entities = entities.flatten
57
+ break if entities.size >= response.headers['X-Total'].to_i
58
+ page += 1
59
+ end
60
+ entities
61
+ end
62
+
63
+ def entity_request_with_id path, id
64
+ get_request "#{path}#{id}", {}
65
+ end
66
+
67
+ def entity_request_with_options path, options
68
+ get_request path, options
69
+ end
70
+
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,25 @@
1
+ module Shiphawk
2
+ module Api
3
+
4
+ # Rates API
5
+ #
6
+ # @see https://shiphawk.com/api-docs
7
+ #
8
+ # The following API actions provide the CRUD interface to managing rates.
9
+ #
10
+ module Rates
11
+
12
+ # Retrieve rates using a Rates ID key
13
+ def rates_show rate_id
14
+ entity_request_with_id rates_path, rate_id
15
+ end
16
+
17
+ # Obtain rates given a source
18
+ def rates_create options
19
+ post_request rates_path, options
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ module Shiphawk
2
+ module Api
3
+
4
+ # Company API
5
+ #
6
+ # @see https://shiphawk.com/api-docs
7
+ #
8
+ # The following API actions provide the CRUD interface to managing a company.
9
+ #
10
+ module ShipmentNotes
11
+
12
+ def shipment_notes_show shipment_id
13
+ get_request notes_path(shipment_id), {}
14
+ end
15
+
16
+ def shipment_notes_update shipment_id, options
17
+ put_request notes_path(shipment_id), options
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module Shiphawk
2
+ module Api
3
+
4
+ # Company API
5
+ #
6
+ # @see https://shiphawk.com/api-docs
7
+ #
8
+ # The following API actions provide the CRUD interface to managing multiple shipments by setting status.
9
+ #
10
+ module ShipmentTracking
11
+
12
+ def shipments_status_show shipment_id
13
+ get_request tracking_path(shipment_id), {}
14
+ end
15
+
16
+ def shipments_status_update shipment_id, options
17
+ put_request tracking_path(shipment_id), options
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,43 @@
1
+ module Shiphawk
2
+ module Api
3
+
4
+ # Shipments API
5
+ #
6
+ # @see https://shiphawk.com/api-docs
7
+ #
8
+ # The following API actions provide the CRUD interface to managing Shipments.
9
+ #
10
+ module Shipments
11
+
12
+ def shipments_index
13
+ collection_request shipments_path, 500
14
+ end
15
+
16
+ def shipments_show shipment_id
17
+ entity_request_with_id shipments_path, shipment_id
18
+ end
19
+
20
+ def shipments_bol shipment_id
21
+ get_request shipments_path("#{shipment_id}/bol"), {}
22
+ end
23
+
24
+ def shipments_bol_pdf shipment_id
25
+ get_request shipments_path("#{shipment_id}/bol.pdf"), {}
26
+ end
27
+
28
+ def shipments_create options
29
+ post_request shipments_path, options
30
+ end
31
+
32
+ def shipments_update shipment_id, options
33
+ put_request shipments_path(shipment_id), options
34
+ end
35
+
36
+ def shipments_destroy shipment_id
37
+ delete_request shipments_path(shipment_id), {}
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ module Shiphawk
2
+ module Api
3
+
4
+ # Company API
5
+ #
6
+ # @see https://shiphawk.com/api-docs
7
+ #
8
+ # The following API actions provide the CRUD interface to managing a shipment's tracking.
9
+ #
10
+ module ShipmentsStatus
11
+
12
+ def shipments_status_update options
13
+ put_request status_path, options
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module Shiphawk
2
+ module Api
3
+
4
+ # Zip Codes API
5
+ #
6
+ # @see https://shiphawk.com/api-docs
7
+ #
8
+ # The following API actions provide the CRUD interface to managing zip codes.
9
+ #
10
+ module ZipCodes
11
+
12
+ def zip_codes_index
13
+ collection_request zip_codes_path, 1000
14
+ end
15
+
16
+ def zip_codes_search options
17
+ entity_request_with_options zip_codes_path('search'), q: options.fetch(:q, '')
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ module Shiphawk
2
+ class Client
3
+
4
+ PRODUCTION_API_HOST = 'https://shiphawk.com'
5
+ SANDBOX_API_HOST = 'https://sandbox.shiphawk.com'
6
+ DEFAULT_API_VERSION = 'v3'
7
+
8
+ include Shiphawk::Helpers::Request
9
+ include Shiphawk::Api::QueryHelpers
10
+ include Shiphawk::Api::Items
11
+ include Shiphawk::Api::Rates
12
+ include Shiphawk::Api::ShipmentNotes
13
+ include Shiphawk::Api::ShipmentTracking
14
+ include Shiphawk::Api::Shipments
15
+ include Shiphawk::Api::ShipmentsStatus
16
+ include Shiphawk::Api::ZipCodes
17
+ include Shiphawk::Api::Notifications
18
+
19
+ attr_reader :api_token, :options, :sandbox
20
+
21
+ def initialize(options={api_token: Shiphawk.api_token})
22
+ host = Shiphawk.sandbox ? SANDBOX_API_HOST : PRODUCTION_API_HOST
23
+ @options = options
24
+ @api_token = @options.delete(:api_token) { |key| Shiphawk.api_token }
25
+ @api_version = @options.delete(:api_version) { |key| DEFAULT_API_VERSION }
26
+ @host_url = @options.delete(:host_url) { |key| host }
27
+ @adapter = @options.delete(:adapter) { |key| Faraday.default_adapter }
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,34 @@
1
+ module Shiphawk
2
+ module Errors
3
+
4
+ class ShiphawkError < StandardError
5
+ attr_reader :data
6
+ def initialize(data)
7
+ @data = data
8
+ super
9
+ end
10
+ end
11
+
12
+ # Raised when a 401 response status code is received
13
+ class UnauthorizedError < ShiphawkError; end
14
+
15
+ # Raised when a 400 response status code is received
16
+ class GeneralError < ShiphawkError; end
17
+
18
+ # Raised when a 403 response status code is received
19
+ class AccessDeniedError < ShiphawkError; end
20
+
21
+ # Raised when a 404 response status code is received
22
+ class NotFoundError < ShiphawkError; end
23
+
24
+ # Raised when a 422 response status code is received
25
+ class UnprocessableEntityError < ShiphawkError; end
26
+
27
+ # Raised when a 500 response status code is received
28
+ class InformShiphawkError < ShiphawkError; end
29
+
30
+ # Raised when a 502 or 503 response status code is received
31
+ class UnavailableError < ShiphawkError; end
32
+
33
+ end
34
+ end
@@ -0,0 +1,6 @@
1
+ module Shiphawk
2
+ module Helpers
3
+ autoload :Request, 'shiphawk/helpers/request'
4
+ autoload :Response, 'shiphawk/helpers/response'
5
+ end
6
+ end
@@ -0,0 +1,70 @@
1
+ require 'faraday_middleware'
2
+ require 'faraday/conductivity'
3
+
4
+ module Shiphawk
5
+ module Helpers
6
+
7
+ module Request
8
+
9
+ APP_VERSION = IO.popen(['git', 'rev-parse', 'HEAD', :chdir => Rails.root]).read.chomp
10
+
11
+ DEFAULT_HEADERS = {
12
+ 'x-li-format' => 'json'
13
+ }
14
+
15
+ API_PATH = '/api/'
16
+
17
+ attr_reader :connection, :host_url, :adapter, :api_version, :api_token
18
+
19
+ %w(get post put patch delete).each do |verb|
20
+ define_method("#{verb}_request") do |endpoint, params|
21
+ conn = get_connection
22
+ response = conn.send(verb, "#{API_PATH}#{api_version}/#{endpoint}", params)
23
+ raise_errors response
24
+ response
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def get_connection
31
+ raise ArgumentError, 'Incorrect connection information' if host_url.nil? || adapter.nil?
32
+ @connection ||= Faraday.new(url: host_url) do |conn|
33
+ conn.request :multipart
34
+ conn.request :url_encoded
35
+
36
+ conn.request :user_agent, app: 'ShipHawk Client', version: APP_VERSION
37
+ conn.request :request_id
38
+ conn.request :request_headers, accept: 'application/json', 'X-API-KEY' => api_token
39
+
40
+ conn.use :extended_logging, logger: Rails.logger
41
+
42
+ conn.response :json, content_type: /\bjson$/
43
+
44
+ conn.adapter adapter
45
+ end
46
+ end
47
+
48
+ def raise_errors response
49
+ case response.status.to_i
50
+ when 401
51
+ fail Shiphawk::Errors::UnauthorizedError, "(#{response.status}): #{response.body['error']}"
52
+ when 400
53
+ fail Shiphawk::Errors::GeneralError, "(#{response.status}): #{response.body['error']}"
54
+ when 403
55
+ fail Shiphawk::Errors::AccessDeniedError, "(#{response.status}): #{response.body['error']}"
56
+ when 404
57
+ fail Shiphawk::Errors::NotFoundError, "(#{response.status}): #{response.body['error']}"
58
+ when 422
59
+ fail Shiphawk::Errors::UnprocessableEntityError, "(#{response.status}): #{response.body['error']}"
60
+ when 500
61
+ fail Shiphawk::Errors::InformShiphawkError, "ShipHawk had an internal error. Please send an email to api@shiphawk.com. (#{response.status}): #{response.body['error']}"
62
+ when 502..503
63
+ fail Shiphawk::Errors::UnavailableError, "(#{response.status}): #{response.body['error']}"
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,9 @@
1
+ module Shiphawk
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 8
5
+ PATCH = 1
6
+ PRE = nil
7
+ STRING = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
8
+ end
9
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'shiphawk/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'shiphawk'
8
+ spec.version = Shiphawk::VERSION::STRING
9
+ spec.authors = ['Robert Schmitt']
10
+ spec.email = ['bob.schmitt@shiphawk.com']
11
+ spec.summary = %q{Ruby bindings for the ShipHawk API}
12
+ spec.description = %q{ShipHawk, technology that delivers. See https://shiphawk.com for details.}
13
+ spec.homepage = 'https://github.com/ShipHawk/shiphawk-ruby'
14
+ spec.license = 'MIT'
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'faraday'
23
+ spec.add_dependency 'faraday_middleware'
24
+ spec.add_dependency 'faraday-conductivity'
25
+ spec.add_dependency 'httpclient'
26
+ spec.add_dependency 'typhoeus'
27
+ spec.add_dependency 'em-http-request'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 1.9.4'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'rspec'
32
+ spec.add_development_dependency 'webmock'
33
+ spec.add_development_dependency 'vcr'
34
+ spec.add_development_dependency 'pry'
35
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shiphawk::Api::Rates do
4
+
5
+ context 'POST to rates' do
6
+
7
+ before do
8
+ VCR.insert_cassette 'rates_create'
9
+ end
10
+
11
+ after do
12
+ VCR.eject_cassette
13
+ end
14
+
15
+ it 'returns rates based on rquest data' do
16
+ expect(true).to eql(true)
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,9 @@
1
+ require 'shiphawk'
2
+ require 'webmock'
3
+ require 'vcr'
4
+ require 'faraday'
5
+
6
+ VCR.configure do |c|
7
+ c.cassette_library_dir = 'spec/cassettes'
8
+ c.hook_into :webmock
9
+ end
metadata ADDED
@@ -0,0 +1,244 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shiphawk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.1
5
+ platform: ruby
6
+ authors:
7
+ - Robert Schmitt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday-conductivity
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httpclient
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: typhoeus
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: em-http-request
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.9.4
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.9.4
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '10.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '10.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: webmock
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: vcr
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: pry
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ description: ShipHawk, technology that delivers. See https://shiphawk.com for details.
182
+ email:
183
+ - bob.schmitt@shiphawk.com
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".gitignore"
189
+ - ".rspec"
190
+ - EXAMPLES.md
191
+ - Gemfile
192
+ - LICENSE.txt
193
+ - README.md
194
+ - Rakefile
195
+ - history.txt
196
+ - lib/shiphawk.rb
197
+ - lib/shiphawk/api.rb
198
+ - lib/shiphawk/api/api_keys.rb
199
+ - lib/shiphawk/api/categories.rb
200
+ - lib/shiphawk/api/items.rb
201
+ - lib/shiphawk/api/notifications.rb
202
+ - lib/shiphawk/api/products.rb
203
+ - lib/shiphawk/api/query_helpers.rb
204
+ - lib/shiphawk/api/rates.rb
205
+ - lib/shiphawk/api/shipment_notes.rb
206
+ - lib/shiphawk/api/shipment_tracking.rb
207
+ - lib/shiphawk/api/shipments.rb
208
+ - lib/shiphawk/api/shipments_status.rb
209
+ - lib/shiphawk/api/zip_codes.rb
210
+ - lib/shiphawk/client.rb
211
+ - lib/shiphawk/errors.rb
212
+ - lib/shiphawk/helpers.rb
213
+ - lib/shiphawk/helpers/request.rb
214
+ - lib/shiphawk/version.rb
215
+ - shiphawk.gemspec
216
+ - spec/shiphawk/api/rates_spec.rb
217
+ - spec/spec_helper.rb
218
+ homepage: https://github.com/ShipHawk/shiphawk-ruby
219
+ licenses:
220
+ - MIT
221
+ metadata: {}
222
+ post_install_message:
223
+ rdoc_options: []
224
+ require_paths:
225
+ - lib
226
+ required_ruby_version: !ruby/object:Gem::Requirement
227
+ requirements:
228
+ - - ">="
229
+ - !ruby/object:Gem::Version
230
+ version: '0'
231
+ required_rubygems_version: !ruby/object:Gem::Requirement
232
+ requirements:
233
+ - - ">="
234
+ - !ruby/object:Gem::Version
235
+ version: '0'
236
+ requirements: []
237
+ rubyforge_project:
238
+ rubygems_version: 2.4.6
239
+ signing_key:
240
+ specification_version: 4
241
+ summary: Ruby bindings for the ShipHawk API
242
+ test_files:
243
+ - spec/shiphawk/api/rates_spec.rb
244
+ - spec/spec_helper.rb