Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ To use brEshop plugin you have to have HAML installed:

$ gem install --no-ri haml

And to install brEshop as plugin type:
And to install brEshop as plugin type:

ruby script/plugin install git://github.com/mangar/breshop.git

Expand All @@ -39,18 +39,18 @@ And to install brEshop as plugin type:
:quantity=>1,
:price=>11.50,
:weight=>0.100})

@item_2 = Item.new({:code=>"2",
:description=>"producto02",
:quantity=>2,
:price=>22.30,
:weight=>0.200})
:weight=>0.200})

#cria o carrinho de compras e insere os itens..
sale = Sale.new
sale << @item_1
sale << @item_2

#registra os dados do comprador.... e as ultimas opos de entrega...
sale.buyer = Buyer.new
sale.buyer.name = request.parameters['name'] + " " + request.parameters['last_name']
Expand All @@ -67,7 +67,7 @@ And to install brEshop as plugin type:
sale.buyer.phone = request.parameters['phone1'][4..12]
sale.buyer.phone = sale.buyer.phone.gsub('-', '')
sale.shipment_type = sale.shipment_type

pagseguro = Integration.new
@content = pagseguro.checkout(sale)

Expand Down Expand Up @@ -98,7 +98,7 @@ More info about Automatic Return service can be found in PagSeguro website, in f

## Help

Do you wanna to help? Do you have any new idea? Contact us! Make a fork!
Do you wanna to help? Do you have any new idea? Contact us! Make a fork!

Enjoy!

Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
task :test do
task :test do
require 'rake/runtest'
Rake.run_tests '**/*test.rb'
end
Expand Down
6 changes: 3 additions & 3 deletions correios/lib/config/correios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ aviso_recebimento: N
mao_propria: N

#
# Default service (servico_padrao). Correios has two types of delivery service:
# Default service (servico_padrao). Correios has two types of delivery service:
# 1 - SEDEX : faster but not so sheap
# 2 - PAC : not so fast but sheap
#
#
# The codes are:
#
#
# 41106 = PAC
# ????? = SEDEX
#
Expand Down
34 changes: 17 additions & 17 deletions correios/lib/correios/correios.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Correios
# if no one is specified, will be used the default: /../config/correios.yml
#
def initialize(pars = {})

@config = (pars[:config].nil? ? YAML.load_file(File.dirname(__FILE__) + "/../config/correios.yml") : pars[:config])

@params_config = {:resposta => "xml"}
Expand All @@ -18,15 +18,15 @@ def initialize(pars = {})
@params_config.merge!({:servico => "#{@config['servico_padrao']}"})
@params_config.merge!({:cepOrigem => "#{@config['cep_origem']}"})

@uri = URI.parse(@config['url'])
@uri = URI.parse(@config['url'])
end




#
# Calculates the freight based on brazilian delivery company called: Correios (www.correios.com.br)
#
#
# Ex.:
# correios = Correios.new
# freight = correios.freight "14055-490", "0.300", "1500.00" ==> freight price
Expand All @@ -37,39 +37,39 @@ def initialize(pars = {})
# freight = correios.freight "14055-490", "0.300", "1500.00", {:servico => "Correios.SEDEX", :cepOrigem => "04515-030"} ==> freight price
#
def freight (zip_destination, weight, price, pars = {})

raise "Invalid Destination Zip Code (#{zip_destination})" if (zip_destination.nil? || zip_destination.empty?)
raise "Invalid Weight (#{weight})" if (weight.nil? || weight.empty?)
raise "Invalid Price (#{price})" if (price.nil? || price.empty?)

#replace some parameters.....
@params_config.merge!({:servico => pars[:servico]}) if pars.has_key?(:servico)
@params_config.merge!({:cepOrigem => pars[:cep_origem]}) if pars.has_key?(:cep_origem)
@params_config.merge!({:servico => pars[:servico]}) if pars.has_key?(:servico)
@params_config.merge!({:cepOrigem => pars[:cep_origem]}) if pars.has_key?(:cep_origem)

#insert some new ones.....
@params_config.merge!({:cepDestino => zip_destination})
@params_config.merge!({:peso => weight})
@params_config.merge!({:valorDeclarado => price})
@params_config.merge!({:valorDeclarado => price})

#sending requesto to Correios....
url_pars = ""
@params_config.each do |k,v| url_pars += "&#{k}=#{v}" end

response = Net::HTTP.get_response(URI.parse("#{@config['url']}#{url_pars}"))
raise "Problem to get response. (#{response.code} - #{response.message})" unless response.kind_of?(Net::HTTPSuccess)

doc = REXML::Document.new(response.body)

# puts "#{doc}"

freight = ""
%w(descricao preco_postal).each do |elm|
element = REXML::XPath.match(doc, "//#{elm}").first
raise "Problems with parameters (#{element.text})" if ((elm.eql? 'descricao') && (!element.text.nil? && !element.text.empty?))
freight = element.text if (elm.eql? 'preco_postal')
end

return freight
return freight
end

end
36 changes: 18 additions & 18 deletions correios/test/correios_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,41 @@
require File.dirname(__FILE__) + '/../lib/correios'

class CorreiosTest < Test::Unit::TestCase



def setup

@config = YAML.load_file(File.dirname(__FILE__) + '/correios_test.yml')
end



def test_shipment_calculate

correios = Correios.new({:config => @config})

#zip_destination, weight and total proce can not be nil...
assert_raise RuntimeError do correios.freight nil, nil, nil; end

#...neighter blank!
assert_raise RuntimeError do correios.freight "", "", ""; end

assert_equal('22.3', correios.freight("14055-490", "0.300", "1560.00"), "Check the freight price, may be it can be changed.")

# Invalid service code!
assert_raise RuntimeError do correios.freight("14055-490", "0.300", "1560.00", {:servico => "aaaa"}); end

# Invalid Origin ZipCode
assert_raise RuntimeError do correios.freight("14055-490", "0.300", "1560.00", {:cep_origem => ""}); end

correios = Correios.new({:config => @config})
assert_equal('22.3', correios.freight("14055-490", "0.300", "1560.00", {:servico => "41106", :cep_origem => "04515-030"}), "Check the freight price, may be it can be changed.")


end




end
2 changes: 1 addition & 1 deletion correios/test/correios_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ url: http://www.correios.com.br/encomendas/precos/calculo.cfm?

aviso_recebimento: N
mao_propria: N
#41106 = PAC /
#41106 = PAC /
servico_padrao: 41106
cep_origem: 04515-030
8 changes: 4 additions & 4 deletions general/lib/general/item.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
class Item
attr_accessor :code, :description, :quantity, :price, :weight

def initialize(item = {})
@code = item[:code]
@description = item[:description]
@quantity = item[:quantity]
@price = item[:price]
@weight = item[:weight]
end

def price_total
@quantity * @price
end

def weight_total
@quantity * @weight
end

end
8 changes: 4 additions & 4 deletions general/lib/general/sale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ def initialize(params = {})
#
# Ex.:
# sale << item
def <<(item)
def <<(item)
return if (item.nil?)
raise InvalidItem unless item.class.eql? Item

#item is in sale....
if (@itens.has_key?(item.code))

#quantity eq zero, remove...
@itens.delete item.code if (item.quantity == 0)

#change the quantity
@itens[item.code].quantity = item.quantity if (item.quantity > 0)

else
@itens[item.code] = item
end
Expand All @@ -47,7 +47,7 @@ def price
price += (@shipment.nil? ? 0 : @shipment)
price
end

# Returns the total weight of the all product in the sale
def weight
weight = 0
Expand Down
20 changes: 10 additions & 10 deletions general/test/item_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
require File.dirname(__FILE__) + '/../lib/general'

class ItemTest < Test::Unit::TestCase

def setup
@item_1 = Item.new({:code=>1,
:description=>"Primeiro Item",
:price=>100.0,
:quantity=>2,
@item_1 = Item.new({:code=>1,
:description=>"Primeiro Item",
:price=>100.0,
:quantity=>2,
:weight=>10})
end
def test_obtendo_peso_total

def test_obtendo_peso_total
assert_equal(10, @item_1.weight)
end

def test_obtendo_preco_total
assert_equal(100, @item_1.price)
end


end
Loading