intelipost-api 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/Gemfile +18 -0
- data/LICENSE.txt +22 -0
- data/README.md +63 -0
- data/Rakefile +34 -0
- data/intelipost-api.gemspec +23 -0
- data/lib/intelipost/api/components/cep_location.rb +23 -0
- data/lib/intelipost/api/components/component.rb +7 -0
- data/lib/intelipost/api/components/quote.rb +48 -0
- data/lib/intelipost/api/facade.rb +41 -0
- data/lib/intelipost/api/models/address.rb +15 -0
- data/lib/intelipost/api/models/delivery_option.rb +13 -0
- data/lib/intelipost/api/models/error_response.rb +9 -0
- data/lib/intelipost/api/models/quote.rb +15 -0
- data/lib/intelipost/api/models/volume.rb +12 -0
- data/lib/intelipost/api/version.rb +3 -0
- data/lib/intelipost/api.rb +27 -0
- data/lib/intelipost-api.rb +6 -0
- data/spec/factories/address.rb +13 -0
- data/spec/factories/delivery_option.rb +12 -0
- data/spec/factories/error_response.rb +7 -0
- data/spec/factories/quote.rb +18 -0
- data/spec/factories/volume.rb +10 -0
- data/spec/lib/intelipost/api/components/cep_location_spec.rb +52 -0
- data/spec/lib/intelipost/api/components/quote_spec.rb +88 -0
- data/spec/lib/intelipost/api/facade_spec.rb +23 -0
- data/spec/lib/intelipost/api_spec.rb +11 -0
- data/spec/spec_helper.rb +37 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7f486d26ecb8dd5b78bd6220de7efdae8707e7da
|
4
|
+
data.tar.gz: 96d3b32683fe1279e9a5eecf25f727983e3cd3e6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0cd684bdffab64c5de936f134954b681e1f7b851faf656ce94c3b789eaab5a8ccec8a1526a18508a8b631209526d30e34dcc365b71a2e467ec36f790bb78191a
|
7
|
+
data.tar.gz: c5d5c988288afd6fcb1d6ecfeffb4202c4d6199274d8d04100fdc88f7e92c1f85f769d4e7cdefd7fdbe23ac4236e0d1878f003c6f576b94ad2a66218933f48ff
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
ruby '2.1.4'
|
4
|
+
|
5
|
+
group :test, :development do
|
6
|
+
gem 'rspec'
|
7
|
+
gem 'pry'
|
8
|
+
gem 'factory_girl'
|
9
|
+
end
|
10
|
+
|
11
|
+
gem 'httparty'
|
12
|
+
gem 'logging'
|
13
|
+
gem 'colorize'
|
14
|
+
gem 'virtus'
|
15
|
+
gem 'colorize'
|
16
|
+
|
17
|
+
# Specify your gem's dependencies in intelipost-api.gemspec
|
18
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Renato Alves
|
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,63 @@
|
|
1
|
+
# Intelipost::Api
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'intelipost-api'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install intelipost-api
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/intelipost-api/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
32
|
+
|
33
|
+
## API Structure
|
34
|
+
|
35
|
+
The idea is to keep the api very close to the real HTTP urls:
|
36
|
+
Every subset of data must become a Unique Intelipost::Api::Model, some example of subsets:
|
37
|
+
- Volume
|
38
|
+
- DeliveryOption
|
39
|
+
|
40
|
+
The convention for the APIs is the same: Don't pass a JSON/Hash a facade param, since JSONs/Hashs, may become complex grouping of data.
|
41
|
+
|
42
|
+
Done:
|
43
|
+
|
44
|
+
- Intelipost::Api::CepLocation.address_complete(zipcode)
|
45
|
+
- Intelipost::Api::Quote.create(origin_zipcode, destination_zipcode, volumes(object))
|
46
|
+
|
47
|
+
Undone:
|
48
|
+
|
49
|
+
- Intelipost::Api::Tracking.create()
|
50
|
+
- Intelipost::Api::Quote.find()
|
51
|
+
- Intelipost::Api::ShipmentOrder.create()
|
52
|
+
- Intelipost::Api::ShipmentOrder.find()
|
53
|
+
- Intelipost::Api::ShipmentOrder.cancel()
|
54
|
+
- Intelipost::Api::ShipmentOrder.end_customer()
|
55
|
+
- Intelipost::Api::ShipmentOrder.invoice_data()
|
56
|
+
- Intelipost::Api::ShipmentOrder.get_label()
|
57
|
+
- Intelipost::Api::ShipmentOrder.get_volumes()
|
58
|
+
- Intelipost::Api::ShipmentOrder.read_status()
|
59
|
+
- Intelipost::Api::ShipmentOrder.ready_for_shipment()
|
60
|
+
- Intelipost::Api::ShipmentOrder.set_invoice()
|
61
|
+
- Intelipost::Api::ShipmentOrder.set_tracking_data()
|
62
|
+
- Intelipost::Api::ShipmentOrder.set_volume()
|
63
|
+
- Intelipost::Api::ShipmentOrder.shipped()
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "colorize"
|
3
|
+
|
4
|
+
def load_irb
|
5
|
+
require 'irb'
|
6
|
+
require 'rspec'
|
7
|
+
require_relative 'spec/spec_helper'
|
8
|
+
ARGV.clear
|
9
|
+
IRB.start
|
10
|
+
end
|
11
|
+
|
12
|
+
def puts_header_text
|
13
|
+
puts "-------------------------------------"
|
14
|
+
puts "Intelipost::Api Console".on_blue
|
15
|
+
puts "-------------------------------------"
|
16
|
+
puts "This is a Irb Console including Intelipost Lib and Rspec environment.".yellow
|
17
|
+
puts "A method called ".yellow + " i ".on_red.white + " will instantiate Intelipost::Api and your key".yellow
|
18
|
+
puts "For your convenience, you can pass an ApiKey for this method.".yellow
|
19
|
+
puts "If you dont specify any ApiKey, we will generate the variable with a Fake ApiKey.".white
|
20
|
+
puts "-------------------------------------"
|
21
|
+
end
|
22
|
+
|
23
|
+
def i(apikey=nil)
|
24
|
+
o = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
|
25
|
+
o1 = (0...50).map { o[rand(o.length)] }.join
|
26
|
+
|
27
|
+
Intelipost::Api.new((apikey || o1))
|
28
|
+
end
|
29
|
+
|
30
|
+
desc "Open an irb session preloaded with this library"
|
31
|
+
task :console, [:apikey] do |t, apikey|
|
32
|
+
puts_header_text
|
33
|
+
load_irb
|
34
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'intelipost/api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "intelipost-api"
|
8
|
+
spec.version = Intelipost::VERSION
|
9
|
+
spec.authors = ["Renato Alves"]
|
10
|
+
spec.email = ["renatodex@gmail.com"]
|
11
|
+
spec.summary = %q{Intelipost API Gem for Ruby}
|
12
|
+
spec.description = %q{Intelipost API Gem for Ruby}
|
13
|
+
spec.homepage = "http://github.com/renatodex/intelipost-ruby-api"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Intelipost::ApiComponents
|
2
|
+
class CepLocation < Component
|
3
|
+
attr_accessor :api_key
|
4
|
+
|
5
|
+
def initialize(api_key)
|
6
|
+
self.api_key = api_key
|
7
|
+
end
|
8
|
+
|
9
|
+
def address_complete(zipcode)
|
10
|
+
raw_data = Intelipost::Facade.get_with_log("/v1/cep_location/address_complete/#{zipcode}", api_key)
|
11
|
+
|
12
|
+
if has_error?(raw_data)
|
13
|
+
response = Intelipost::Models::ErrorResponse.new(raw_data)
|
14
|
+
else
|
15
|
+
response = Intelipost::Models::Address.new
|
16
|
+
response.attributes = raw_data
|
17
|
+
response.attributes = raw_data["content"] || {}
|
18
|
+
end
|
19
|
+
|
20
|
+
response
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Intelipost::ApiComponents
|
2
|
+
class Quote < Component
|
3
|
+
attr_accessor :api_key
|
4
|
+
|
5
|
+
def initialize(api_key)
|
6
|
+
self.api_key = api_key
|
7
|
+
end
|
8
|
+
|
9
|
+
def request_json(origin_zipcode, destination_zipcode, volumes_collection)
|
10
|
+
{
|
11
|
+
"origin_zip_code" => origin_zipcode,
|
12
|
+
"destination_zip_code" => destination_zipcode,
|
13
|
+
"volumes" => volumes_collection.inject([]) { |arr, v|
|
14
|
+
arr << {
|
15
|
+
"weight" => v.weight,
|
16
|
+
"volume_type" => v.volume_type,
|
17
|
+
"cost_of_goods" => v.cost_of_goods,
|
18
|
+
"width" => v.width,
|
19
|
+
"height" => v.height,
|
20
|
+
"length" => v.length
|
21
|
+
}
|
22
|
+
}
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def create(origin_zipcode, destination_zipcode, volumes_collection)
|
27
|
+
raw_request = self.request_json(origin_zipcode, destination_zipcode, volumes_collection)
|
28
|
+
raw_response = Intelipost::Facade.post_with_log("/v1/quote", api_key, :body => raw_request.to_json)
|
29
|
+
|
30
|
+
if raw_response["status"] == "ERROR"
|
31
|
+
response = Intelipost::Models::ErrorResponse.new(raw_response)
|
32
|
+
else
|
33
|
+
response = Intelipost::Models::Quote.new(raw_response)
|
34
|
+
response.attributes = raw_response["content"] || {}
|
35
|
+
response.volumes = raw_response["content"]["volumes"].inject([]) { |res, volume|
|
36
|
+
res << Intelipost::Models::Volume.new(volume)
|
37
|
+
res
|
38
|
+
}
|
39
|
+
response.delivery_options = raw_response["content"]["delivery_options"].inject([]) { |res, delivery_option|
|
40
|
+
res << Intelipost::Models::DeliveryOption.new(delivery_option)
|
41
|
+
res
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
response
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Intelipost
|
2
|
+
class Facade
|
3
|
+
include ::HTTParty
|
4
|
+
base_uri 'https://api.intelipost.com.br/api'
|
5
|
+
format :json
|
6
|
+
default_timeout 15
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def get_with_log(url, api_key, params={})
|
10
|
+
call_intelipost(api_key, :get, url, params)
|
11
|
+
end
|
12
|
+
|
13
|
+
def post_with_log(url, api_key, params={})
|
14
|
+
call_intelipost(api_key, :post, url, params)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def call_intelipost(api_key, action, url, params)
|
19
|
+
$logger.info ("[info] calling intelipost with ".yellow + "#{action.to_s.upcase}".white.on_blue + ": #{url},".white + " #{params.to_s}".green)
|
20
|
+
begin
|
21
|
+
response = self.send(action, url, params_with_apikey(params, api_key))
|
22
|
+
rescue SocketError
|
23
|
+
response = Intelipost::Models::ErrorResponse.new({status: "ERROR", messages: [{"type" => "ERROR", "text" => "Connection Timeout", "key" => "timeout"}]})
|
24
|
+
end
|
25
|
+
$logger.info ("[info] intelipost response from call ".yellow + "#{action.to_s.upcase}".white.on_blue + " " + "#{url}, ".white + " #{response.to_s}".blue)
|
26
|
+
|
27
|
+
response
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def params_with_apikey(params, api_key)
|
32
|
+
params.merge({
|
33
|
+
:headers => {
|
34
|
+
"api_key" => api_key,
|
35
|
+
"Content-Type" => "application/json"
|
36
|
+
}
|
37
|
+
})
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Intelipost::Models
|
2
|
+
class Address
|
3
|
+
include ::Virtus::Model
|
4
|
+
|
5
|
+
attribute :status, String
|
6
|
+
attribute :messages, Array
|
7
|
+
attribute :state, String
|
8
|
+
attribute :city, String
|
9
|
+
attribute :neighborhood, String
|
10
|
+
attribute :street, String
|
11
|
+
attribute :time, String
|
12
|
+
attribute :state_short, String
|
13
|
+
attribute :ibge, String
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Intelipost::Models
|
2
|
+
class DeliveryOption
|
3
|
+
include Virtus::Model
|
4
|
+
attribute :delivery_method_id
|
5
|
+
attribute :delivery_estimate_business_days
|
6
|
+
attribute :provider_shipping_cost
|
7
|
+
attribute :final_shipping_cost
|
8
|
+
attribute :description
|
9
|
+
attribute :logistic_provider_name
|
10
|
+
attribute :delivery_method_type
|
11
|
+
attribute :delivery_method_name
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Intelipost::Models
|
2
|
+
class Quote
|
3
|
+
include Virtus::Model
|
4
|
+
|
5
|
+
attribute :id, Integer
|
6
|
+
attribute :client_id, Integer
|
7
|
+
attribute :origin_zip_code, String
|
8
|
+
attribute :destination_zip_code, String
|
9
|
+
attribute :status, String
|
10
|
+
attribute :messages, Array
|
11
|
+
attribute :time, String
|
12
|
+
attribute :volumes, Array[::Intelipost::Models::Volume]
|
13
|
+
attribute :delivery_options, Array[::Intelipost::Models::DeliveryOption]
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Intelipost
|
2
|
+
class Api
|
3
|
+
attr_accessor :api_key
|
4
|
+
|
5
|
+
def initialize(api_key, environment=nil)
|
6
|
+
self.api_key = api_key
|
7
|
+
$logger = Logging.logger(log_env(environment))
|
8
|
+
$logger.level = :info
|
9
|
+
end
|
10
|
+
|
11
|
+
def CepLocation
|
12
|
+
ApiComponents::CepLocation.new(self.api_key)
|
13
|
+
end
|
14
|
+
|
15
|
+
def Quote
|
16
|
+
ApiComponents::Quote.new(self.api_key)
|
17
|
+
end
|
18
|
+
|
19
|
+
def log_env(environment)
|
20
|
+
if environment.nil?
|
21
|
+
STDOUT
|
22
|
+
else
|
23
|
+
"logs/intelipost_#{environment}.log"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
FactoryGirl.define do
|
2
|
+
factory :delivery_option do
|
3
|
+
delivery_method_id 1
|
4
|
+
delivery_estimate_business_days 4
|
5
|
+
provider_shipping_cost 17.4
|
6
|
+
final_shipping_cost 17.4
|
7
|
+
description "Correios PAC"
|
8
|
+
logistic_provider_name "CORREIOS"
|
9
|
+
delivery_method_type "STANDARD"
|
10
|
+
delivery_method_name "Correios PAC"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
FactoryGirl.define do
|
2
|
+
factory :quote do
|
3
|
+
id 293
|
4
|
+
client_id 648278
|
5
|
+
origin_zip_code "01419-002"
|
6
|
+
destination_zip_code "04180-010"
|
7
|
+
status "OK"
|
8
|
+
messages []
|
9
|
+
time "32.1ms"
|
10
|
+
volumes []
|
11
|
+
delivery_options []
|
12
|
+
|
13
|
+
after(:build) do |quote|
|
14
|
+
quote.volumes = build_list(:volume, 2)
|
15
|
+
quote.delivery_options = build_list(:delivery_option, 2)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Intelipost::ApiComponents::CepLocation do
|
4
|
+
|
5
|
+
let(:intelipost) {
|
6
|
+
Intelipost::Api.new("apikey", :test)
|
7
|
+
}
|
8
|
+
|
9
|
+
let(:response_cep_ok) {{
|
10
|
+
"status" => "OK",
|
11
|
+
"messages" => [ ],
|
12
|
+
"content" => {
|
13
|
+
"state" => "Sao Paulo",
|
14
|
+
"city" => "Sao Paulo",
|
15
|
+
"neighborhood" => "Cerqueira Cesar",
|
16
|
+
"street" => "Rua Augusta",
|
17
|
+
"state_short" => "SP",
|
18
|
+
"ibge" => "3550308",
|
19
|
+
"bairro" => "Vila Gomes Cardim"
|
20
|
+
},
|
21
|
+
"time" => "32.1 ms"
|
22
|
+
}}
|
23
|
+
|
24
|
+
let(:response_cep_invalid) {{
|
25
|
+
"status"=>"OK",
|
26
|
+
"messages"=>[
|
27
|
+
{"type"=>"INFO", "text"=>"Cep desconhecido", "key"=>"cep.missing"}
|
28
|
+
],
|
29
|
+
"time"=>"1.0 ms"
|
30
|
+
}}
|
31
|
+
|
32
|
+
it "should retrieve a valid zipcode" do
|
33
|
+
allow(Intelipost::Facade).to receive(:get) { response_cep_ok }
|
34
|
+
|
35
|
+
intelipost_address = intelipost.CepLocation.address_complete('01413000')
|
36
|
+
expect(intelipost_address.status).to eq "OK"
|
37
|
+
expect(intelipost_address.messages).to eq []
|
38
|
+
expect(intelipost_address.state).to eq "Sao Paulo"
|
39
|
+
expect(intelipost_address.city).to eq "Sao Paulo"
|
40
|
+
expect(intelipost_address.neighborhood).to eq "Cerqueira Cesar"
|
41
|
+
expect(intelipost_address.street).to eq "Rua Augusta"
|
42
|
+
expect(intelipost_address.time).to eq "32.1 ms"
|
43
|
+
expect(intelipost_address.ibge).to eq "3550308"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should retrieve an invalid zipcode" do
|
47
|
+
allow(Intelipost::Facade).to receive(:get) { response_cep_invalid }
|
48
|
+
response = intelipost.CepLocation.address_complete('99999999')
|
49
|
+
expect(response.class).to be Intelipost::Models::ErrorResponse
|
50
|
+
expect(response.messages.first["key"]).to eq "cep.missing"
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Intelipost::ApiComponents::Quote do
|
4
|
+
|
5
|
+
let(:intelipost) {
|
6
|
+
Intelipost::Api.new("apikey", :test)
|
7
|
+
}
|
8
|
+
|
9
|
+
let(:raw_quote) {
|
10
|
+
{
|
11
|
+
"status" => "OK",
|
12
|
+
"messages" => [],
|
13
|
+
"content" => {
|
14
|
+
"id" => 1022029,
|
15
|
+
"client_id" => 243,
|
16
|
+
"origin_zip_code" => "04180-010",
|
17
|
+
"destination_zip_code" => "01419-002",
|
18
|
+
"delivery_options" =>[{
|
19
|
+
"delivery_method_id" => 1,
|
20
|
+
"delivery_estimate_business_days" => 4,
|
21
|
+
"provider_shipping_cost" => 17.4,
|
22
|
+
"final_shipping_cost" => 17.4,
|
23
|
+
"description" => "Correios PAC",
|
24
|
+
"logistic_provider_name" => "CORREIOS",
|
25
|
+
"delivery_method_type" => "STANDARD",
|
26
|
+
"delivery_method_name" => "Correios PAC"
|
27
|
+
}],
|
28
|
+
"volumes" =>[{
|
29
|
+
"weight" => 2,
|
30
|
+
"cost_of_goods" => 30.99,
|
31
|
+
"width" => 20,
|
32
|
+
"height" => 10,
|
33
|
+
"length" => 25,
|
34
|
+
"volume_type" => "BOX"
|
35
|
+
}]
|
36
|
+
},
|
37
|
+
"time" => "19.9 ms"
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
let(:volumes_array) {
|
42
|
+
[{
|
43
|
+
"weight" => 2.0,
|
44
|
+
"volume_type" => "BOX",
|
45
|
+
"cost_of_goods" => 30.99,
|
46
|
+
"width" => 20.0,
|
47
|
+
"height" => 10.0,
|
48
|
+
"length" => 25.0
|
49
|
+
}]
|
50
|
+
}
|
51
|
+
|
52
|
+
let(:volumes_object_array) {
|
53
|
+
[build(:volume)]
|
54
|
+
# volumes_array.inject([]) { |res, v|
|
55
|
+
# res << Intelipost::Models::Volume.new(v)
|
56
|
+
# }
|
57
|
+
}
|
58
|
+
|
59
|
+
it "should create a quote" do
|
60
|
+
allow(Intelipost::Facade).to receive(:post) { raw_quote }
|
61
|
+
quote = intelipost.Quote.create('04180-010', '014190-002', volumes_object_array)
|
62
|
+
|
63
|
+
expect(quote.status).to eq "OK"
|
64
|
+
expect(quote.messages).to eq []
|
65
|
+
expect(quote.id).to eq 1022029
|
66
|
+
expect(quote.client_id).to eq 243
|
67
|
+
expect(quote.time).to eq "19.9 ms"
|
68
|
+
expect(quote.origin_zip_code).to eq '04180-010'
|
69
|
+
expect(quote.destination_zip_code).to eq '01419-002'
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should generate json request object for quote" do
|
73
|
+
expect(intelipost.Quote.request_json('01419002', '04180010', volumes_object_array)).to eq({
|
74
|
+
"origin_zip_code" => "01419002",
|
75
|
+
"destination_zip_code" => "04180010",
|
76
|
+
"volumes" => [
|
77
|
+
{
|
78
|
+
"weight" => 2.0,
|
79
|
+
"volume_type" => "BOX",
|
80
|
+
"cost_of_goods" => 30.99,
|
81
|
+
"width" => 20.0,
|
82
|
+
"height" => 10.0,
|
83
|
+
"length" => 25.0
|
84
|
+
}
|
85
|
+
]
|
86
|
+
})
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Intelipost::Api do
|
4
|
+
|
5
|
+
shared_examples "intelipost_request" do
|
6
|
+
it {
|
7
|
+
allow(Intelipost::Facade).to receive(request_method).and_raise SocketError
|
8
|
+
response = Intelipost::Facade.send("#{request_method}_with_log",'http://test.com', 'apikey', {})
|
9
|
+
expect(response.status).to eq "ERROR"
|
10
|
+
expect(response.messages.first["type"]).to eq "ERROR"
|
11
|
+
expect(response.messages.first["text"]).to eq "Connection Timeout"
|
12
|
+
expect(response.messages.first["key"]).to eq "timeout"
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
[:get, :post].each do |method|
|
17
|
+
describe "Timeout for #{method} request" do
|
18
|
+
it_behaves_like "intelipost_request" do
|
19
|
+
let(:request_method) { method }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler.setup
|
3
|
+
require "factory_girl"
|
4
|
+
require "intelipost-api"
|
5
|
+
require "colorize"
|
6
|
+
|
7
|
+
['factories/*'].each do |p|
|
8
|
+
Dir[File.dirname(__FILE__) + "/#{p}"].each {|file| require file }
|
9
|
+
end
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
include Intelipost::Models
|
13
|
+
include FactoryGirl::Syntax::Methods
|
14
|
+
|
15
|
+
# rspec-expectations config goes here. You can use an alternate
|
16
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
17
|
+
# assertions if you prefer.
|
18
|
+
config.expect_with :rspec do |expectations|
|
19
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
20
|
+
# and `failure_message` of custom matchers include text for helper methods
|
21
|
+
# defined using `chain`, e.g.:
|
22
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
23
|
+
# # => "be bigger than 2 and smaller than 4"
|
24
|
+
# ...rather than:
|
25
|
+
# # => "be bigger than 2"
|
26
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
27
|
+
end
|
28
|
+
|
29
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
30
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
31
|
+
config.mock_with :rspec do |mocks|
|
32
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
33
|
+
# a real object. This is generally recommended, and will default to
|
34
|
+
# `true` in RSpec 4.
|
35
|
+
mocks.verify_partial_doubles = true
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: intelipost-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Renato Alves
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Intelipost API Gem for Ruby
|
42
|
+
email:
|
43
|
+
- renatodex@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- intelipost-api.gemspec
|
55
|
+
- lib/intelipost-api.rb
|
56
|
+
- lib/intelipost/api.rb
|
57
|
+
- lib/intelipost/api/components/cep_location.rb
|
58
|
+
- lib/intelipost/api/components/component.rb
|
59
|
+
- lib/intelipost/api/components/quote.rb
|
60
|
+
- lib/intelipost/api/facade.rb
|
61
|
+
- lib/intelipost/api/models/address.rb
|
62
|
+
- lib/intelipost/api/models/delivery_option.rb
|
63
|
+
- lib/intelipost/api/models/error_response.rb
|
64
|
+
- lib/intelipost/api/models/quote.rb
|
65
|
+
- lib/intelipost/api/models/volume.rb
|
66
|
+
- lib/intelipost/api/version.rb
|
67
|
+
- spec/factories/address.rb
|
68
|
+
- spec/factories/delivery_option.rb
|
69
|
+
- spec/factories/error_response.rb
|
70
|
+
- spec/factories/quote.rb
|
71
|
+
- spec/factories/volume.rb
|
72
|
+
- spec/lib/intelipost/api/components/cep_location_spec.rb
|
73
|
+
- spec/lib/intelipost/api/components/quote_spec.rb
|
74
|
+
- spec/lib/intelipost/api/facade_spec.rb
|
75
|
+
- spec/lib/intelipost/api_spec.rb
|
76
|
+
- spec/spec_helper.rb
|
77
|
+
homepage: http://github.com/renatodex/intelipost-ruby-api
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.4.2
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Intelipost API Gem for Ruby
|
101
|
+
test_files:
|
102
|
+
- spec/factories/address.rb
|
103
|
+
- spec/factories/delivery_option.rb
|
104
|
+
- spec/factories/error_response.rb
|
105
|
+
- spec/factories/quote.rb
|
106
|
+
- spec/factories/volume.rb
|
107
|
+
- spec/lib/intelipost/api/components/cep_location_spec.rb
|
108
|
+
- spec/lib/intelipost/api/components/quote_spec.rb
|
109
|
+
- spec/lib/intelipost/api/facade_spec.rb
|
110
|
+
- spec/lib/intelipost/api_spec.rb
|
111
|
+
- spec/spec_helper.rb
|