jamef 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6381fd8d477f322f0363131934c757c09810f63e
4
+ data.tar.gz: ac21ff9ae3a780ef49e87f08ef06ae6a0d98ffed
5
+ SHA512:
6
+ metadata.gz: 8d51530db62c2445688ed3fbd9e962c13963bc75763b4ec326e3c5306a5156e277b7c9c2f221e14d2bae01dabf1f0c43df3b13349bc71b2c118b91959a5c6269
7
+ data.tar.gz: 369eb855b6d20f178d0cfb501b44bc13e52fbafa64e4b46bdf9ba9ad71b8269db55e57c0049c1702851f07610b2633777dde63f1c9916afc607bcb41a2fedd77
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jamef.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Matheus Caceres
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Jamef
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'jamef'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install jamef
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jamef/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jamef"
8
+ spec.version = Jamef::VERSION
9
+ spec.authors = ["Matheus Caceres"]
10
+ spec.email = ["matheuscaceres@gmail.com"]
11
+ spec.description = %q{Integração com web service da Jamef}
12
+ spec.summary = %q{Jamed}
13
+ spec.homepage = ""
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 'activemodel'
22
+ spec.add_dependency 'actionpack'
23
+ spec.add_dependency 'rest-client'
24
+ spec.add_dependency 'nokogiri'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "rspec"
29
+ spec.add_development_dependency "fakeweb"
30
+ end
@@ -0,0 +1,40 @@
1
+ require 'active_model'
2
+ require 'action_view'
3
+ require 'nokogiri'
4
+ require 'rest_client'
5
+
6
+ Dir[File.dirname(__FILE__) + '/jamef/*.rb', File.dirname(__FILE__) + '/jamef/errors/*.rb'].each{|f| require(f)}
7
+
8
+ module Jamef
9
+ AJU = 174
10
+ BAR = 122
11
+ BAU = 111
12
+ BHZ = 1
13
+ BNU = 13
14
+ BSB = 163
15
+ CCM = 160
16
+ CPQ = 12
17
+ CXJ = 5
18
+ CWB = 10
19
+ FES = 192
20
+ FLN = 11
21
+ FOR = 177
22
+ GYN = 148
23
+ ITJ = 193
24
+ JAS = 4
25
+ JOI = 9
26
+ LDB = 8
27
+ MAO = 149
28
+ MCZ = 183
29
+ POA = 14
30
+ PSA = 159
31
+ RAO = 121
32
+ REC = 171
33
+ RIO = 3
34
+ SAO = 2
35
+ SJK = 125
36
+ SJP = 124
37
+ SSA = 168
38
+ UDI = 109
39
+ VIX = 7
40
+ end
@@ -0,0 +1,48 @@
1
+ module Jamef
2
+ class Cubage
3
+
4
+ attr_accessor :items
5
+
6
+ def initialize(items)
7
+ @items = items.kind_of?(Array) ? items : [items]
8
+ end
9
+
10
+ def total
11
+ Elegible.new('+', cubage_sum).total
12
+ end
13
+
14
+ def to_s
15
+ Elegible.new('+', cubage_sum).to_s
16
+ end
17
+
18
+ def price
19
+ Elegible.new('+', price_sum)
20
+ end
21
+
22
+ def weight
23
+ Elegible.new('+', weight_sum)
24
+ end
25
+
26
+ private
27
+
28
+ def cubage_sum
29
+ array_sum(:cubage)
30
+ end
31
+
32
+ def price_sum
33
+ array_sum(:price)
34
+ end
35
+
36
+ def weight_sum
37
+ array_sum(:weight)
38
+ end
39
+
40
+ def array_sum(variable)
41
+ sum = []
42
+ self.items.each{|item| sum << item.send(variable)}
43
+
44
+ sum
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,16 @@
1
+ module Jamef
2
+ class Elegible
3
+ include ActionView::Helpers::NumberHelper
4
+ attr_reader :total
5
+
6
+ def initialize(operator, *args)
7
+ args = args.shift if args[0].kind_of?(Array)
8
+ @total = args.inject(operator)
9
+ end
10
+
11
+ def to_s
12
+ number_to_currency(@total, separator: ",", delimiter: "", format: "%n")
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module Jamef
2
+ module Errors
3
+ class NoCarriage
4
+
5
+ def initialize
6
+ super("Carrige route no exist")
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Jamef
2
+ module Errors
3
+ class NoClassArg
4
+
5
+ def initialize
6
+ super "Pass the first argument"
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ module Jamef
2
+ class Item
3
+ include ActiveModel::Validations
4
+
5
+ validate :width, :height, :length, :quantity, :weight, :price, presence: true
6
+
7
+ attr_accessor :width, :height, :length, :quantity, :weight, :price
8
+
9
+ def initialize(width=nil, height=nil, length=nil, quantity=nil, weight=nil, price=nil)
10
+ @width = width
11
+ @height = height
12
+ @length = length
13
+ @quantity = quantity
14
+ @weight = weight
15
+ @price = price
16
+ end
17
+
18
+ def cubage
19
+ Elegible.new('*', self.width, self.height, self.length, self.quantity).total
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,47 @@
1
+ module Jamef
2
+ class Tax
3
+ attr_accessor :cubage, :region, :cnpj, :cep, :uf
4
+
5
+ URL = "http://www.jamef.com.br/internet/e-comerce/calculafrete_xml.asp"
6
+ XPATH = "//transporte/frete"
7
+
8
+ def initialize(cubage=nil, cnpj=nil, region=nil, cep=nil, uf=nil)
9
+ @cubage = cubage
10
+ @region = region.to_s
11
+ @cnpj = cnpj
12
+ @cep = cep
13
+ @uf = uf
14
+ end
15
+
16
+ def get
17
+ raise Errors::NoClassArg.new if self.cubage.nil?
18
+
19
+ node = Nokogiri::XML(get_xml)
20
+ code = node.xpath("#{XPATH}/status").first.text
21
+
22
+ if code.to_i == 2
23
+ raise NoCarriage.new
24
+ else
25
+ node.xpath("#{XPATH}/valor").first.text.sub(",", ".").to_f
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def get_xml
32
+ RestClient.get(URL, params).to_str
33
+ end
34
+
35
+ def params
36
+ {
37
+ P_CIC_NEGC: self.cnpj,
38
+ P_CEP: self.cep,
39
+ P_VLR_CARG: cubage.price.to_s,
40
+ P_PESO_KG: cubage.weight.to_s,
41
+ P_CUBG: cubage.to_s,
42
+ P_COD_REGN: self.region.to_s,
43
+ P_UF: self.uf
44
+ }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Jamef
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ items = [
4
+ Jamef::Item.new(10, 9, 8, 7, 6, 10.0),
5
+ Jamef::Item.new(8, 7, 6, 5, 4, 10.0),
6
+ ]
7
+
8
+ describe Jamef::Cubage do
9
+ before(:each) do
10
+ @cubage = Jamef::Cubage.new(items)
11
+ end
12
+
13
+ it "should return the cubage" do
14
+ @cubage.total.should == 6720
15
+ end
16
+
17
+ it "should human readable" do
18
+ @cubage.to_s.should == '6720,00'
19
+ end
20
+
21
+ it "should total price" do
22
+ @cubage.price.total.should == 20.0
23
+ end
24
+
25
+ it "should total weight" do
26
+ @cubage.weight.total.should == 10.0
27
+ end
28
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jamef::Elegible do
4
+ context "small numbers" do
5
+ subject{Jamef::Elegible.new('+',1,2,3,4)}
6
+
7
+ it "should return sum of args" do
8
+ subject.total.should eq(10)
9
+ end
10
+
11
+ it "should jamef send" do
12
+ subject.to_s.should == "10,00"
13
+ end
14
+ end
15
+
16
+ context "big numbers" do
17
+ subject{Jamef::Elegible.new('*', 1000, 1000.10)}
18
+
19
+ it "should return multiplier of args" do
20
+ subject.to_s.should == "1000100,00"
21
+ end
22
+ end
23
+
24
+ context 'pass array arg' do
25
+ subject{Jamef::Elegible.new('*', [100, 100])}
26
+
27
+ it "should return multiplier array" do
28
+ subject.total.should eql(10000)
29
+ subject.to_s.should == "10000,00"
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jamef::Item do
4
+ subject{Jamef::Item}
5
+
6
+ before(:each) do
7
+ @item = subject.new(10, 9, 8, 7, 6, 10.0)
8
+ end
9
+
10
+ it "should be valid" do
11
+ @item.should be_valid
12
+ end
13
+
14
+ it "should be valid pass arguments" do
15
+ item = subject.new
16
+ item.width = 10
17
+ item.height = 9
18
+ item.length = 8
19
+ item.quantity = 7
20
+ item.weight = 6
21
+ item.price = 10.0
22
+ item.should be_valid
23
+ end
24
+
25
+ it "should return self cubage" do
26
+ @item.cubage.should eq(5040)
27
+ end
28
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+ require 'fakeweb'
3
+
4
+ items = [
5
+ Jamef::Item.new(10, 9, 8, 7, 6, 10.0),
6
+ Jamef::Item.new(8, 7, 6, 5, 4, 10.0),
7
+ ]
8
+
9
+ describe Jamef::Tax do
10
+ subject{Jamef::Tax}
11
+
12
+ context "success" do
13
+ before(:each) do
14
+ page = File.open(File.join(File.dirname(__FILE__), '..', 'response', 'carrier_success.xml')).read
15
+ @cubage ||= Jamef::Cubage.new(items)
16
+
17
+ FakeWeb.register_uri(:get, %r|www\.jamef\.com\.br|, :body => page, content_type: 'text/xml')
18
+ FakeWeb.allow_net_connect = false
19
+ end
20
+
21
+ it "should return the tax" do
22
+ tax = subject.new
23
+ tax.cubage = @cubage
24
+ tax.cnpj = "1234"
25
+ tax.region = Jamef::RAO
26
+ tax.cep = "140000100"
27
+ tax.uf = "SP"
28
+
29
+ tax.get.should == 40.0
30
+ end
31
+ end
32
+
33
+ context "fail" do
34
+ describe "no pass arg cubage" do
35
+
36
+ it "should raise" do
37
+ tax = subject.new
38
+
39
+ expect{tax.get}.to raise_error
40
+ end
41
+
42
+ end
43
+
44
+ describe "code 2" do
45
+ before(:each) do
46
+ page = File.open(File.join(File.dirname(__FILE__), '..', 'response', 'carrier_error.xml')).read
47
+ @cubage ||= Jamef::Cubage.new(items)
48
+
49
+ FakeWeb.register_uri(:get, %r|www\.jamef\.com\.br|, :body => page, content_type: 'text/xml')
50
+ FakeWeb.allow_net_connect = false
51
+ end
52
+
53
+ it "should raise when show code two in status" do
54
+ tax = subject.new
55
+ tax.cubage = @cubage
56
+ tax.cnpj = "1234"
57
+ tax.region = Jamef::RAO
58
+ tax.cep = "140000100"
59
+ tax.uf = "SP"
60
+
61
+ expect{tax.get}.to raise_error
62
+ end
63
+
64
+ end
65
+ end
66
+
67
+ end
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="ISO-8859-1" ?>
2
+ <transporte>
3
+ <frete>
4
+ <status>2</status>
5
+ <msg>Rota nao atendida pela transportadora</msg>
6
+ <valor>0</valor>
7
+ </frete>
8
+ </transporte>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="ISO-8859-1" ?>
2
+ <transporte>
3
+ <frete>
4
+ <status>1</status>
5
+ <msg>OK</msg>
6
+ <valor>40,0</valor>
7
+ <transporte>R</transporte>
8
+ </frete>
9
+ </transporte>
@@ -0,0 +1,3 @@
1
+
2
+ require File.dirname(__FILE__) + "/../lib/jamef"
3
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each{|f| require f}
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jamef
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matheus Caceres
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionpack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: fakeweb
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Integração com web service da Jamef
126
+ email:
127
+ - matheuscaceres@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .gitignore
133
+ - .rspec
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - jamef.gemspec
139
+ - lib/jamef.rb
140
+ - lib/jamef/cubage.rb
141
+ - lib/jamef/elegible.rb
142
+ - lib/jamef/errors/no_carriage.rb
143
+ - lib/jamef/errors/no_class_arg.rb
144
+ - lib/jamef/item.rb
145
+ - lib/jamef/tax.rb
146
+ - lib/jamef/version.rb
147
+ - spec/jamef/cubage_spec.rb
148
+ - spec/jamef/elegible_spec.rb
149
+ - spec/jamef/item_spec.rb
150
+ - spec/jamef/tax_spec.rb
151
+ - spec/response/carrier_error.xml
152
+ - spec/response/carrier_success.xml
153
+ - spec/spec_helper.rb
154
+ homepage: ''
155
+ licenses:
156
+ - MIT
157
+ metadata: {}
158
+ post_install_message:
159
+ rdoc_options: []
160
+ require_paths:
161
+ - lib
162
+ required_ruby_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ! '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ! '>='
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ requirements: []
173
+ rubyforge_project:
174
+ rubygems_version: 2.0.3
175
+ signing_key:
176
+ specification_version: 4
177
+ summary: Jamed
178
+ test_files:
179
+ - spec/jamef/cubage_spec.rb
180
+ - spec/jamef/elegible_spec.rb
181
+ - spec/jamef/item_spec.rb
182
+ - spec/jamef/tax_spec.rb
183
+ - spec/response/carrier_error.xml
184
+ - spec/response/carrier_success.xml
185
+ - spec/spec_helper.rb