Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 99ccf5aa867bd519c8e2f2cb124f133a1df16848 @brewski committed Apr 18, 2012
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in fedex-web-services.gemspec
+gemspec
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "fedex/version"
+require "rake"
+
+Gem::Specification.new do |s|
+ s.name = "fedex-web-services"
+ s.version = Fedex::VERSION
+ s.authors = ["Brian Abreu"]
+ s.email = ["brian@nut.com"]
+ s.homepage = "https://github.com/brewski/fedex-web-services"
+ s.summary = %q{Provies an interface to the FedEx web services API (version 10)}
+ s.description = %q{Interfaces with the FedEx web services API to look up shipping rates, generate labels, and cancel shipments}
+
+ s.rubyforge_project = "fedex-web-services"
+
+ s.files = FileList[ "lib/**/*" ]
+ s.test_files = [ ]
+ s.executables = [ ]
+ s.require_paths = ["lib"]
+
+ # specify any dependencies here; for example:
+ # s.add_development_dependency "rspec"
+ # s.add_runtime_dependency "rest-client"
+end
@@ -0,0 +1,6 @@
+require "fedex/version"
+require "fedex/web_services"
+
+module Fedex
+ require 'fedex/railtie' if defined?(Rails)
+end
@@ -0,0 +1,9 @@
+module Fedex
+ class Railtie < Rails::Railtie
+ railtie_name :fedex
+
+ rake_tasks do
+ load "fedex/tasks/fedex.rake"
+ end
+ end
+end
@@ -0,0 +1,21 @@
+namespace :fedex do
+ desc "Generate the ruby definition files from the FedEx wsdl files"
+ task :generate_definitions do
+ wsdl_dir = ENV['WSDL_DIR']
+ if (!Dir.exist?(wsdl_dir || ""))
+ puts "Set the WSDL_DIR environment variable to location of the Fedex wsdl files"
+ else
+ Fedex::WebServices::Definitions.generate_definitions(
+ File.join(Rails.root, 'lib'),
+ *Dir.glob(File.join(ENV['WSDL_DIR'], '*.wsdl'))
+ )
+
+ File.open(File.join(Rails.root, 'config', 'initializers', 'fedex.rb'), "w") do |file|
+ file.puts "Fedex::WebServices::Definitions.load_definitions('lib')"
+ end
+
+ puts "Added lib/fedex/web_services/definitions/"
+ puts "Added config/initializers/fedex.rb"
+ end
+ end
+end
@@ -0,0 +1,3 @@
+module Fedex
+ VERSION = "1.0.42"
+end
@@ -0,0 +1,10 @@
+require 'fedex/web_services/definitions'
+
+require 'fedex/web_services/request/base'
+require 'fedex/web_services/request/process_shipment'
+require 'fedex/web_services/request/delete_shipment'
+require 'fedex/web_services/request/get_rates'
+
+require 'fedex/web_services/service/base'
+require 'fedex/web_services/service/ship'
+require 'fedex/web_services/service/rate'
@@ -0,0 +1,46 @@
+module Fedex
+ module WebServices
+ module Definitions
+ extend(self)
+
+ MODULE_PATH = File.join(%w(fedex web_services definitions))
+
+ def load_definitions(lib_dir)
+ $VERBOSE = $VERBOSE.tap do
+ $VERBOSE = nil
+
+ $:.unshift(lib_dir)
+ $:.unshift(File.join(lib_dir, MODULE_PATH))
+ Dir.glob(File.join(lib_dir, MODULE_PATH, "*DefinitionsDriver.rb")).each do |definition_file|
+ begin
+ require File.join(MODULE_PATH, File.basename(definition_file))
+ rescue Exception
+ raise "Error loading Fedex definition file #{definition_file}: #{$!.message}"
+ end
+ end
+ $:.shift
+ $:.shift
+ end
+ end
+
+ def generate_definitions(lib_dir, *wsdl_files)
+ require 'wsdl/soap/wsdl2ruby'
+ require 'fileutils'
+
+ wsdl_files.each do |wsdl_file|
+ worker = WSDL::SOAP::WSDL2Ruby.new
+ worker.basedir = FileUtils.mkdir_p(File.join(Dir.new(lib_dir), MODULE_PATH)).first
+ worker.location = File.new(wsdl_file).path
+ worker.logger.level = Logger::WARN
+ worker.opt.update(
+ "module_path" => "Fedex::WebServices::Definitions",
+ "mapping_registry" => nil,
+ "driver" => nil,
+ "classdef" => nil
+ )
+ worker.run
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,69 @@
+require 'time'
+
+module Fedex::WebServices
+ module Request
+ class Base
+ include Fedex::WebServices::Definitions
+
+ def initialize(service)
+ @service = service
+ end
+
+ def remote_method
+ raise "remote_method must be implemented by subclasses of Service"
+ end
+
+ private
+ def web_authentication_detail
+ WebAuthenticationDetail.new.tap do |o|
+ o.userCredential = WebAuthenticationCredential.new.tap do |o|
+ o.key = @service.credentials.key
+ o.password = @service.credentials.password
+ end
+ end
+ end
+
+ def client_detail
+ ClientDetail.new.tap do |o|
+ o.accountNumber = @service.credentials.account_number
+ o.meterNumber = @service.credentials.meter_number
+ end
+ end
+
+ def version
+ VersionId.new.tap do |o|
+ o.serviceId = @service.service_id
+ o.major = 10
+ o.intermediate = 0
+ o.minor = 0
+ end
+ end
+ end
+ end
+end
+# return
+#
+#
+# ship_service = Fedex::WebServices::Service::Ship.new(creds)
+# a = nil
+# ship_service.delete_shipment('1234', 'GROUND') { |contents| a = contents }
+#
+# # req = FedExRequest.new(credentials).delete_shipment_request('794797411470', TrackingIdType::GROUND)
+# req = FedExRequest.new(credentials).process_shipment
+# ship_port = ShipPortType.new('https://wsbeta.fedex.com:443/web-services')
+# ship_port.wiredump_dev = STDOUT
+#
+# begin
+# # response = ship_port.deleteShipment(req)
+# response = ship_port.processShipment(req)
+# check_response(response)
+# rescue Exception => root_exception
+# begin
+# err = RuntimeError.new(root_exception.detail.fault.details.validationFailureDetail.message * ",")
+# err.set_backtrace([ "#{__FILE__}:#{__LINE__}", *root_exception.backtrace ])
+# rescue
+# raise root_exception.message
+# end
+#
+# raise err
+# end
@@ -0,0 +1,27 @@
+module Fedex::WebServices
+ module Request
+ class DeleteShipment < Base
+ attr_accessor :tracking_id
+
+ def initialize(service, tracking_id)
+ super(service)
+ @tracking_id = tracking_id
+ end
+
+ def remote_method
+ :deleteShipment
+ end
+
+ def contents
+ DeleteShipmentRequest.new.tap do |o|
+ o.webAuthenticationDetail = web_authentication_detail
+ o.version = version
+ o.clientDetail = client_detail
+ o.deletionControl = DeletionControlType::DELETE_ALL_PACKAGES
+
+ o.trackingId = @tracking_id
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,56 @@
+module Fedex::WebServices
+ module Request
+ class GetRates < Base
+
+ def initialize(service, service_type, rate_request_type, shipper, recipient, weight)
+
+ super(service)
+
+ @service_type = service_type
+ @shipper = shipper
+ @recipient = recipient
+ @rate_request_type = rate_request_type
+ @requested_package_line_items = [
+ RequestedPackageLineItem.new.tap do |o|
+ o.groupPackageCount = 1
+ o.sequenceNumber = 1
+ o.weight = weight
+ end
+ ]
+ end
+
+ def remote_method
+ :getRates
+ end
+
+ def contents
+ RateRequest.new.tap do |o|
+ o.webAuthenticationDetail = web_authentication_detail
+ o.version = version
+ o.clientDetail = client_detail
+
+ o.requestedShipment = RequestedShipment.new.tap do |o|
+ o.shipTimestamp = Time.now.iso8601
+ o.serviceType = @service_type
+ o.packagingType = PackagingType::YOUR_PACKAGING
+
+ o.shipper = @shipper
+ o.recipient = @recipient
+
+ o.shippingChargesPayment = Payment.new.tap do |o|
+ o.paymentType = PaymentType::SENDER
+ o.payor = Payor.new.tap do |o|
+ o.accountNumber = @service.credentials.account_number
+ end
+ end
+
+ o.rateRequestTypes = [ @rate_request_type ]
+
+ o.packageCount = @requested_package_line_items.size
+ o.requestedPackageLineItems = @requested_package_line_items
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,76 @@
+module Fedex::WebServices
+ module Request
+ class ProcessShipment < Base
+
+ def self.mps_requests(service, service_type, shipper, recipient,
+ label_specification, package_weights)
+
+ package_weights.map.with_index do |weight, index|
+ requested_package_line_item = RequestedPackageLineItem.new.tap do |o|
+ o.sequenceNumber = index + 1
+ o.weight = weight
+ end
+
+ ProcessShipment.new(
+ service,
+ service_type,
+ shipper,
+ recipient,
+ label_specification,
+ package_weights.size,
+ [ requested_package_line_item ]
+ )
+ end
+ end
+
+ def initialize(service, service_type, shipper, recipient,
+ label_specification, package_count, requested_package_line_items)
+
+ super(service)
+
+ @service_type = service_type
+ @shipper = shipper
+ @recipient = recipient
+ @label_specification = label_specification
+ @package_count = package_count
+ @requested_package_line_items = requested_package_line_items
+ end
+
+ def remote_method
+ :processShipment
+ end
+
+ def contents
+ ProcessShipmentRequest.new.tap do |o|
+ o.webAuthenticationDetail = web_authentication_detail
+ o.version = version
+ o.clientDetail = client_detail
+
+ o.requestedShipment = RequestedShipment.new.tap do |o|
+ o.shipTimestamp = Time.now.iso8601
+ o.dropoffType = DropoffType::REGULAR_PICKUP
+ o.serviceType = @service_type
+ o.packagingType = PackagingType::YOUR_PACKAGING
+
+ o.shipper = @shipper
+ o.recipient = @recipient
+
+ o.shippingChargesPayment = Payment.new.tap do |o|
+ o.paymentType = PaymentType::SENDER
+ o.payor = Payor.new.tap do |o|
+ o.accountNumber = @service.credentials.account_number
+ end
+ end
+
+ o.labelSpecification = @label_specification
+
+ o.rateRequestTypes = [ RateRequestType::LIST ]
+
+ o.requestedPackageLineItems = @requested_package_line_items
+ o.packageCount = @package_count
+ end
+ end
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 99ccf5a

Please sign in to comment.