intelipost 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
- [![Code Climate](https://codeclimate.com/github/natuelabs/intelipost/badges/gpa.svg)](https://codeclimate.com/github/natuelabs/intelipost)
1
+ [![Gem Version](https://badge.fury.io/rb/intelipost.svg)](http://badge.fury.io/rb/intelipost)
2
2
  [![Build Status](https://travis-ci.org/natuelabs/intelipost.svg?branch=master)](https://travis-ci.org/natuelabs/intelipost)
3
+ [![Code Climate](https://codeclimate.com/github/natuelabs/intelipost/badges/gpa.svg)](https://codeclimate.com/github/natuelabs/intelipost)
4
+ [![Test Coverage](https://codeclimate.com/github/natuelabs/intelipost/badges/coverage.svg)](https://codeclimate.com/github/natuelabs/intelipost/coverage)
5
+
3
6
 
4
7
  # intelipost
5
8
  Gem for the Intelipost API
data/intelipost.gemspec CHANGED
@@ -4,30 +4,30 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'intelipost/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "intelipost"
7
+ spec.name = 'intelipost'
8
8
  spec.version = Intelipost::VERSION
9
- spec.authors = ["Eduardo Sampaio"]
10
- spec.email = ["eduardo@sampa.io"]
9
+ spec.authors = ['Eduardo Sampaio']
10
+ spec.email = ['eduardo@sampa.io']
11
11
  spec.description = %q{Gem to access the REST API of Intelipost}
12
12
  spec.summary = %q{Gem to access the REST API of Intelipost}
13
- spec.homepage = "http://github.com/natuelabs/intelipost"
14
- spec.license = "MIT"
13
+ spec.homepage = 'http://github.com/natuelabs/intelipost'
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_dependency "faraday", "~> 0.9.1"
22
- spec.add_dependency "faraday_middleware", "~> 0.9.1"
23
- spec.add_dependency "hashie", "~> 3.4.1"
21
+ spec.add_dependency 'faraday', '~> 0.9.1'
22
+ spec.add_dependency 'faraday_middleware', '~> 0.9.1'
23
+ spec.add_dependency 'hashie', '~> 3.4.1'
24
24
 
25
- spec.add_development_dependency "bundler", ">= 1.7.6"
26
- spec.add_development_dependency "rake", "~> 10.4.2"
27
- spec.add_development_dependency "rspec", "~> 3.2.0"
28
- spec.add_development_dependency "vcr", "~> 2.9.3"
29
- spec.add_development_dependency "webmock", "~> 1.21"
30
- spec.add_development_dependency "simplecov", "~> 0.10"
31
- spec.add_development_dependency "dotenv", "~> 2.0.1"
32
- spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4.7"
25
+ spec.add_development_dependency 'bundler', '>= 1.7.6'
26
+ spec.add_development_dependency 'rake', '~> 10.4.2'
27
+ spec.add_development_dependency 'rspec', '~> 3.2.0'
28
+ spec.add_development_dependency 'vcr', '~> 2.9.3'
29
+ spec.add_development_dependency 'webmock', '~> 1.21'
30
+ spec.add_development_dependency 'simplecov', '~> 0.10'
31
+ spec.add_development_dependency 'dotenv', '~> 2.0.1'
32
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4.7'
33
33
  end
data/lib/intelipost.rb CHANGED
@@ -1,11 +1,13 @@
1
- require "uri"
2
- require "hashie"
3
- require "faraday"
4
- require "faraday_middleware"
5
- require "intelipost/version"
6
- require "intelipost/fluent_interface"
7
- require "intelipost/client"
8
- require "intelipost/cep"
1
+ require 'uri'
2
+ require 'hashie'
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+ require 'middleware/gzip'
6
+ require 'intelipost/version'
7
+ require 'intelipost/fluent_interface'
8
+ require 'intelipost/client'
9
+ require 'intelipost/cep'
10
+ require 'intelipost/quote'
9
11
 
10
12
  module Intelipost
11
13
  end
@@ -19,6 +19,8 @@ module Intelipost
19
19
 
20
20
  def connection
21
21
  raise ArgumentError, 'an api_key is required to connect' if api_key.nil?
22
+
23
+ Faraday::Middleware.register_middleware gzip: lambda { FaradayMiddleware::Gzip }
22
24
  Faraday.new(url: @uri.to_s) do |conn|
23
25
  conn.request :json
24
26
 
@@ -26,7 +28,8 @@ module Intelipost
26
28
  conn.response :json
27
29
 
28
30
  conn.headers['api_key'] = api_key
29
- conn.adapter Faraday.default_adapter
31
+ conn.adapter :net_http
32
+ conn.use :gzip
30
33
  conn.proxy @options[:proxy]
31
34
  end
32
35
  end
@@ -35,6 +38,10 @@ module Intelipost
35
38
  connection.get(endpoint, args).body
36
39
  end
37
40
 
41
+ def post(endpoint, args = {})
42
+ connection.post(endpoint, args).body
43
+ end
44
+
38
45
  def method_missing(method, *args, &block)
39
46
  method = camelize method
40
47
  if Intelipost.const_defined? method
@@ -9,9 +9,12 @@ module Intelipost
9
9
  end
10
10
 
11
11
  module InstanceMethods
12
+ def create(post_values)
13
+ connection.post(endpoint, post_values)
14
+ end
15
+
12
16
  def method_missing(method, *args, &block)
13
- uri = [endpoint, method, args].join '/'
14
- connection.get uri
17
+ connection.get [endpoint, method, args].join '/'
15
18
  end
16
19
  end
17
20
  end
@@ -0,0 +1,5 @@
1
+ module Intelipost
2
+ class Quote < Intelipost::FluentInterfaceBase
3
+ set_endpoint 'quote'
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module Intelipost
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -0,0 +1,56 @@
1
+ module FaradayMiddleware
2
+ # TODO: Remove this file and the related line on the client.rb
3
+ # it was only added because faraday_middleware has not released
4
+ # a gem version including this specific middleware.
5
+ #
6
+ # Middleware to automatically decompress response bodies. If the
7
+ # "Accept-Encoding" header wasn't set in the request, this sets it to
8
+ # "gzip,deflate" and appropriately handles the compressed response from the
9
+ # server. This resembles what Ruby 1.9+ does internally in Net::HTTP#get.
10
+ #
11
+ # This middleware is NOT necessary when these adapters are used:
12
+ # - net_http on Ruby 1.9+
13
+ # - net_http_persistent on Ruby 2.0+
14
+ # - em_http
15
+ class Gzip < Faraday::Middleware
16
+ dependency 'zlib'
17
+
18
+ ACCEPT_ENCODING = 'Accept-Encoding'.freeze
19
+ CONTENT_ENCODING = 'Content-Encoding'.freeze
20
+ CONTENT_LENGTH = 'Content-Length'.freeze
21
+ SUPPORTED_ENCODINGS = 'gzip,deflate'.freeze
22
+ RUBY_ENCODING = '1.9'.respond_to?(:force_encoding)
23
+
24
+ def call(env)
25
+ env[:request_headers][ACCEPT_ENCODING] ||= SUPPORTED_ENCODINGS
26
+ @app.call(env).on_complete do |response_env|
27
+ case response_env[:response_headers][CONTENT_ENCODING]
28
+ when 'gzip'
29
+ reset_body(response_env, &method(:uncompress_gzip))
30
+ when 'deflate'
31
+ reset_body(response_env, &method(:inflate))
32
+ end
33
+ end
34
+ end
35
+
36
+ def reset_body(env)
37
+ env[:body] = yield(env[:body])
38
+ env[:response_headers].delete(CONTENT_ENCODING)
39
+ env[:response_headers][CONTENT_LENGTH] = env[:body].length
40
+ end
41
+
42
+ def uncompress_gzip(body)
43
+ io = StringIO.new(body)
44
+ gzip_reader = if RUBY_ENCODING
45
+ Zlib::GzipReader.new(io, :encoding => 'ASCII-8BIT')
46
+ else
47
+ Zlib::GzipReader.new(io)
48
+ end
49
+ gzip_reader.read
50
+ end
51
+
52
+ def inflate(body)
53
+ Zlib::Inflate.inflate(body)
54
+ end
55
+ end
56
+ end
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  describe Intelipost::Cep do
4
2
  subject { Intelipost::Cep.new double('Intelipost::Client') }
5
3
 
@@ -1,21 +1,25 @@
1
- require 'spec_helper'
1
+ # coding: utf-8
2
2
 
3
3
  describe Intelipost::Client, :vcr do
4
- it 'raises ArgumentError if initialized with no options' do
5
- expect do
6
- Intelipost::Client.new
7
- end.to raise_error ArgumentError
8
- end
4
+ subject { Intelipost::Client.new api_key: ENV['INTELIPOST_API_KEY'] }
5
+
6
+ context 'when initialized with no options' do
7
+ subject { Intelipost::Client }
8
+
9
+ it 'raises ArgumentError if initialized with no options' do
10
+ expect do
11
+ Intelipost::Client.new
12
+ end.to raise_error ArgumentError
13
+ end
9
14
 
10
- it 'raises ArgumentError if initialized without credentials' do
11
- expect do
12
- Intelipost::Client.new api_key: nil
13
- end.to raise_error ArgumentError
15
+ it 'raises ArgumentError if initialized without credentials' do
16
+ expect do
17
+ Intelipost::Client.new api_key: nil
18
+ end.to raise_error ArgumentError
19
+ end
14
20
  end
15
21
 
16
22
  context 'initialized with credentials' do
17
- subject { Intelipost::Client.new api_key: ENV['INTELIPOST_API_KEY'] }
18
-
19
23
  describe '#connection' do
20
24
  it 'is an instance of Faraday::Connection' do
21
25
  expect(subject.connection).to be_an_instance_of Faraday::Connection
@@ -25,17 +29,53 @@ describe Intelipost::Client, :vcr do
25
29
  expect(subject.connection.headers).to include('api_key')
26
30
  end
27
31
  end
32
+ end
28
33
 
29
- describe '#options' do
30
- it 'includes api_key' do
31
- expect(subject.options).to include(:api_key)
32
- end
34
+ context 'dealing with options' do
35
+ it 'includes api_key' do
36
+ expect(subject.options).to include(:api_key)
33
37
  end
38
+ end
34
39
 
35
- describe '#cep' do
36
- it 'returns a Hashie::Mash' do
37
- expect(subject.cep.address_complete('04661100').class).to be Hashie::Mash
38
- end
40
+ context 'dealing with zipcode (cep)' do
41
+ it 'returns a Hashie::Mash on successful query' do
42
+ expect(subject.cep.address_complete('04661100').class).to be Hashie::Mash
43
+ end
44
+ end
45
+
46
+ context 'request quotations' do
47
+ let(:volumes) do
48
+ {
49
+ 'origin_zip_code' => '04037-003',
50
+ 'destination_zip_code' => '06396-200',
51
+ 'volumes' => [
52
+ {
53
+ 'weight' => 0.1,
54
+ 'volume_type' => 'BOX',
55
+ 'cost_of_goods' => 100,
56
+ 'width' => 10,
57
+ 'height' => 10,
58
+ 'length' => 10
59
+ }
60
+ ],
61
+ 'additional_information' => {
62
+ 'free_shipping' => false,
63
+ 'extra_cost_absolute' => 0,
64
+ 'extra_cost_percentage' => 0,
65
+ 'lead_time_business_days' => 0,
66
+ 'sales_channel' => 'hotsite',
67
+ 'client_type' => 'gold',
68
+ 'delivery_method_ids' => [
69
+ 4,
70
+ 3,
71
+ 2
72
+ ]
73
+ }
74
+ }
75
+ end
76
+
77
+ it '.quote.create' do
78
+ expect(subject.quote.create(volumes)).to have_key(:content)
39
79
  end
40
80
  end
41
81
  end
@@ -0,0 +1,38 @@
1
+ describe Intelipost::Quote do
2
+ subject { Intelipost::Quote.new(double('Intelipost::Client')) }
3
+
4
+ let(:volumes) do
5
+ {
6
+ 'origin_zip_code' => '04037-003',
7
+ 'destination_zip_code' => '06396-200',
8
+ 'volumes' => [
9
+ {
10
+ 'weight' => 0.1,
11
+ 'volume_type' => 'BOX',
12
+ 'cost_of_goods'=> 100,
13
+ 'width' => 10,
14
+ 'height' => 10,
15
+ 'length' => 10
16
+ }
17
+ ],
18
+ 'additional_information' => {
19
+ 'free_shipping' => false,
20
+ 'extra_cost_absolute' => 0,
21
+ 'extra_cost_percentage' => 0,
22
+ 'lead_time_business_days' => 0,
23
+ 'sales_channel' => 'hotsite',
24
+ 'client_type' => 'gold',
25
+ 'delivery_method_ids' => [
26
+ 4,
27
+ 3,
28
+ 2
29
+ ]
30
+ }
31
+ }
32
+ end
33
+
34
+ it 'correctly fetch for a quote' do
35
+ expect(subject.connection).to receive(:post).with('quote', volumes)
36
+ subject.create(volumes)
37
+ end
38
+ end
metadata CHANGED
@@ -1,167 +1,190 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: intelipost
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Eduardo Sampaio
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-05-22 00:00:00.000000000 Z
12
+ date: 2015-05-26 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: faraday
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - "~>"
19
+ - - ~>
18
20
  - !ruby/object:Gem::Version
19
21
  version: 0.9.1
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - "~>"
27
+ - - ~>
25
28
  - !ruby/object:Gem::Version
26
29
  version: 0.9.1
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: faraday_middleware
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - "~>"
35
+ - - ~>
32
36
  - !ruby/object:Gem::Version
33
37
  version: 0.9.1
34
38
  type: :runtime
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - "~>"
43
+ - - ~>
39
44
  - !ruby/object:Gem::Version
40
45
  version: 0.9.1
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: hashie
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - "~>"
51
+ - - ~>
46
52
  - !ruby/object:Gem::Version
47
53
  version: 3.4.1
48
54
  type: :runtime
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - "~>"
59
+ - - ~>
53
60
  - !ruby/object:Gem::Version
54
61
  version: 3.4.1
55
62
  - !ruby/object:Gem::Dependency
56
63
  name: bundler
57
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
- - - ">="
67
+ - - ! '>='
60
68
  - !ruby/object:Gem::Version
61
69
  version: 1.7.6
62
70
  type: :development
63
71
  prerelease: false
64
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
65
74
  requirements:
66
- - - ">="
75
+ - - ! '>='
67
76
  - !ruby/object:Gem::Version
68
77
  version: 1.7.6
69
78
  - !ruby/object:Gem::Dependency
70
79
  name: rake
71
80
  requirement: !ruby/object:Gem::Requirement
81
+ none: false
72
82
  requirements:
73
- - - "~>"
83
+ - - ~>
74
84
  - !ruby/object:Gem::Version
75
85
  version: 10.4.2
76
86
  type: :development
77
87
  prerelease: false
78
88
  version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
79
90
  requirements:
80
- - - "~>"
91
+ - - ~>
81
92
  - !ruby/object:Gem::Version
82
93
  version: 10.4.2
83
94
  - !ruby/object:Gem::Dependency
84
95
  name: rspec
85
96
  requirement: !ruby/object:Gem::Requirement
97
+ none: false
86
98
  requirements:
87
- - - "~>"
99
+ - - ~>
88
100
  - !ruby/object:Gem::Version
89
101
  version: 3.2.0
90
102
  type: :development
91
103
  prerelease: false
92
104
  version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
93
106
  requirements:
94
- - - "~>"
107
+ - - ~>
95
108
  - !ruby/object:Gem::Version
96
109
  version: 3.2.0
97
110
  - !ruby/object:Gem::Dependency
98
111
  name: vcr
99
112
  requirement: !ruby/object:Gem::Requirement
113
+ none: false
100
114
  requirements:
101
- - - "~>"
115
+ - - ~>
102
116
  - !ruby/object:Gem::Version
103
117
  version: 2.9.3
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
107
122
  requirements:
108
- - - "~>"
123
+ - - ~>
109
124
  - !ruby/object:Gem::Version
110
125
  version: 2.9.3
111
126
  - !ruby/object:Gem::Dependency
112
127
  name: webmock
113
128
  requirement: !ruby/object:Gem::Requirement
129
+ none: false
114
130
  requirements:
115
- - - "~>"
131
+ - - ~>
116
132
  - !ruby/object:Gem::Version
117
133
  version: '1.21'
118
134
  type: :development
119
135
  prerelease: false
120
136
  version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
121
138
  requirements:
122
- - - "~>"
139
+ - - ~>
123
140
  - !ruby/object:Gem::Version
124
141
  version: '1.21'
125
142
  - !ruby/object:Gem::Dependency
126
143
  name: simplecov
127
144
  requirement: !ruby/object:Gem::Requirement
145
+ none: false
128
146
  requirements:
129
- - - "~>"
147
+ - - ~>
130
148
  - !ruby/object:Gem::Version
131
149
  version: '0.10'
132
150
  type: :development
133
151
  prerelease: false
134
152
  version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
135
154
  requirements:
136
- - - "~>"
155
+ - - ~>
137
156
  - !ruby/object:Gem::Version
138
157
  version: '0.10'
139
158
  - !ruby/object:Gem::Dependency
140
159
  name: dotenv
141
160
  requirement: !ruby/object:Gem::Requirement
161
+ none: false
142
162
  requirements:
143
- - - "~>"
163
+ - - ~>
144
164
  - !ruby/object:Gem::Version
145
165
  version: 2.0.1
146
166
  type: :development
147
167
  prerelease: false
148
168
  version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
149
170
  requirements:
150
- - - "~>"
171
+ - - ~>
151
172
  - !ruby/object:Gem::Version
152
173
  version: 2.0.1
153
174
  - !ruby/object:Gem::Dependency
154
175
  name: codeclimate-test-reporter
155
176
  requirement: !ruby/object:Gem::Requirement
177
+ none: false
156
178
  requirements:
157
- - - "~>"
179
+ - - ~>
158
180
  - !ruby/object:Gem::Version
159
181
  version: 0.4.7
160
182
  type: :development
161
183
  prerelease: false
162
184
  version_requirements: !ruby/object:Gem::Requirement
185
+ none: false
163
186
  requirements:
164
- - - "~>"
187
+ - - ~>
165
188
  - !ruby/object:Gem::Version
166
189
  version: 0.4.7
167
190
  description: Gem to access the REST API of Intelipost
@@ -171,8 +194,9 @@ executables: []
171
194
  extensions: []
172
195
  extra_rdoc_files: []
173
196
  files:
174
- - ".gitignore"
175
- - ".travis.yml"
197
+ - .gitignore
198
+ - .rspec
199
+ - .travis.yml
176
200
  - Gemfile
177
201
  - LICENSE
178
202
  - README.md
@@ -182,35 +206,40 @@ files:
182
206
  - lib/intelipost/cep.rb
183
207
  - lib/intelipost/client.rb
184
208
  - lib/intelipost/fluent_interface.rb
209
+ - lib/intelipost/quote.rb
185
210
  - lib/intelipost/version.rb
211
+ - lib/middleware/gzip.rb
186
212
  - spec/lib/intelipost/cep_spec.rb
187
213
  - spec/lib/intelipost/client_spec.rb
214
+ - spec/lib/intelipost/quote_spec.rb
188
215
  - spec/spec_helper.rb
189
216
  homepage: http://github.com/natuelabs/intelipost
190
217
  licenses:
191
218
  - MIT
192
- metadata: {}
193
219
  post_install_message:
194
220
  rdoc_options: []
195
221
  require_paths:
196
222
  - lib
197
223
  required_ruby_version: !ruby/object:Gem::Requirement
224
+ none: false
198
225
  requirements:
199
- - - ">="
226
+ - - ! '>='
200
227
  - !ruby/object:Gem::Version
201
228
  version: '0'
202
229
  required_rubygems_version: !ruby/object:Gem::Requirement
230
+ none: false
203
231
  requirements:
204
- - - ">="
232
+ - - ! '>='
205
233
  - !ruby/object:Gem::Version
206
234
  version: '0'
207
235
  requirements: []
208
236
  rubyforge_project:
209
- rubygems_version: 2.2.2
237
+ rubygems_version: 1.8.23.2
210
238
  signing_key:
211
- specification_version: 4
239
+ specification_version: 3
212
240
  summary: Gem to access the REST API of Intelipost
213
241
  test_files:
214
242
  - spec/lib/intelipost/cep_spec.rb
215
243
  - spec/lib/intelipost/client_spec.rb
244
+ - spec/lib/intelipost/quote_spec.rb
216
245
  - spec/spec_helper.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: d6ca01e96d446405e77f5c6572671e28760403bc
4
- data.tar.gz: fee233889e6fb54aef9304b94b9c06cd31718d84
5
- SHA512:
6
- metadata.gz: 3be6fa5942c0716766640bd8da478da18db7e0a3bda89a8c4f7828d005c2e5baaba08bc13c5a37f71f96ed3a5129896b3cafbd0d8e0069d3623861b20eab574d
7
- data.tar.gz: 6dca90b3314dac9a530f821afa8d63bdf6fdb006f22114461ef77ff2a71d820fe4af7423a17c34b20cd06661ce3382705b9bca894c16ff59a987e4b1b1535680