intelipost-rb 0.0.9

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: 8fca173b390a7dcf9ded781290d1bba55eb454b4
4
+ data.tar.gz: 7f018f1958cf404dfb877afe4c04f7b253a686e6
5
+ SHA512:
6
+ metadata.gz: 97f894aa75b607930444b30345dc1bf8c816ea03544644cd1b9aa34deb927f23136ab73eadaae1b68090c2808524fbf4121b8176138a4b49eb791b56a62a2c5f
7
+ data.tar.gz: ee175f252338a6b157f0f385a281fda08b440df49559b8db576cd2f3291f8a36fdf2b5a4bf13ae218af3f30ba1d20f7bd33e7536855ba5acd7e525490415b363
@@ -0,0 +1,31 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Documentation cache and generated files:
13
+ /.yardoc/
14
+ /_yardoc/
15
+ /doc/
16
+ /rdoc/
17
+
18
+ ## Environment normalisation:
19
+ /.bundle/
20
+ /vendor/bundle
21
+ /lib/bundler/man/
22
+ .env
23
+ spec/cassettes
24
+
25
+ # gem
26
+ Gemfile.lock
27
+ .ruby-version
28
+ .ruby-gemset
29
+
30
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
31
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,30 @@
1
+ cache: bundler
2
+
3
+ language: ruby
4
+
5
+ rvm:
6
+ - 2.0.0
7
+ - 2.1
8
+ - 2.2
9
+ - 2.3
10
+ - jruby-head
11
+ - rbx-2
12
+ - ruby-head
13
+
14
+ sudo: false
15
+
16
+ bundler_args: --retry=3 --jobs=3
17
+
18
+ env:
19
+ global:
20
+ - JRUBY_OPTS="$JRUBY_OPTS --debug"
21
+
22
+ matrix:
23
+ allow_failures:
24
+ - rvm: jruby-head
25
+ - rvm: rbx-2
26
+ fast_finish: true
27
+
28
+ addons:
29
+ code_climate:
30
+ repo_token: 6ae54518b280ed0fe7aab012f10000f75e2684e62c306785a9ca54954b1faccc
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Natuelabs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,84 @@
1
+ [![Gem Version](https://badge.fury.io/rb/intelipost.svg)](http://badge.fury.io/rb/intelipost)
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
+
6
+
7
+ # intelipost
8
+ Gem for the Intelipost API
9
+
10
+ ### Usage:
11
+
12
+ #### Address Complete (CEP)
13
+
14
+ ````ruby
15
+ # GET /cep_address/complete
16
+ require 'intelipost'
17
+
18
+ client = Intelipost::Client.new api_key: <your api key>
19
+ address = client.cep.address_complete.get '05307000'
20
+ # => #<Hashie::Mash content=#<Hashie::Mash bairro="Vila Ribeiro de Barros" city="São Paulo" ibge="3550308" neighborhood="Vila Ribeiro de Barros" state="São Paulo" state_short="SP" street="R Maj Paladino"> messages=[] status="OK" time="0.6 ms">
21
+ address.content.street
22
+ # => "R Maj Paladino"
23
+ ````
24
+
25
+ #### Quote (Cotação)
26
+
27
+ ````ruby
28
+ # POST /quote
29
+ require 'intelipost'
30
+
31
+ client = Intelipost::Client.new api_key: <your api key>
32
+ quote = client.quote.create({hash_of: :intelipost, args: :values})
33
+ # => #<Hashie::Mash content=#<Hashie::Mash additional_information=#<Hashie::Mash client_type="gold" delivery_method_ids=[4, 3, 2] extra_cost_absolute=0.0 extra_cost_percentage=0.0 free_shipping=false lead_time_business_days=0 sales_channel="hotsite" tax_id=nil> client_id=1783 created=1433872646799 created_iso="2015-06-09T14:57:26.799-03:00" delivery_options=[#<Hashie::Mash delivery_estimate_business_days=1 delivery_method_id=4 delivery_method_name="Total Express" delivery_method_type="EXPRESS" delivery_note=nil description="Total Express" final_shipping_cost=5.05 logistic_provider_name="Total" provider_shipping_cost=5.05>, #<Hashie::Mash delivery_estimate_business_days=1 delivery_method_id=3 delivery_method_name="Correios eSedex" delivery_method_type="EXPRESS" delivery_note=nil description="Correios eSedex" final_shipping_cost=7.83 logistic_provider_name="Correios" provider_shipping_cost=7.83>, #<Hashie::Mash delivery_estimate_business_days=1 delivery_method_id=2 delivery_method_name="Correios Sedex" delivery_method_type="EXPRESS" delivery_note=nil description="Correios Sedex" final_shipping_cost=13.83 logistic_provider_name="Correios" provider_shipping_cost=13.83>] destination_zip_code="06396-200" id=4347667 origin_zip_code="04037-003" platform=nil volumes=[#<Hashie::Mash cost_of_goods=100.0 description=nil height=10.0 length=10.0 volume_type="BOX" weight=0.1 width=10.0>]> messages=[] status="OK" time="34.0 ms">
34
+ quote.content.id
35
+ # => 4347667
36
+ ````
37
+
38
+ ````ruby
39
+ # GET /quote/{id}
40
+ require 'intelipost'
41
+ client = Intelipost::Client.new api_key: <your api key>
42
+ quote = client.quote.get(124560)
43
+ ````
44
+
45
+ #### Shipment Order (Pedido de Envio)
46
+
47
+ ````ruby
48
+ # POST /shipment_order
49
+ require 'intelipost'
50
+
51
+ client = Intelipost::Client.new api_key: <your api key>
52
+ client.shipment_order.create({hash_of: :intelipost, args: :values})
53
+ ````
54
+
55
+ ````ruby
56
+ # POST /shipment_order/set_invoice
57
+ require 'intelipost'
58
+
59
+ client = Intelipost::Client.new api_key: <your api key>
60
+ client.shipment_order.set_invoice.update({hash_of: :intelipost, args: :values})
61
+ ````
62
+
63
+ ````ruby
64
+ # POST /shipment_order/set_tracking_data
65
+ require 'intelipost'
66
+
67
+ client = Intelipost::Client.new api_key: <your api key>
68
+ client.shipment_order.set_tracking_data.update({hash_of: :intelipost, args: :values})
69
+ ````
70
+
71
+ ````ruby
72
+ # GET /shipment_order/read_status/{order_number}
73
+ require 'intelipost'
74
+
75
+ client = Intelipost::Client.new api_key: <your api key>
76
+ client.shipment_order.read_status.get(124560)
77
+ ````
78
+
79
+ ### Development:
80
+
81
+ For testing, create a `.env` file with the following content:
82
+ ````bash
83
+ INTELIPOST_API_KEY=<YOUR API KEY>
84
+ ````
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :coverage do
7
+ ENV['COVERAGE'] = 'true'
8
+ Rake::Task["spec"].execute
9
+ end
10
+
11
+ task :console do
12
+ sh "irb -rubygems -r intelipost-rb"
13
+ end
14
+
15
+ task :default => :spec
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'intelipost/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'intelipost-rb'
8
+ spec.version = Intelipost::VERSION
9
+ spec.authors = ['Celestino Gomes', 'Luiz Rocha']
10
+ spec.email = ['tinorj@gmail.com', 'lsdrocha@gmail.com']
11
+ spec.description = %q{Gem to access the REST API of Intelipost}
12
+ spec.summary = %q{Gem to access the REST API of Intelipost}
13
+ spec.homepage = 'http://github.com/gaveteiro/intelipost-rb'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'faraday', '~> 0.11.0'
22
+ spec.add_dependency 'faraday_middleware', '~> 0.11.0'
23
+ spec.add_dependency 'hashie', '~> 3.4.1'
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'
33
+ end
@@ -0,0 +1 @@
1
+ require 'intelipost'
@@ -0,0 +1,14 @@
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'
11
+ require 'intelipost/shipment_order'
12
+
13
+ module Intelipost
14
+ end
@@ -0,0 +1,5 @@
1
+ module Intelipost
2
+ class Cep < Intelipost::FluentInterfaceBase
3
+ set_endpoint 'cep_location'
4
+ end
5
+ end
@@ -0,0 +1,66 @@
1
+ require "intelipost/mash"
2
+
3
+ module Intelipost
4
+ class Client
5
+ HOST = "api.intelipost.com.br"
6
+ SERVICE = "api/"
7
+ API_VERSION = "v1"
8
+ attr_reader :uri, :options, :connection, :api_key, :platform
9
+
10
+ def initialize(options)
11
+ @options = { ssl: true }
12
+ @options.merge! options
13
+ uri = @options[:ssl] ? "https://" : "http://"
14
+ uri.concat HOST
15
+
16
+ @uri = URI.join uri, SERVICE, API_VERSION
17
+ @api_key = @options[:api_key]
18
+ @platform = @options[:platform]
19
+
20
+ @connection = connection
21
+ end
22
+
23
+ def connection
24
+ raise ArgumentError, "an api_key is required to connect" if api_key.nil?
25
+
26
+ Faraday::Middleware.register_middleware gzip: lambda { FaradayMiddleware::Gzip }
27
+ Faraday.new(url: @uri.to_s) do |conn|
28
+ conn.request :json
29
+
30
+ conn.response :mashify, { mash_class: Intelipost::Mash }
31
+ conn.response :json
32
+
33
+ conn.headers["api_key"] = api_key
34
+ conn.headers["platform"] = platform unless platform.nil?
35
+ conn.adapter :net_http
36
+ conn.use :gzip
37
+ conn.proxy @options[:proxy]
38
+ end
39
+ end
40
+
41
+ def get(endpoint, args={})
42
+ connection.get(endpoint, args).body
43
+ end
44
+
45
+ def post(endpoint, args = {})
46
+ connection.post(endpoint, args).body
47
+ end
48
+
49
+ def method_missing(method, *args, &block)
50
+ method = camelize method
51
+ if Intelipost.const_defined? method
52
+ return Intelipost.const_get(method).new self
53
+ else
54
+ super
55
+ end
56
+ end
57
+
58
+ def camelize(term)
59
+ string = term.to_s
60
+ string = string.sub(/^[a-z\d]*/) { $&.capitalize }
61
+ string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
62
+ string.gsub!("/", "::")
63
+ string
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,48 @@
1
+ module Intelipost
2
+ module FluentInterface
3
+
4
+ def self.included(cls)
5
+ cls.extend(ClassMethods)
6
+ end
7
+
8
+ def create(post_values)
9
+ connection.post(@fluent_interfaces.join('/'), post_values)
10
+ end
11
+
12
+ def update(post_values)
13
+ create(post_values)
14
+ end
15
+
16
+ def get(value)
17
+ connection.get([@fluent_interfaces, value].join('/'))
18
+ end
19
+
20
+ def method_missing(method, *args, &block)
21
+ add_fluent_interface_path(method)
22
+ self
23
+ end
24
+
25
+ private
26
+ def add_fluent_interface_path(method)
27
+ @fluent_interfaces << method
28
+ end
29
+
30
+ module ClassMethods
31
+ def set_endpoint endpoint
32
+ define_method :endpoint do
33
+ endpoint
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ class FluentInterfaceBase
40
+ include Intelipost::FluentInterface
41
+ attr_accessor :connection
42
+
43
+ def initialize(connection)
44
+ @connection = connection
45
+ @fluent_interfaces = [endpoint]
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,16 @@
1
+ module Intelipost
2
+ class Mash < ::Hashie::Mash
3
+ def success?
4
+ self.has_key?('status') and self.status == 'OK'
5
+ end
6
+
7
+ def failure?
8
+ !success?
9
+ end
10
+
11
+ def all_messages
12
+ return unless self.has_key?('messages')
13
+ self.messages.map(&:text).join(';')
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ module Intelipost
2
+ class Quote < Intelipost::FluentInterfaceBase
3
+ set_endpoint 'quote'
4
+ end
5
+
6
+ class QuoteByProduct < Intelipost::FluentInterfaceBase
7
+ set_endpoint 'quote_by_product'
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Intelipost
2
+ class ShipmentOrder < Intelipost::FluentInterfaceBase
3
+ set_endpoint 'shipment_order'
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Intelipost
2
+ VERSION = '0.0.9'
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
@@ -0,0 +1,10 @@
1
+ describe Intelipost::Cep do
2
+ subject { Intelipost::Cep.new double('Intelipost::Client') }
3
+
4
+ describe '#address_complete' do
5
+ it 'correctly queries the cep' do
6
+ expect(subject.connection).to receive(:get).with('cep_location/address_complete/00000000')
7
+ subject.address_complete.get '00000000'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Intelipost::Client, :vcr do
4
+ subject { described_class.new api_key: ENV["INTELIPOST_API_KEY"], platform: "intelipost-gem-test" }
5
+
6
+ context "when initialized with no options" do
7
+ subject { described_class }
8
+
9
+ it "raises ArgumentError if initialized with no options" do
10
+ expect do
11
+ described_class.new
12
+ end.to raise_error ArgumentError
13
+ end
14
+
15
+ it "raises ArgumentError if initialized without credentials" do
16
+ expect do
17
+ described_class.new api_key: nil
18
+ end.to raise_error ArgumentError
19
+ end
20
+ end
21
+
22
+ context "when call for an non existing defined class" do
23
+ it ".method_missing" do
24
+ expect { subject.foobar(1) }.to raise_error ArgumentError
25
+ end
26
+ end
27
+
28
+ context "without platform" do
29
+ subject { described_class.new api_key: ENV["INTELIPOST_API_KEY"] }
30
+
31
+ describe "#connection" do
32
+ it "is an instance of Faraday::Connection" do
33
+ expect(subject.connection).to be_an_instance_of Faraday::Connection
34
+ end
35
+
36
+ it "has the api_key header on the request" do
37
+ expect(subject.connection.headers).to include("api_key")
38
+ end
39
+
40
+ it "has not the platform header on the request" do
41
+ expect(subject.connection.headers).not_to include("platform")
42
+ end
43
+ end
44
+ end
45
+
46
+ context "initialized with credentials" do
47
+ describe "#connection" do
48
+ it "is an instance of Faraday::Connection" do
49
+ expect(subject.connection).to be_an_instance_of Faraday::Connection
50
+ end
51
+
52
+ it "has the api_key header on the request" do
53
+ expect(subject.connection.headers).to include("api_key")
54
+ end
55
+
56
+ it "has the platform header on the request" do
57
+ expect(subject.connection.headers).to include("platform")
58
+ end
59
+ end
60
+ end
61
+
62
+ context "dealing with options" do
63
+ it "includes api_key" do
64
+ expect(subject.options).to include(:api_key)
65
+ end
66
+
67
+ it "includes platform" do
68
+ expect(subject.options).to include(:platform)
69
+ end
70
+ end
71
+
72
+ context "dealing with zipcode (cep)" do
73
+ it "returns a Intelipost::Mash on successful query" do
74
+ response = subject.cep.address_complete.get("04661100")
75
+ expect(response.class).to eq Intelipost::Mash
76
+ expect(response.success?).to eq true
77
+ expect(response.failure?).to eq false
78
+ expect(response.all_messages).to eq ""
79
+ expect(response.messages).to be_empty
80
+ end
81
+ end
82
+
83
+ context "dealing with Quotes" do
84
+ let(:volumes) do
85
+ {
86
+ "origin_zip_code" => "04037-003",
87
+ "destination_zip_code" => "06396-200",
88
+ "volumes" => [
89
+ {
90
+ "weight" => 0.1,
91
+ "volume_type" => "BOX",
92
+ "cost_of_goods" => 100,
93
+ "width" => 10,
94
+ "height" => 10,
95
+ "length" => 10
96
+ }
97
+ ],
98
+ "additional_information" => {
99
+ "free_shipping" => false,
100
+ "extra_cost_absolute" => 0,
101
+ "extra_cost_percentage" => 0,
102
+ "lead_time_business_days" => 0,
103
+ "sales_channel" => "hotsite",
104
+ "client_type" => "gold",
105
+ "delivery_method_ids" => [ 4, 3, 2 ]
106
+ }
107
+ }
108
+ end
109
+
110
+ it ".quote.create" do
111
+ expect(subject.quote.create(volumes)).to have_key(:content)
112
+ end
113
+
114
+ it ".quote.create.failure" do
115
+ response = subject.quote.create({"failure"=>"failure"})
116
+ expect(response.success?).to eq false
117
+ expect(response.failure?).to eq true
118
+ expect(response.all_messages).not_to eq ""
119
+ expect(response.messages).not_to be_empty
120
+ end
121
+
122
+ it ".quote.get(#)" do
123
+ quote_id = subject.quote.create(volumes).content.id
124
+ expect(subject.quote.get(quote_id)).to have_key(:content)
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,19 @@
1
+ describe Intelipost::Mash do
2
+ it 'will create a intelipost mash and check if it is a success response' do
3
+ mash = Intelipost::Mash.new({status: 'OK'})
4
+ expect(mash.success?).to eq true
5
+ expect(mash.failure?).to eq false
6
+ expect(mash.all_messages).to eq nil
7
+ end
8
+
9
+ it 'will create a intelipost mash and check if it is a failure response' do
10
+ mash = Intelipost::Mash.new({messages:[
11
+ {text: 'some error message'},
12
+ {text: 'some error message2'}
13
+ ]})
14
+ expect(mash.success?).to eq false
15
+ expect(mash.failure?).to eq true
16
+ expect(mash.all_messages).to eq 'some error message;some error message2'
17
+ end
18
+
19
+ end
@@ -0,0 +1,75 @@
1
+ describe Intelipost::Quote do
2
+ subject { described_class.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
+
39
+ it 'will pull an existing quote by its id' do
40
+ id = 99
41
+ expect(subject.connection).to receive(:get).with("quote/#{id}")
42
+ subject.get(99)
43
+ end
44
+ end
45
+
46
+ describe Intelipost::QuoteByProduct do
47
+ subject { described_class.new(double('Intelipost::Client')) }
48
+
49
+ let(:by_products) do
50
+ {
51
+ 'origin_zip_code' => '04037-003',
52
+ 'destination_zip_code' => '06396-200',
53
+ 'products' => [
54
+ {
55
+ 'weight' => 0.1,
56
+ 'cost_of_goods'=> 100,
57
+ 'width' => 10,
58
+ 'height' => 10,
59
+ 'length' => 10,
60
+ 'quantity' => 1,
61
+ 'sku_id' => 'SKU', # optional
62
+ 'product_category' => 'product category', # optional
63
+ 'description' => 'description', #optional
64
+ 'can_group' => 'true' # optional
65
+ }
66
+ ]
67
+ }
68
+ end
69
+
70
+ it 'correctly fetch for a quote' do
71
+ expect(subject.connection).to receive(:post).with('quote_by_product', by_products)
72
+
73
+ subject.create(by_products)
74
+ end
75
+ end
@@ -0,0 +1,111 @@
1
+ # coding: utf-8
2
+
3
+ describe Intelipost::ShipmentOrder do
4
+ subject { Intelipost::ShipmentOrder.new(double('Intelipost::Client')) }
5
+
6
+ let(:order_to_ship) do
7
+ {
8
+ 'quote_id' => 59094,
9
+ 'delivery_method_id' => 2,
10
+ 'end_customer' => {
11
+ 'first_name' => 'Teste',
12
+ 'last_name' => 'QATLT',
13
+ 'email' => 'talita.sousa@intelipost.com.br',
14
+ 'phone' => '00231424551',
15
+ 'cellphone' => '01231233123',
16
+ 'is_company' => false,
17
+ 'federal_tax_payer_id' => '44611341801',
18
+ 'state_tax_payer_id' => '2314234',
19
+ 'shipping_address' => 'Avenida Paulista',
20
+ 'shipping_number' => '2',
21
+ 'shipping_additional' => 'Apto 202',
22
+ 'shipping_reference' => 'Supermercado Superpão',
23
+ 'shipping_quarter' => 'Consolação',
24
+ 'shipping_city' => 'São Paulo',
25
+ 'shipping_state' => 'São Paulo',
26
+ 'shipping_zip_code' => '01311-000',
27
+ 'shipping_country' => 'BR'
28
+ },
29
+ 'shipment_order_volume_array' => [
30
+ {
31
+ 'shipment_order_volume_number' => '1',
32
+ 'weight' => 3.2,
33
+ 'volume_type_code' => 'box',
34
+ 'width' => 10,
35
+ 'height' => 20,
36
+ 'length' => 30,
37
+ 'products_nature' => 'beverages',
38
+ 'products_quantity' => 3,
39
+ 'is_icms_exempt' => false,
40
+ 'tracking_code' => 'SW123456789BR',
41
+ 'shipment_order_volume_invoice' => {
42
+ 'invoice_series' => '1',
43
+ 'invoice_number' => '1000',
44
+ 'invoice_key' => '41140502834982004563550010000084111000132317',
45
+ 'invoice_date' => '12-03-14',
46
+ 'invoice_total_value' => '45.99',
47
+ 'invoice_products_value' => '39.99',
48
+ 'invoice_cfop' => '2890'
49
+ }
50
+ }
51
+ ],
52
+ 'order_number' => '09123241214TLT',
53
+ 'estimated_delivery_date' => '2014-12-15'
54
+ }
55
+ end
56
+
57
+ it 'correctly submit a shipment order' do
58
+ expect(subject.connection).to receive(:post).with('shipment_order', order_to_ship)
59
+ subject.create(order_to_ship)
60
+ end
61
+
62
+ let(:order_invoice) do
63
+ {
64
+ 'order_number' => '12314324',
65
+ 'shipment_order_volume_invoice_array' => [
66
+ {
67
+ 'shipment_order_volume_number' => '1',
68
+ 'invoice_series' => '123123123',
69
+ 'invoice_number' => 'BR283248123',
70
+ 'invoice_key' => 'CDFx2342396078192310231982',
71
+ 'invoice_date' => '2014-02-28',
72
+ 'invoice_total_value' => '32,49',
73
+ 'invoice_products_value' => '28,99',
74
+ 'invoice_cfop' => '5120'
75
+ }
76
+ ]
77
+ }
78
+ end
79
+
80
+ it 'invoice update' do
81
+ expect(subject.connection).to receive(:post).with('shipment_order/set_invoice', order_invoice)
82
+ subject.set_invoice.update(order_invoice)
83
+ end
84
+
85
+ let(:order_tracking_code) do
86
+ {
87
+ 'order_number' => 'BR12345',
88
+ 'tracking_data_array' => [
89
+ {
90
+ 'shipment_order_volume_number' => 1,
91
+ 'tracking_code' => 'SW123456789BR'
92
+ },
93
+ {
94
+ 'shipment_order_volume_number' => 2,
95
+ 'tracking_code' => 'SW123456789BR'
96
+ }
97
+ ]
98
+ }
99
+ end
100
+
101
+ it 'tracking update' do
102
+ expect(subject.connection).to receive(:post).with('shipment_order/set_tracking_data', order_tracking_code)
103
+ subject.set_tracking_data.update(order_tracking_code)
104
+ end
105
+
106
+ it 'read status from API' do
107
+ order_id = 128492394
108
+ expect(subject.connection).to receive(:get).with("shipment_order/read_status/#{order_id}")
109
+ subject.read_status.get(order_id)
110
+ end
111
+ end
@@ -0,0 +1,29 @@
1
+ require 'simplecov'
2
+ require "codeclimate-test-reporter"
3
+
4
+ CodeClimate::TestReporter.start
5
+ SimpleCov.start do
6
+ add_filter '/spec'
7
+ end if ENV["COVERAGE"]
8
+
9
+ require File.expand_path("../../lib/intelipost", __FILE__)
10
+ require 'intelipost'
11
+ require 'dotenv'
12
+ require 'vcr'
13
+
14
+ Dotenv.load
15
+
16
+ VCR.configure do |c|
17
+ c.cassette_library_dir = 'spec/cassettes'
18
+ c.hook_into :webmock
19
+ c.configure_rspec_metadata!
20
+ c.ignore_hosts 'codeclimate.com'
21
+ end
22
+
23
+ RSpec.configure do |config|
24
+ config.expect_with :rspec do |c|
25
+ c.syntax = :expect
26
+ end
27
+
28
+ config.order = "random"
29
+ end
metadata ADDED
@@ -0,0 +1,230 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: intelipost-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.9
5
+ platform: ruby
6
+ authors:
7
+ - Celestino Gomes
8
+ - Luiz Rocha
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-03-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 0.11.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 0.11.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: faraday_middleware
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 0.11.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 0.11.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: hashie
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 3.4.1
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 3.4.1
56
+ - !ruby/object:Gem::Dependency
57
+ name: bundler
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.7.6
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 1.7.6
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 10.4.2
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 10.4.2
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: 3.2.0
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: 3.2.0
98
+ - !ruby/object:Gem::Dependency
99
+ name: vcr
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: 2.9.3
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: 2.9.3
112
+ - !ruby/object:Gem::Dependency
113
+ name: webmock
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: '1.21'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '1.21'
126
+ - !ruby/object:Gem::Dependency
127
+ name: simplecov
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - "~>"
131
+ - !ruby/object:Gem::Version
132
+ version: '0.10'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '0.10'
140
+ - !ruby/object:Gem::Dependency
141
+ name: dotenv
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - "~>"
145
+ - !ruby/object:Gem::Version
146
+ version: 2.0.1
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - "~>"
152
+ - !ruby/object:Gem::Version
153
+ version: 2.0.1
154
+ - !ruby/object:Gem::Dependency
155
+ name: codeclimate-test-reporter
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - "~>"
159
+ - !ruby/object:Gem::Version
160
+ version: 0.4.7
161
+ type: :development
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: 0.4.7
168
+ description: Gem to access the REST API of Intelipost
169
+ email:
170
+ - tinorj@gmail.com
171
+ - lsdrocha@gmail.com
172
+ executables: []
173
+ extensions: []
174
+ extra_rdoc_files: []
175
+ files:
176
+ - ".gitignore"
177
+ - ".rspec"
178
+ - ".travis.yml"
179
+ - Gemfile
180
+ - LICENSE
181
+ - README.md
182
+ - Rakefile
183
+ - intelipost-rb.gemspec
184
+ - lib/intelipost-rb.rb
185
+ - lib/intelipost.rb
186
+ - lib/intelipost/cep.rb
187
+ - lib/intelipost/client.rb
188
+ - lib/intelipost/fluent_interface.rb
189
+ - lib/intelipost/mash.rb
190
+ - lib/intelipost/quote.rb
191
+ - lib/intelipost/shipment_order.rb
192
+ - lib/intelipost/version.rb
193
+ - lib/middleware/gzip.rb
194
+ - spec/lib/intelipost/cep_spec.rb
195
+ - spec/lib/intelipost/client_spec.rb
196
+ - spec/lib/intelipost/mash_spec.rb
197
+ - spec/lib/intelipost/quote_spec.rb
198
+ - spec/lib/intelipost/shipment_order_spec.rb
199
+ - spec/spec_helper.rb
200
+ homepage: http://github.com/gaveteiro/intelipost-rb
201
+ licenses:
202
+ - MIT
203
+ metadata: {}
204
+ post_install_message:
205
+ rdoc_options: []
206
+ require_paths:
207
+ - lib
208
+ required_ruby_version: !ruby/object:Gem::Requirement
209
+ requirements:
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ version: '0'
213
+ required_rubygems_version: !ruby/object:Gem::Requirement
214
+ requirements:
215
+ - - ">="
216
+ - !ruby/object:Gem::Version
217
+ version: '0'
218
+ requirements: []
219
+ rubyforge_project:
220
+ rubygems_version: 2.5.2
221
+ signing_key:
222
+ specification_version: 4
223
+ summary: Gem to access the REST API of Intelipost
224
+ test_files:
225
+ - spec/lib/intelipost/cep_spec.rb
226
+ - spec/lib/intelipost/client_spec.rb
227
+ - spec/lib/intelipost/mash_spec.rb
228
+ - spec/lib/intelipost/quote_spec.rb
229
+ - spec/lib/intelipost/shipment_order_spec.rb
230
+ - spec/spec_helper.rb