easyship-rates-ruby-api 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 175eacb565636cc6b781053822beecfa8e7fb274
4
+ data.tar.gz: e0c738c96f7c98ece8dae119033803340beea7a2
5
+ SHA512:
6
+ metadata.gz: 726b84aa0df2a24ee31e0588bba6c2f354c2b1f15a823d6d5390c7d4fd66e72c0c66e0f35a6f5d281db9ee9f7ea7e4aeb98bd6a99041fd1847713f41d949e960
7
+ data.tar.gz: 7314dff470e1d06684f6377257c7a795617ada837ef8f4a6e38aef55f3a629ac2ea55cb13decc259acad149042b6ac456713043f3bbe2c2626ddbe615647e041
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .ruby-gemset
11
+ .ruby-version
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easyship-rates-ruby-api.gemspec
4
+ gemspec
@@ -0,0 +1,67 @@
1
+ # EasyshipRatesRubyApi
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'easyship-rates-ruby-api'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install easyship-rates-ruby-api
18
+
19
+ ## Usage
20
+
21
+ Create a file:
22
+
23
+ **config/initializers/easyship_rate_ruby_api.rb**
24
+
25
+ ```ruby
26
+ require 'easyship_rates_ruby_api'
27
+ EasyshipRatesRubyApi.configure do |config|
28
+ config.uid = 'RATE_CALCULATOR_APPID'
29
+ config.secret = 'RATE_CALCULATOR_SECRET'
30
+ end
31
+ ```
32
+
33
+ ### Get Rate
34
+
35
+ ```ruby
36
+ client = EasyshipRatesRubyApi::Client.new
37
+ client.get_token
38
+ client.get_rate(length: 1, width: 1, height: 1, origin_country_id: 96, destination_country_id: 199, total_actual_weight: 1)
39
+ ```
40
+
41
+
42
+ ### Get All Rates
43
+
44
+ ```ruby
45
+ client = EasyshipRatesRubyApi::Client.new
46
+ client.get_token
47
+ params = {
48
+ origin_country_id: 96,
49
+ destination_country_id: 199,
50
+ is_insured: true,
51
+ items: [
52
+ { category_id: 1, actual_weight: 30, length: 1, width: 1, height: 1, declared_customs_value: 100, declared_currency: 'HKD' }
53
+ ]
54
+ }
55
+ client.get_all_rates(params)
56
+ ```
57
+
58
+ ## Development
59
+
60
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
61
+
62
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/easyship/easyship-rates-ruby-api.
67
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "easyship/rates/ruby/api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easyship/rates/ruby/api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'easyship-rates-ruby-api'
8
+ spec.version = EasyshipRatesRubyApi::Version::STRING
9
+ spec.authors = ['aloha']
10
+ spec.email = ['y.alohac@gmail.com']
11
+
12
+ spec.summary = 'Ruby API for Rate Calculator'
13
+ spec.description = 'Ruby API for Rate Calculator'
14
+ spec.homepage = 'https://github.com/easyship/easyship-rates-ruby-api'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'oauth2', '~> 1.2'
22
+ spec.add_dependency 'hashie', '~> 3.4'
23
+ spec.add_dependency 'multi_json', '~> 1.11'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.12'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'minitest', '~> 5.0'
28
+ spec.add_development_dependency 'pry', '~> 0.10'
29
+ end
@@ -0,0 +1,6 @@
1
+ module EasyshipRatesRubyApi
2
+ module Api
3
+ autoload :QueryHelpers, 'easyship/rates/ruby/api/api/query_helpers'
4
+ autoload :Rates, 'easyship/rates/ruby/api/api/rates'
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ module EasyshipRatesRubyApi
2
+ module Api
3
+ module QueryHelpers
4
+ private
5
+
6
+ def simple_query(path, options = {})
7
+ headers = options.delete(:headers) || {}
8
+ params = to_query(options)
9
+ path += "#{path.include?('?') ? '&' : '?'}#{params}" if !params.empty?
10
+
11
+ Mash.from_json(get(path, headers))
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ module EasyshipRatesRubyApi
2
+ module Api
3
+ # Rate API
4
+ #
5
+ # @see https://github.com/easyship/easyship-rates Rate API
6
+ #
7
+ # [(contribute here)](https://github.com/easyship/easyship-rates-ruby-api)
8
+ module Rates
9
+ # Retrieve a Rate
10
+ #
11
+ # @param [Hash] options the options to get rate.
12
+ # @option options [String] :length length
13
+ # @option options [String] :width width
14
+ # @option options [String] :height height
15
+ # @option options [String] :origin_country_id origin_country_id
16
+ # @option options [String] :destination_country_id destination_country_id
17
+ # @option options [String] :total_actual_weight total_actual_weight
18
+ # @return [Easyship::Rates::Ruby::Mash]
19
+ def get_rate(options = {})
20
+ path = '/get_rates'
21
+ simple_query(path, options)
22
+ end
23
+
24
+ # Retrieve all detailed Rate
25
+ # @param [Hash] options the options to get rate.
26
+ # @option options [String] :origin_country_id
27
+ # @option options [String] :destination_country_id
28
+ # @option options [String] :is_insured
29
+ # @option options [Array<Hash>] :items
30
+ # @param [Hash] items the options to item detail.
31
+ # @option items [String] :category_id
32
+ # @option items [String] :actual_weight
33
+ # @option items [String] :length
34
+ # @option items [String] :width
35
+ # @option items [String] :height
36
+ # @option items [String] :declared_customs_value
37
+ # @option items [String] :declared_currency
38
+ # @return [Easyship::Rates::Ruby::Mash]
39
+ def get_all_rates(options = {})
40
+ path = '/get_all_rates'
41
+ defaults = {}
42
+ Mash.from_json(post(path, defaults.merge(options)).body)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,19 @@
1
+ require 'cgi'
2
+
3
+ module EasyshipRatesRubyApi
4
+ class Client
5
+ include Helpers::Request
6
+ include Helpers::Authorization
7
+ include Api::QueryHelpers
8
+ include Api::Rates
9
+
10
+ attr_reader :client_token, :client_secret, :client_options
11
+
12
+ def initialize(cuid = EasyshipRatesRubyApi.uid, csecret = EasyshipRatesRubyApi.secret, options = {})
13
+ @client_uid = cuid
14
+ @client_secret = csecret
15
+ raise EasyshipRatesRubyApi::Errors::ArgumentMissingError.new('please provide uid and secret') unless @client_uid && @client_secret
16
+ @client_options = options
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,32 @@
1
+ module EasyshipRatesRubyApi
2
+ module Errors
3
+ class RateCalculatorError < StandardError
4
+ attr_reader :data
5
+ def initialize(data)
6
+ @data = data
7
+ super
8
+ end
9
+ end
10
+
11
+
12
+ class ArgumentMissingError < StandardError; end
13
+
14
+ # Raised when a 401 response status code is received
15
+ class UnauthorizedError < RateCalculatorError; end
16
+
17
+ # Raised when a 400 response status code is received
18
+ class GeneralError < RateCalculatorError; end
19
+
20
+ # Raised when a 403 response status code is received
21
+ class AccessDeniedError < RateCalculatorError; end
22
+
23
+ # Raised when a 404 response status code is received
24
+ class NotFoundError < StandardError; end
25
+
26
+ # Raised when a 500 response status code is received
27
+ class InformRateCalculatorError < StandardError; end
28
+
29
+ # Raised when a 502 or 503 response status code is received
30
+ class UnavailableError < StandardError; end
31
+ end
32
+ end
@@ -0,0 +1,6 @@
1
+ module EasyshipRatesRubyApi
2
+ module Helpers
3
+ autoload :Authorization, 'easyship/rates/ruby/api/helpers/authorization'
4
+ autoload :Request, 'easyship/rates/ruby/api/helpers/request'
5
+ end
6
+ end
@@ -0,0 +1,45 @@
1
+ module EasyshipRatesRubyApi
2
+ module Helpers
3
+ module Authorization
4
+
5
+ DEFAULT_OAUTH_OPTIONS = {
6
+ staging_api_host: 'http://52.71.247.119',
7
+ production_api_host: '',
8
+ token_url: '/oauth2/token'
9
+ }
10
+
11
+ def client
12
+ @client ||= ::OAuth2::Client.new(@client_uid, @client_secret, parse_oauth_options)
13
+ end
14
+
15
+ def get_token(params = {})
16
+ @access_token ||= client.assertion.get_token(build_param(params))
17
+ end
18
+
19
+ def access_token
20
+ @access_token
21
+ end
22
+
23
+ private
24
+
25
+ # Since Rate Calculator using `/oauth2/token` instead of `/oauth/token`
26
+ def parse_oauth_options
27
+ {
28
+ site: @client_options[:site] || @client_options[:api_host] || (EasyshipRatesRubyApi.sandbox ? DEFAULT_OAUTH_OPTIONS[:staging_api_host] : DEFAULT_OAUTH_OPTIONS[:production_api_host]),
29
+ token_url: @client_options[:token_url] || DEFAULT_OAUTH_OPTIONS[:token_url]
30
+ }
31
+ end
32
+
33
+ def build_param(client_params)
34
+ {
35
+ iss: @client_uid,
36
+ aud: 'https://www.easyship-rate-calculator.com/oauth2/token',
37
+ prn: @client_uid,
38
+ exp: Time.now.to_i + 3600,
39
+ private_key: OpenSSL::PKey::RSA.new(@client_secret),
40
+ scope: 'get_rate get_all_rate me'
41
+ }.merge(client_params)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,66 @@
1
+ module EasyshipRatesRubyApi
2
+ module Helpers
3
+ module Request
4
+
5
+ API_PATH = '/v1'
6
+
7
+ DEFAULT_HEADERS = {}
8
+
9
+ protected
10
+
11
+ def get(path, options = {})
12
+ response = access_token.get("#{API_PATH}#{path}", DEFAULT_HEADERS.merge(options))
13
+ raise_errors(response)
14
+ response.body
15
+ end
16
+
17
+ def post(path, options = {})
18
+ response = access_token.post("#{API_PATH}#{path}", { body: options })
19
+ raise_errors(response)
20
+ response
21
+ end
22
+
23
+ private
24
+
25
+ def raise_errors(response)
26
+ case response.status.to_i
27
+ when 401
28
+ data = Mash.from_json(response.body)
29
+ raise EasyshipRatesRubyApi::Errors::UnauthorizedError.new(data), "(#{data.status}): #{data.message}"
30
+ when 400
31
+ data = Mash.from_json(response.body)
32
+ raise EasyshipRatesRubyApi::Errors::GeneralError.new(data), "(#{data.status}): #{data.message}"
33
+ when 403
34
+ data = Mash.from_json(response.body)
35
+ raise EasyshipRatesRubyApi::Errors::AccessDeniedError.new(data), "(#{data.status}): #{data.message}"
36
+ when 404
37
+ raise EasyshipRatesRubyApi::Errors::NotFoundError, "(#{response.code}): #{response.message}"
38
+ when 500
39
+ raise EasyshipRatesRubyApi::Errors::InformRateCalculatorError, "Rate Calculator had an internal error. Please let them know in the forum. (#{response.code}): #{response.message}"
40
+ when 502..503
41
+ raise EasyshipRatesRubyApi::Errors::UnavailableError, "(#{response.code}): #{response.message}"
42
+ end
43
+ end
44
+
45
+ # Stolen from Rack::Util.build_query
46
+ def to_query(params)
47
+ params.map { |k, v|
48
+ if v.class == Array
49
+ to_query(v.map { |x| [k, x] })
50
+ else
51
+ v.nil? ? escape(k) : "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
52
+ end
53
+ }.join('&')
54
+ end
55
+
56
+ def to_uri(path, options)
57
+ uri = URI.parse(path)
58
+
59
+ if options && options != {}
60
+ uri.query = to_query(options)
61
+ end
62
+ uri.to_s
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,17 @@
1
+ require 'hashie'
2
+ require 'multi_json'
3
+
4
+ module EasyshipRatesRubyApi
5
+ # The generalized pseudo-object that is returned for all query
6
+ # requests.
7
+ class Mash < ::Hashie::Mash
8
+ # Convert a json string to a Mash
9
+ #
10
+ # @param [String] json_string
11
+ # @return [Easyship::Rates::Ruby::Mash]
12
+ def self.from_json(json_string)
13
+ result_hash = ::MultiJson.decode(json_string)
14
+ new(result_hash)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module EasyshipRatesRubyApi
2
+ module Version #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+ PRE = nil
7
+ STRING = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
8
+ end
9
+ end
@@ -0,0 +1,34 @@
1
+ require 'oauth2'
2
+
3
+ module EasyshipRatesRubyApi
4
+ class << self
5
+ attr_accessor :uid, :secret, :sandbox
6
+
7
+ # config/initializers/easyship_rate_ruby_api.rb (for instance)
8
+ #
9
+ # ```ruby
10
+ # EasyshipRatesRubyApi.configure do |config|
11
+ # config.uid = 'uid'
12
+ # config.secret = 'secret'
13
+ # config.sandbox = true
14
+ # end
15
+ # ```
16
+ # elsewhere
17
+ #
18
+ # ```ruby
19
+ # client = EasyshipRatesRubyApi::Client.new
20
+ # ```
21
+ def configure
22
+ self.sandbox = false
23
+ yield self
24
+ true
25
+ end
26
+ end
27
+
28
+ autoload :Api, 'easyship/rates/ruby/api/api'
29
+ autoload :Client, 'easyship/rates/ruby/api/client'
30
+ autoload :Errors, 'easyship/rates/ruby/api/errors'
31
+ autoload :Helpers, 'easyship/rates/ruby/api/helpers'
32
+ autoload :Mash, 'easyship/rates/ruby/api/mash'
33
+ autoload :Version, 'easyship/rates/ruby/api/version'
34
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easyship-rates-ruby-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - aloha
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: multi_json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.10'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.10'
111
+ description: Ruby API for Rate Calculator
112
+ email:
113
+ - y.alohac@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - README.md
122
+ - Rakefile
123
+ - bin/console
124
+ - bin/setup
125
+ - easyship-rates-ruby-api.gemspec
126
+ - lib/easyship/rates/ruby/api/api.rb
127
+ - lib/easyship/rates/ruby/api/api/query_helpers.rb
128
+ - lib/easyship/rates/ruby/api/api/rates.rb
129
+ - lib/easyship/rates/ruby/api/client.rb
130
+ - lib/easyship/rates/ruby/api/errors.rb
131
+ - lib/easyship/rates/ruby/api/helpers.rb
132
+ - lib/easyship/rates/ruby/api/helpers/authorization.rb
133
+ - lib/easyship/rates/ruby/api/helpers/request.rb
134
+ - lib/easyship/rates/ruby/api/mash.rb
135
+ - lib/easyship/rates/ruby/api/version.rb
136
+ - lib/easyship_rates_ruby_api.rb
137
+ homepage: https://github.com/easyship/easyship-rates-ruby-api
138
+ licenses: []
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.4.8
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Ruby API for Rate Calculator
160
+ test_files: []