Skip to content

Commit

Permalink
Fixed small mistakes in Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
FotoVerite committed Aug 21, 2008
1 parent 3e81410 commit 87f6b13
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 55 deletions.
8 changes: 3 additions & 5 deletions README.markdown
Expand Up @@ -90,9 +90,7 @@ Object Methods are as follow

# Sample Usage
#In your environment.rb or production.rb

require 'active_shipping'
include FotoVerite::AwesomeUsps
include FotoVerite

# Then to access the plugin you much create an instance of USPS class
usps = USPS.new('Your user name')
Expand Down Expand Up @@ -376,8 +374,8 @@ The nicest way to submit changes would be to set up a GitHub account and fork th

## Legal Mumbo Jumbo

Unless otherwise noted in specific files, all code in the Active Shipping project is under the copyright and license described in the included MIT-LICENSE file.
Unless otherwise noted in specific files, all code in the Awesome USPS is under the copyright and license described in the included MIT-LICENSE file.

Packages Module created by James MacAulay

Copyright (c) 2008 FotoVerite, released under the MIT license
Copyright (c) 2008 Matthew Bergman, released under the MIT license
2 changes: 1 addition & 1 deletion lib/foto_verite/express_mail.rb
Expand Up @@ -20,7 +20,7 @@ def express_mail_label(orgin, destination, ounces, image_type, options={})
commit_express_mail_xml(:express_mail_label, request, image_type, false)
end

def express_mail_label_canned_test
def canned_express_mail_label_test
@orgin = Location.new( :first_name=> "Craig", :last_name=>"Engle", :address2 => "6406 Ivy Lane", :state => 'MD', :city => 'Greenbelt', :zip5 => '20770', :phone => "2127658576")
@destination =Location.new( :firm_name=> "XYZ Corp.", :address2 =>"1100 West Avenue", :address2 => "6406 Ivy Lane", :state => 'MD', :city => 'Greenbelt', :zip5 => '20770')
@ounces = "50"
Expand Down
125 changes: 76 additions & 49 deletions lib/foto_verite/shipping.rb
Expand Up @@ -3,28 +3,14 @@ module Shipping

MAX_RETRIES = 3

ORIGIN_ZIP = "07024" #User should change this

LIVE_DOMAIN = 'production.shippingapis.com'
LIVE_RESOURCE = '/ShippingAPI.dll'

TEST_DOMAINS = { #indexed by security; e.g. TEST_DOMAINS[USE_SSL[:rates]]
true => 'secure.shippingapis.com',
false => 'testing.shippingapis.com'
}

TEST_RESOURCE = '/ShippingAPITest.dll'

API_CODES = {
:us_rates => 'RateV3',
:world_rates => 'IntlRate',
:test => 'CarrierPickupAvailability'
}

USE_SSL = {
:tracking => false,
:test => true
}
CONTAINERS = {
:envelope => 'Flat Rate Envelope',
:box => 'Flat Rate Box'
Expand Down Expand Up @@ -62,21 +48,12 @@ module Shipping
:all => 'ALL'
}

#Determins size of package automatically. Taken from Active_Shipping
def size_code_for(package)
total = package.inches(:length) + package.inches(:girth)
if total <= 84
return 'REGULAR'
elsif total <= 108
return 'LARGE'
else # <= 130
return 'OVERSIZE'
end
end

# options Are?
def domestic_rates(destination_zip, packages, options={})
@packages = Array(packages)
def domestic_rates(origin_zip, destination_zip, packages, options={})
@origin_zip = origin_zip
@packages = packages
Array(@packages) if not @packages.is_a? Array
@destination_zip = destination_zip
@options = options
request = xml_for_us
Expand All @@ -85,14 +62,62 @@ def domestic_rates(destination_zip, packages, options={})

# options Are?
def world_rates(country, packages, options={})
@packages = Array(packages)
@packages = packages
Array(@packages) if not @packages.is_a? Array
@country = country
@options = options
request = xml_for_world
tracking_commit(:world_rates, request ,false)
end


def canned_domestic_rates_test
@origin_zip = "07024"
@packages =[
Package.new( 100,
[93,10],
:cylinder => true),

Package.new( (7.5 * 16),
[15, 10, 4.5],
:units => :imperial)
]
@destination_zip = "10010"
@options = {}
request = xml_for_us
tracking_commit(:us_rates, request ,false)
end


def canned_world_rates_test
@packages = [
Package.new( 100,
[93,10],
:cylinder => true),

Package.new( (7.5 * 16),
[15, 10, 4.5],
:units => :imperial)
]
@country = "Japan"
@options ={}
request = xml_for_world
tracking_commit(:world_rates, request ,false)
end

private

#Determins size of package automatically. Taken from Active_Shipping
def size_code_for(package)
total = package.inches(:length) + package.inches(:girth)
if total <= 84
return 'REGULAR'
elsif total <= 108
return 'LARGE'
else # <= 130
return 'OVERSIZE'
end
end

# XML built with Build:XmlMarkup
def xml_for_us
xm = Builder::XmlMarkup.new
Expand All @@ -101,7 +126,7 @@ def xml_for_us
p = @packages[id]
xm.Package("ID" => "#{id}") {
xm.Service("#{US_SERVICES[@options[:service]] || :all}")
xm.ZipOrigination(ORIGIN_ZIP)
xm.ZipOrigination(@origin_zip)
xm.ZipDestination(@destination_zip)
xm.Pounds("0")
xm.Ounces("#{'%0.1f' % [p.ounces,1].max}")
Expand Down Expand Up @@ -142,9 +167,10 @@ def xml_for_world
# {Package1 =>{'First Class' => "1.90"}Package2 => {'First Class' => "26.90" }

def parse_us(xml)
domestic_rate_hash = Hash.new
i= 0
i=0
domestic_rate_array = []
Hpricot.parse(xml).search('package').each do |package|
h = {}
i+=1
#This will return the first error description found in response xml.
#TODO find way to return all errors.
Expand All @@ -153,46 +179,48 @@ def parse_us(xml)

return "package number #{i} has the error #{package.search("description").inner_html} please fix before continuing"
end
#Initializing hash for each package. Is there a better way I wonder.
domestic_rate_hash["Package#{i}"] = {}
#Going through each package and finding the rate.
package.search("postage").each do |services|

mailservice=services.search("mailservice")
rate = services.search("rate")
domestic_rate_hash["Package#{i}"][mailservice.inner_html] = rate.inner_html
h[mailservice.inner_html] = rate.inner_html
end
domestic_rate_array << h
end
if domestic_rate_hash == {}
domestic_rate_hash = Hpricot.parse(xml).search('description').inner_html
if domestic_rate_array == []
return Hpricot.parse(xml).search('description').inner_html
end
return domestic_rate_hash
return domestic_rate_array
end

def parse_world(xml)
international_rate_hash = Hash.new
international_rate_array = []
i= 0
Hpricot.parse(xml).search('package').each do |package|
i+=1
h = {}
#This will return the first error description found in response xml.
#TODO find way to return all errors.
if package.search("error") != []
RAILS_DEFAULT_LOGGER.info("package number #{i} has the error #{package.search("description").inner_html} please fix before continuing")

return "package number #{i} has the error #{package.search("description").inner_html} please fix before continuing"
end
#Initializing hash for each package. Is there a better way I wonder.
international_rate_hash["Package#{i}"] = {}
#Going through each package and finding the rate.
package.search("service").each do |services|

svcdescription=services.search("svcdescription")
rate = services.search("postage")
international_rate_hash["Package#{i}"][svcdescription.inner_html] = rate.inner_html
h[svcdescription.inner_html] = rate.inner_html

end
international_rate_array << h
end
if international_rate_hash == {}
international_rate_hash = Hpricot.parse(xml).search('description').inner_html
if international_rate_array == []
return Hpricot.parse(xml).search('description').inner_html
end
return international_rate_hash
return international_rate_array
end

def tracking_commit(action, request, test = false)
Expand All @@ -202,8 +230,8 @@ def tracking_commit(action, request, test = false)
req = Net::HTTP::Post.new(url.path)
req.set_form_data({'API' => API_CODES[action], 'XML' => request})
response = Net::HTTP.new(url.host, url.port)
response.open_timeout = 5
response.read_timeout = 5
response.open_timeout = 2
response.read_timeout = 2
response.start
rescue Timeout::Error
if retries > 0
Expand Down Expand Up @@ -233,4 +261,3 @@ def tracking_commit(action, request, test = false)
end
end
end

0 comments on commit 87f6b13

Please sign in to comment.