spree_correios 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ \#*
2
+ *~
3
+ .#*
4
+ .DS_Store
5
+ .idea
6
+ .project
7
+ tmp
8
+ nbproject
9
+ *.swp
10
+ spec/dummy
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'ffaker'
5
+ gem 'rspec'
6
+ gem 'simplecov', require: false
7
+ gem 'guard-rspec'
8
+ gem 'growl'
9
+ end
10
+
11
+ if RUBY_VERSION < "1.9"
12
+ gem "ruby-debug"
13
+ else
14
+ gem "ruby-debug19"
15
+ end
16
+
17
+ gemspec
18
+
19
+ gem 'spree', git: 'https://github.com/spree/spree.git', branch: '1-0-stable'
data/Gemfile.lock ADDED
@@ -0,0 +1,271 @@
1
+ GIT
2
+ remote: https://github.com/spree/spree.git
3
+ revision: 75e66c5b8b9967fc74a36857d817ad1134d87f0c
4
+ branch: 1-0-stable
5
+ specs:
6
+ spree (1.0.0)
7
+ spree_api (= 1.0.0)
8
+ spree_auth (= 1.0.0)
9
+ spree_cmd (= 1.0.0)
10
+ spree_core (= 1.0.0)
11
+ spree_dash (= 1.0.0)
12
+ spree_promo (= 1.0.0)
13
+ spree_sample (= 1.0.0)
14
+ spree_api (1.0.0)
15
+ spree_auth (= 1.0.0)
16
+ spree_core (= 1.0.0)
17
+ spree_auth (1.0.0)
18
+ cancan (= 1.6.7)
19
+ devise (= 1.4.8)
20
+ spree_core (= 1.0.0)
21
+ spree_cmd (1.0.0)
22
+ rails (>= 3.1.1, <= 3.1.3)
23
+ spree_core (1.0.0)
24
+ activemerchant (= 1.20.1)
25
+ acts_as_list (= 0.1.4)
26
+ deface (>= 0.7.2)
27
+ ffaker (~> 1.12.0)
28
+ highline (= 1.6.8)
29
+ jquery-rails (>= 1.0.18, <= 1.0.19)
30
+ kaminari (>= 0.13.0)
31
+ meta_search (= 1.1.1)
32
+ nested_set (= 1.6.8)
33
+ paperclip (= 2.5.0)
34
+ rails (>= 3.1.1, <= 3.1.3)
35
+ state_machine (= 1.1.1)
36
+ stringex (~> 1.3.0)
37
+ spree_dash (1.0.0)
38
+ spree_core (= 1.0.0)
39
+ spree_promo (1.0.0)
40
+ spree_auth (= 1.0.0)
41
+ spree_core (= 1.0.0)
42
+ spree_sample (1.0.0)
43
+ spree_core (= 1.0.0)
44
+
45
+ PATH
46
+ remote: .
47
+ specs:
48
+ spree_correios (1.0.0)
49
+ correios-frete (~> 1.5.2)
50
+ spree_core (~> 1.0.0)
51
+
52
+ GEM
53
+ remote: http://rubygems.org/
54
+ specs:
55
+ actionmailer (3.1.3)
56
+ actionpack (= 3.1.3)
57
+ mail (~> 2.3.0)
58
+ actionpack (3.1.3)
59
+ activemodel (= 3.1.3)
60
+ activesupport (= 3.1.3)
61
+ builder (~> 3.0.0)
62
+ erubis (~> 2.7.0)
63
+ i18n (~> 0.6)
64
+ rack (~> 1.3.5)
65
+ rack-cache (~> 1.1)
66
+ rack-mount (~> 0.8.2)
67
+ rack-test (~> 0.6.1)
68
+ sprockets (~> 2.0.3)
69
+ active_utils (1.0.3)
70
+ activesupport (>= 2.3.11)
71
+ i18n
72
+ activemerchant (1.20.1)
73
+ active_utils (>= 1.0.1)
74
+ activesupport (>= 2.3.11)
75
+ braintree (>= 2.0.0)
76
+ builder (>= 2.0.0)
77
+ i18n
78
+ json (>= 1.5.1)
79
+ money (<= 3.7.1)
80
+ activemodel (3.1.3)
81
+ activesupport (= 3.1.3)
82
+ builder (~> 3.0.0)
83
+ i18n (~> 0.6)
84
+ activerecord (3.1.3)
85
+ activemodel (= 3.1.3)
86
+ activesupport (= 3.1.3)
87
+ arel (~> 2.2.1)
88
+ tzinfo (~> 0.3.29)
89
+ activeresource (3.1.3)
90
+ activemodel (= 3.1.3)
91
+ activesupport (= 3.1.3)
92
+ activesupport (3.1.3)
93
+ multi_json (~> 1.0)
94
+ acts_as_list (0.1.4)
95
+ archive-tar-minitar (0.5.2)
96
+ arel (2.2.3)
97
+ bcrypt-ruby (3.0.1)
98
+ braintree (2.13.4)
99
+ builder (>= 2.0.0)
100
+ builder (3.0.0)
101
+ cancan (1.6.7)
102
+ capybara (1.0.1)
103
+ mime-types (>= 1.16)
104
+ nokogiri (>= 1.3.3)
105
+ rack (>= 1.0.0)
106
+ rack-test (>= 0.5.4)
107
+ selenium-webdriver (~> 2.0)
108
+ xpath (~> 0.1.4)
109
+ childprocess (0.3.1)
110
+ ffi (~> 1.0.6)
111
+ cocaine (0.2.1)
112
+ columnize (0.3.6)
113
+ correios-frete (1.5.2)
114
+ log-me (~> 0.0.2)
115
+ nokogiri (~> 1.5.0)
116
+ sax-machine (~> 0.1.0)
117
+ deface (0.7.2)
118
+ nokogiri (~> 1.5.0)
119
+ rails (>= 3.0.9)
120
+ devise (1.4.8)
121
+ bcrypt-ruby (~> 3.0)
122
+ orm_adapter (~> 0.0.3)
123
+ warden (~> 1.0.3)
124
+ diff-lcs (1.1.3)
125
+ erubis (2.7.0)
126
+ factory_girl (2.6.0)
127
+ activesupport (>= 2.3.9)
128
+ ffaker (1.12.1)
129
+ ffi (1.0.11)
130
+ growl (1.0.3)
131
+ guard (1.0.0)
132
+ ffi (>= 0.5.0)
133
+ thor (~> 0.14.6)
134
+ guard-rspec (0.6.0)
135
+ guard (>= 0.10.0)
136
+ highline (1.6.8)
137
+ hike (1.2.1)
138
+ i18n (0.6.0)
139
+ jquery-rails (1.0.19)
140
+ railties (~> 3.0)
141
+ thor (~> 0.14)
142
+ json (1.6.5)
143
+ kaminari (0.13.0)
144
+ actionpack (>= 3.0.0)
145
+ activesupport (>= 3.0.0)
146
+ railties (>= 3.0.0)
147
+ linecache19 (0.5.12)
148
+ ruby_core_source (>= 0.1.4)
149
+ log-me (0.0.2)
150
+ mail (2.3.0)
151
+ i18n (>= 0.4.0)
152
+ mime-types (~> 1.16)
153
+ treetop (~> 1.4.8)
154
+ meta_search (1.1.1)
155
+ actionpack (~> 3.1.0)
156
+ activerecord (~> 3.1.0)
157
+ activesupport (~> 3.1.0)
158
+ polyamorous (~> 0.5.0)
159
+ mime-types (1.17.2)
160
+ money (3.7.1)
161
+ i18n (~> 0.4)
162
+ multi_json (1.1.0)
163
+ nested_set (1.6.8)
164
+ activerecord (>= 3.0.0)
165
+ railties (>= 3.0.0)
166
+ nokogiri (1.5.0)
167
+ orm_adapter (0.0.6)
168
+ paperclip (2.5.0)
169
+ activerecord (>= 2.3.0)
170
+ activesupport (>= 2.3.2)
171
+ cocaine (>= 0.0.2)
172
+ mime-types
173
+ polyamorous (0.5.0)
174
+ activerecord (~> 3.0)
175
+ polyglot (0.3.3)
176
+ rack (1.3.6)
177
+ rack-cache (1.1)
178
+ rack (>= 0.4)
179
+ rack-mount (0.8.3)
180
+ rack (>= 1.0.0)
181
+ rack-ssl (1.3.2)
182
+ rack
183
+ rack-test (0.6.1)
184
+ rack (>= 1.0)
185
+ rails (3.1.3)
186
+ actionmailer (= 3.1.3)
187
+ actionpack (= 3.1.3)
188
+ activerecord (= 3.1.3)
189
+ activeresource (= 3.1.3)
190
+ activesupport (= 3.1.3)
191
+ bundler (~> 1.0)
192
+ railties (= 3.1.3)
193
+ railties (3.1.3)
194
+ actionpack (= 3.1.3)
195
+ activesupport (= 3.1.3)
196
+ rack-ssl (~> 1.3.2)
197
+ rake (>= 0.8.7)
198
+ rdoc (~> 3.4)
199
+ thor (~> 0.14.6)
200
+ rake (0.9.2.2)
201
+ rdoc (3.12)
202
+ json (~> 1.4)
203
+ rspec (2.8.0)
204
+ rspec-core (~> 2.8.0)
205
+ rspec-expectations (~> 2.8.0)
206
+ rspec-mocks (~> 2.8.0)
207
+ rspec-core (2.8.0)
208
+ rspec-expectations (2.8.0)
209
+ diff-lcs (~> 1.1.2)
210
+ rspec-mocks (2.8.0)
211
+ rspec-rails (2.8.1)
212
+ actionpack (>= 3.0)
213
+ activesupport (>= 3.0)
214
+ railties (>= 3.0)
215
+ rspec (~> 2.8.0)
216
+ ruby-debug-base19 (0.11.25)
217
+ columnize (>= 0.3.1)
218
+ linecache19 (>= 0.5.11)
219
+ ruby_core_source (>= 0.1.4)
220
+ ruby-debug19 (0.11.6)
221
+ columnize (>= 0.3.1)
222
+ linecache19 (>= 0.5.11)
223
+ ruby-debug-base19 (>= 0.11.19)
224
+ ruby_core_source (0.1.5)
225
+ archive-tar-minitar (>= 0.5.2)
226
+ rubyzip (0.9.6.1)
227
+ sax-machine (0.1.0)
228
+ nokogiri (> 0.0.0)
229
+ selenium-webdriver (2.20.0)
230
+ childprocess (>= 0.2.5)
231
+ ffi (~> 1.0)
232
+ multi_json (~> 1.0)
233
+ rubyzip
234
+ simplecov (0.6.1)
235
+ multi_json (~> 1.0)
236
+ simplecov-html (~> 0.5.3)
237
+ simplecov-html (0.5.3)
238
+ sprockets (2.0.3)
239
+ hike (~> 1.2)
240
+ rack (~> 1.0)
241
+ tilt (~> 1.1, != 1.3.0)
242
+ sqlite3 (1.3.5)
243
+ state_machine (1.1.1)
244
+ stringex (1.3.2)
245
+ thor (0.14.6)
246
+ tilt (1.3.3)
247
+ treetop (1.4.10)
248
+ polyglot
249
+ polyglot (>= 0.3.1)
250
+ tzinfo (0.3.31)
251
+ warden (1.0.6)
252
+ rack (>= 1.0)
253
+ xpath (0.1.4)
254
+ nokogiri (~> 1.3)
255
+
256
+ PLATFORMS
257
+ ruby
258
+
259
+ DEPENDENCIES
260
+ capybara (= 1.0.1)
261
+ factory_girl
262
+ ffaker
263
+ growl
264
+ guard-rspec
265
+ rspec
266
+ rspec-rails (~> 2.7)
267
+ ruby-debug19
268
+ simplecov
269
+ spree!
270
+ spree_correios!
271
+ sqlite3
data/Guardfile ADDED
@@ -0,0 +1,17 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb})
6
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch('spec/spec_helper.rb') { "spec" }
11
+ watch('config/routes.rb') { "spec/routing" }
12
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
13
+ watch(%r{^spec/.+_spec\.rb})
14
+ watch(%r{^app/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
15
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
16
+ watch(%r{^app/controllers/(.+)_(controller)\.rb}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
17
+ end
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2012 [name of plugin creator]
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification,
5
+ are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ * Neither the name Spree nor the names of its contributors may be used to
13
+ endorse or promote products derived from this software without specific
14
+ prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Spree Correios
2
+
3
+ Uma extensão do [Spree](http://spreecommerce.com) para permitir cálculos de frete (Sedex e Pac, com ou sem contrato) utilizando o webservice dos correios, através da ótima gem [correios-frete](https://github.com/prodis/correios-frete). Esta gem supõe o uso de caixas retangulares, e não faz cálculos de embalagens em rolo ou prisma.
4
+
5
+ ## Instalação
6
+
7
+ Adicione spree ao gemfile da sua aplicação, e também:
8
+
9
+ gem "spree_correios"
10
+
11
+ ## Configuração
12
+
13
+ Entre na seção administrativa do spree, vá em Configuração -> Métodos de Entrega e adicione o método de entrega desejado (SEDEX ou PAC). O atributo `CEP` é obrigatório e é utilizado como CEP de origem no cálculo do frete. Caso tenha um contrato com os correios, pode opcionalmente fornecer o `Código Administrativo` e a `Senha`.
14
+
15
+ A opção `Valor Declarado` utiliza o valor base do produto para declará-lo.
16
+
17
+ É necessário que os produtos que deseja disponibilizar para entrega através dos correios tenham os altura, largura, profundidade e peso corretamente preenchidos, pois estes atributos são utilizados para o cálculo do frete.
18
+
19
+ ## TODO
20
+
21
+ * Adicionar dimensões do pacote aos produtos para cálculo do frete
22
+
23
+ ## Contribuindo
24
+
25
+ Caso queira contribuir, faça um fork desta gem no [github](https://github.com/heavenstudio/spree_correios), corriga o bug/ adicione a feature desejada e faça um merge request.
26
+
27
+ ## Testando
28
+
29
+ $ bundle
30
+ $ bundle exec rake test app
31
+ $ bundle exec rspec spec
32
+
33
+ ## Licença
34
+
35
+ Copyright (c) 2012 [Stefano Diem Benatti](mailto:stefano@heavenstudio.com.br), released under the New BSD License
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/packagetask'
4
+ require 'rubygems/package_task'
5
+ require 'rspec/core/rake_task'
6
+ require 'spree/core/testing_support/common_rake'
7
+
8
+ RSpec::Core::RakeTask.new
9
+
10
+ task :default => [:spec]
11
+
12
+ spec = eval(File.read('spree_correios.gemspec'))
13
+
14
+ Gem::PackageTask.new(spec) do |p|
15
+ p.gem_spec = spec
16
+ end
17
+
18
+ desc "Release to gemcutter"
19
+ task :release => :package do
20
+ require 'rake/gemcutter'
21
+ Rake::Gemcutter::Tasks.new(spec).define
22
+ Rake::Task['gem:push'].invoke
23
+ end
24
+
25
+ desc "Generates a dummy app for testing"
26
+ task :test_app do
27
+ ENV['LIB_NAME'] = 'spree_correios'
28
+ Rake::Task['common:test_app'].invoke
29
+ end
data/Versionfile ADDED
@@ -0,0 +1,9 @@
1
+ # This file is used to designate compatibilty with different versions of Spree
2
+ # Please see http://spreecommerce.com/documentation/extensions.html#versionfile for details
3
+
4
+ # Examples
5
+ #
6
+ # "0.70.x" => { :branch => "master"}
7
+ # "0.60.x" => { :branch => "0-60-stable" }
8
+ # "0.40.x" => { :tag => "v1.0.0", :version => "1.0.0" }
9
+
@@ -0,0 +1,46 @@
1
+ module Spree
2
+ class Calculator::CorreiosBaseCalculator < Calculator
3
+ preference :zipcode, :string
4
+ preference :token, :string
5
+ preference :password, :password
6
+ preference :declared_value, :boolean, default: false
7
+ preference :receipt_notification, :boolean, default: false
8
+ preference :receive_in_hands, :boolean, default: false
9
+
10
+ attr_reader :delivery_time
11
+
12
+ def compute(object)
13
+ compute_delivery_price_and_time(object)
14
+ end
15
+
16
+ def compute(object)
17
+ return unless object.present? and object.line_items.present?
18
+
19
+ package = ::Correios::Frete::Pacote.new
20
+ object.line_items.map do |item|
21
+ package_item = ::Correios::Frete::PacoteItem.new(peso: item.weight, comprimento: item.length, largura: item.width, altura: item.height)
22
+ package.add_item(package_item)
23
+ end
24
+
25
+ calculator = ::Correios::Frete::Calculador.new do |c|
26
+ c.cep_origem = preferred_zipcode
27
+ c.cep_destino = object.ship_address.zipcode
28
+ c.encomenda = package
29
+ c.valor_declarado = object.amount.to_f if prefers?(:declared_value)
30
+ c.mao_propria = prefers?(:receive_in_hands)
31
+ c.aviso_recebimento = prefers?(:receipt_notification)
32
+ c.codigo_empresa = preferred_token if preferred_token.present?
33
+ c.senha = preferred_password if preferred_password.present?
34
+ end
35
+
36
+ webservice = calculator.calculate(shipping_method)
37
+ raise webservice.msg_erro if webservice.erro?
38
+ @delivery_time = webservice.prazo_entrega
39
+ webservice.valor
40
+ end
41
+
42
+ def has_contract?
43
+ preferred_token.present? && preferred_password.present?
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,23 @@
1
+ module Spree
2
+ class Calculator::PAC < Calculator::CorreiosBaseCalculator
3
+ def self.description
4
+ "PAC"
5
+ end
6
+
7
+ def shipping_method
8
+ if has_contract?
9
+ :pac_com_contrato
10
+ else
11
+ :pac
12
+ end
13
+ end
14
+
15
+ def shipping_code
16
+ if has_contract?
17
+ 41068
18
+ else
19
+ 41106
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module Spree
2
+ class Calculator::SEDEX < Calculator::CorreiosBaseCalculator
3
+ def self.description
4
+ "SEDEX"
5
+ end
6
+
7
+ def shipping_method
8
+ if preferred_token.present? && preferred_password.present?
9
+ :sedex_com_contrato_1
10
+ else
11
+ :sedex
12
+ end
13
+ end
14
+
15
+ def shipping_code
16
+ if has_contract?
17
+ 40096
18
+ else
19
+ 40010
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ pt-BR:
2
+ declared_value: Valor Declarado
3
+ receipt_notification: Confirmação de Recebimento
4
+ receive_in_hands: Mão Própria
5
+ token: Código Administrativo
6
+ zipcode: CEP
7
+
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Spree::Core::Engine.routes.draw do
2
+ # Add your extension routes here
3
+ end
@@ -0,0 +1,29 @@
1
+ module SpreeCorreios
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+
5
+ def add_javascripts
6
+ append_file "app/assets/javascripts/store/all.js", "//= require store/spree_correios\n"
7
+ append_file "app/assets/javascripts/admin/all.js", "//= require admin/spree_correios\n"
8
+ end
9
+
10
+ def add_stylesheets
11
+ inject_into_file "app/assets/stylesheets/store/all.css", " *= require store/spree_correios\n", :before => /\*\//, :verbose => true
12
+ inject_into_file "app/assets/stylesheets/admin/all.css", " *= require admin/spree_correios\n", :before => /\*\//, :verbose => true
13
+ end
14
+
15
+ def add_migrations
16
+ run 'bundle exec rake railties:install:migrations FROM=spree_correios'
17
+ end
18
+
19
+ def run_migrations
20
+ res = ask "Would you like to run the migrations now? [Y/n]"
21
+ if res == "" || res.downcase == "y"
22
+ run 'bundle exec rake db:migrate'
23
+ else
24
+ puts "Skiping rake db:migrate, don't forget to run it!"
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,24 @@
1
+ module SpreeCorreios
2
+ class Engine < Rails::Engine
3
+ engine_name 'spree_correios'
4
+
5
+ config.autoload_paths += %W(#{config.root}/lib)
6
+
7
+ initializer "spree.register.correios_calculator", after: "spree.register.calculators" do |app|
8
+ app.config.spree.calculators.shipping_methods += [Spree::Calculator::SEDEX, Spree::Calculator::PAC]
9
+ end
10
+
11
+ # use rspec for tests
12
+ config.generators do |g|
13
+ g.test_framework :rspec
14
+ end
15
+
16
+ def self.activate
17
+ Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c|
18
+ Rails.configuration.cache_classes ? require(c) : load(c)
19
+ end
20
+ end
21
+
22
+ config.to_prepare &method(:activate).to_proc
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ require 'spree_core'
2
+ require 'spree_correios/engine'
3
+ require 'correios-frete'
data/script/rails ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ ENGINE_PATH = File.expand_path('../..', __FILE__)
5
+ load File.expand_path('../../spec/dummy/script/rails', __FILE__)
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Calculator::CorreiosBaseCalculator do
4
+ it_behaves_like "correios calculator"
5
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Calculator::PAC do
4
+ before do
5
+ @pac = Spree::Calculator::PAC.new
6
+ end
7
+
8
+ it_behaves_like "correios calculator"
9
+
10
+ it "should have a description" do
11
+ Spree::Calculator::PAC.description.should == "PAC"
12
+ end
13
+
14
+ context "without a token and password" do
15
+ it "should have a shipping method of :pac" do
16
+ @pac.shipping_method.should == :pac
17
+ end
18
+
19
+ it "should have a shipping code of 41106" do
20
+ @pac.shipping_code.should == 41106
21
+ end
22
+ end
23
+
24
+ context "with a token and password" do
25
+ before do
26
+ @pac.preferred_token = "some token"
27
+ @pac.preferred_password = "some password"
28
+ end
29
+
30
+ it "should have a shipping method of :pac_com_contrato" do
31
+ @pac.shipping_method.should == :pac_com_contrato
32
+ end
33
+
34
+ it "should have a shipping code of 41068" do
35
+ @pac.shipping_code.should == 41068
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+ require 'open-uri'
3
+
4
+ describe Spree::Calculator::SEDEX do
5
+ before do
6
+ @sedex = Spree::Calculator::SEDEX.new
7
+ end
8
+
9
+ it_behaves_like "correios calculator"
10
+
11
+ it "should have a description" do
12
+ Spree::Calculator::SEDEX.description.should == "SEDEX"
13
+ end
14
+
15
+ context "without a token and password" do
16
+ it "should have a shipping method of :pac" do
17
+ @sedex.shipping_method.should == :sedex
18
+ end
19
+
20
+ it "should have a shipping code of 40010" do
21
+ @sedex.shipping_code.should == 40010
22
+ end
23
+ end
24
+
25
+ context "with a token and password" do
26
+ before do
27
+ @sedex.preferred_token = "some token"
28
+ @sedex.preferred_password = "some password"
29
+ end
30
+
31
+ it "should have a shipping method of :pac_com_contrato" do
32
+ @sedex.shipping_method.should == :sedex_com_contrato_1
33
+ end
34
+
35
+ it "should have a shipping code of 40096" do
36
+ @sedex.shipping_code.should == 40096
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,32 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+
6
+ require 'rspec/rails'
7
+
8
+ # Requires supporting ruby files with custom matchers and macros, etc,
9
+ # in spec/support/ and its subdirectories.
10
+ Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each {|f| require f }
11
+
12
+ # Requires factories defined in spree_core
13
+ require 'spree/core/testing_support/factories'
14
+
15
+ RSpec.configure do |config|
16
+ # == Mock Framework
17
+ #
18
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
19
+ #
20
+ # config.mock_with :mocha
21
+ # config.mock_with :flexmock
22
+ # config.mock_with :rr
23
+ config.mock_with :rspec
24
+
25
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
26
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
27
+
28
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
29
+ # examples within a transaction, remove the following line or assign false
30
+ # instead of true.
31
+ config.use_transactional_fixtures = true
32
+ end
@@ -0,0 +1,77 @@
1
+ shared_examples_for "correios calculator" do
2
+ before { @calculator = subject.class.new }
3
+
4
+ it "should have preferences" do
5
+ @calculator.preferences.keys.should include(:zipcode, :declared_value, :receipt_notification, :receive_in_hands, :token, :password)
6
+ end
7
+
8
+ it "delcared value should default to false" do
9
+ @calculator.preferred(:declared_value).should == false
10
+ end
11
+
12
+ it "receipt notification should default to false" do
13
+ @calculator.preferred(:receipt_notification).should == false
14
+ end
15
+
16
+ it "receive in hands should default to false" do
17
+ @calculator.preferred(:receive_in_hands).should == false
18
+ end
19
+
20
+ it "should have a contract if both token and password are given" do
21
+ @calculator.should_not have_contract
22
+ @calculator.preferred_token = "some token"
23
+ @calculator.preferred_password = "some password"
24
+ @calculator.should have_contract
25
+ end
26
+
27
+ context "compute" do
28
+ def get_correios_price_and_value_for(url)
29
+ doc = Nokogiri::XML(open(url))
30
+ price = doc.css("Valor").first.content.sub(/,(\d\d)$/, '.\1').to_f
31
+ prazo = doc.css("PrazoEntrega").first.content.to_i
32
+ return price, prazo
33
+ end
34
+
35
+ before do
36
+ items = [double(weight: 1, length: 20, width: 15, height: 5)]
37
+ @order = double("Order", line_items: items, amount: BigDecimal("2000,00"), ship_address: stub(zipcode: "72151613"))
38
+ @calculator.preferred_zipcode = "71939360"
39
+ end
40
+
41
+ it "should calculate shipping cost and delivery time" do
42
+ if @calculator.class.name != "Spree::Calculator::CorreiosBaseCalculator"
43
+ price, prazo = get_correios_price_and_value_for("http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?nCdEmpresa=&sDsSenha=&sCepOrigem=71939360&sCepDestino=72151613&nVlPeso=1&nCdFormato=1&nVlComprimento=20&nVlAltura=5&nVlLargura=15&sCdMaoPropria=n&nVlValorDeclarado=0&sCdAvisoRecebimento=n&nCdServico=#{@calculator.shipping_code}&nVlDiametro=0&StrRetorno=xml")
44
+
45
+ @calculator.compute(@order).should == price
46
+ @calculator.delivery_time.should == prazo
47
+ end
48
+ end
49
+
50
+ it "should change price according to declared value" do
51
+ if @calculator.class.name != "Spree::Calculator::CorreiosBaseCalculator"
52
+ price, prazo = get_correios_price_and_value_for("http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?nCdEmpresa=&sDsSenha=&sCepOrigem=71939360&sCepDestino=72151613&nVlPeso=1&nCdFormato=1&nVlComprimento=20&nVlAltura=5&nVlLargura=15&sCdMaoPropria=n&nVlValorDeclarado=2000,00&sCdAvisoRecebimento=n&nCdServico=#{@calculator.shipping_code}&nVlDiametro=0&StrRetorno=xml")
53
+
54
+ @calculator.preferred_declared_value = true
55
+ @calculator.compute(@order).should == price
56
+ end
57
+ end
58
+
59
+ it "should change price according to in hands" do
60
+ if @calculator.class.name != "Spree::Calculator::CorreiosBaseCalculator"
61
+ price, prazo = get_correios_price_and_value_for("http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?nCdEmpresa=&sDsSenha=&sCepOrigem=71939360&sCepDestino=72151613&nVlPeso=1&nCdFormato=1&nVlComprimento=20&nVlAltura=5&nVlLargura=15&sCdMaoPropria=s&nVlValorDeclarado=0&sCdAvisoRecebimento=n&nCdServico=#{@calculator.shipping_code}&nVlDiametro=0&StrRetorno=xml")
62
+
63
+ @calculator.preferred_receive_in_hands = true
64
+ @calculator.compute(@order).should == price
65
+ end
66
+ end
67
+
68
+ it "should change price according to receipt notification" do
69
+ if @calculator.class.name != "Spree::Calculator::CorreiosBaseCalculator"
70
+ price, prazo = get_correios_price_and_value_for("http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?nCdEmpresa=&sDsSenha=&sCepOrigem=71939360&sCepDestino=72151613&nVlPeso=1&nCdFormato=1&nVlComprimento=20&nVlAltura=5&nVlLargura=15&sCdMaoPropria=n&nVlValorDeclarado=0&sCdAvisoRecebimento=s&nCdServico=#{@calculator.shipping_code}&nVlDiametro=0&StrRetorno=xml")
71
+
72
+ @calculator.preferred_receipt_notification = true
73
+ @calculator.compute(@order).should == price
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+ Gem::Specification.new do |s|
3
+ s.platform = Gem::Platform::RUBY
4
+ s.name = 'spree_correios'
5
+ s.version = '1.0.0'
6
+ s.summary = %q{A spree extensions to add Brazil's Correio calculators}
7
+ s.required_ruby_version = '>= 1.9.2'
8
+
9
+ s.authors = ["Stefano Diem Benatti"]
10
+ s.email = ["stefano.diem@gmail.com"]
11
+ s.homepage = "http://github.com/heavenstudio/spree_correios"
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.require_path = 'lib'
16
+ s.requirements << 'none'
17
+
18
+ s.add_dependency 'spree_core', '~> 1.0.0'
19
+ s.add_dependency 'correios-frete', '~> 1.5.2'
20
+
21
+ s.add_development_dependency 'capybara', '1.0.1'
22
+ s.add_development_dependency 'factory_girl'
23
+ s.add_development_dependency 'ffaker'
24
+ s.add_development_dependency 'rspec-rails', '~> 2.7'
25
+ s.add_development_dependency 'sqlite3'
26
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spree_correios
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stefano Diem Benatti
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: spree_core
16
+ requirement: &2164530300 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2164530300
25
+ - !ruby/object:Gem::Dependency
26
+ name: correios-frete
27
+ requirement: &2164537720 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.5.2
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2164537720
36
+ - !ruby/object:Gem::Dependency
37
+ name: capybara
38
+ requirement: &2164543100 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - =
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.1
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2164543100
47
+ - !ruby/object:Gem::Dependency
48
+ name: factory_girl
49
+ requirement: &2164552160 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2164552160
58
+ - !ruby/object:Gem::Dependency
59
+ name: ffaker
60
+ requirement: &2164562500 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *2164562500
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: &2164570920 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: '2.7'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *2164570920
80
+ - !ruby/object:Gem::Dependency
81
+ name: sqlite3
82
+ requirement: &2164575500 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *2164575500
91
+ description:
92
+ email:
93
+ - stefano.diem@gmail.com
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - .gitignore
99
+ - .rspec
100
+ - Gemfile
101
+ - Gemfile.lock
102
+ - Guardfile
103
+ - LICENSE
104
+ - README.md
105
+ - Rakefile
106
+ - Versionfile
107
+ - app/models/spree/calculator/correios_base_calculator.rb
108
+ - app/models/spree/calculator/pac.rb
109
+ - app/models/spree/calculator/sedex.rb
110
+ - config/locales/pt-br.yml
111
+ - config/routes.rb
112
+ - lib/generators/spree_correios/install/install_generator.rb
113
+ - lib/spree_correios.rb
114
+ - lib/spree_correios/engine.rb
115
+ - script/rails
116
+ - spec/models/spree/calculator/correios_base_calculator_spec.rb
117
+ - spec/models/spree/calculator/pac_spec.rb
118
+ - spec/models/spree/calculator/sedex_spec.rb
119
+ - spec/spec_helper.rb
120
+ - spec/support/correios_shared_examples.rb
121
+ - spree_correios.gemspec
122
+ homepage: http://github.com/heavenstudio/spree_correios
123
+ licenses: []
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: 1.9.2
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements:
141
+ - none
142
+ rubyforge_project:
143
+ rubygems_version: 1.8.10
144
+ signing_key:
145
+ specification_version: 3
146
+ summary: A spree extensions to add Brazil's Correio calculators
147
+ test_files:
148
+ - spec/models/spree/calculator/correios_base_calculator_spec.rb
149
+ - spec/models/spree/calculator/pac_spec.rb
150
+ - spec/models/spree/calculator/sedex_spec.rb
151
+ - spec/spec_helper.rb
152
+ - spec/support/correios_shared_examples.rb