Skip to content

Commit

Permalink
adding support for the claim status api
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Matheson committed Dec 7, 2009
1 parent 66276d6 commit cb2dd8d
Show file tree
Hide file tree
Showing 15 changed files with 193 additions and 11 deletions.
9 changes: 8 additions & 1 deletion lib/shipsurance.rb
Expand Up @@ -4,11 +4,18 @@
require 'extensions/hash'
require 'shipsurance/shipsurance'
require 'shipsurance/base'
require 'shipsurance/response'
require 'shipsurance/claim_response'
require 'shipsurance/claim_status_response'
require 'shipsurance/record_shipment_response'
require 'shipsurance/void_record_shipment_response'
require 'shipsurance/validation_response'
require 'shipsurance/claim'
require 'shipsurance/claim_status'
require 'shipsurance/record_shipment'
require 'shipsurance/void_record_shipment'
require 'shipsurance/validation'
require 'shipsurance/person'
require 'shipsurance/address'
require 'shipsurance/response'


13 changes: 10 additions & 3 deletions lib/shipsurance/base.rb
Expand Up @@ -48,7 +48,7 @@ def commit(post)
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
case res
when Net::HTTPSuccess#, Net::HTTPRedirection
Shipsurance::Response.new(res)
response(res)
else
raise Shipsurance::RequestError, res.error!
end
Expand Down Expand Up @@ -77,6 +77,10 @@ def resource_class
@resource_class ||= self.class.to_s.split(":").last
end

def response(response)
"#{self.class}Response".constantize.new(response)
end

def base_url
return RAILS_ENV =~ /production/ ? @@live_base_url : @@test_base_url
end
Expand Down Expand Up @@ -202,9 +206,12 @@ def format_date(date, format = "%m/%d/%Y")
def validate_request(post)
@@errors.clear
required.each do |key|
@@errors << "Missing required parameter #{key.to_s}" unless post.has_key?(key)
@@errors << "#{key.to_s}" unless post.has_key?(key)
end
unless @@errors.empty?
@@errors.unshift("Missing required #{@@errors.size > 1 ? "parameters" : "parameter"}")
raise Shipsurance::RequestError, @@errors.join(", ")
end
raise Shipsurance::RequestError, @@errors.join(", ") unless @@errors.empty?
end

def credentials
Expand Down
15 changes: 15 additions & 0 deletions lib/shipsurance/claim_response.rb
@@ -0,0 +1,15 @@
module Shipsurance
class ClaimResponse < Response
attr_reader :claim_id, :claim_code

def initialize(response)
@response = parse(response)
@code = @response[0]
@reason_code = @response[1]
@claim_id = @response[2]
@claim_code = @response[3]
self
end

end
end
8 changes: 8 additions & 0 deletions lib/shipsurance/claim_status.rb
@@ -0,0 +1,8 @@
module Shipsurance
class ClaimStatus < Base

def required
super.push(:ext_claim_id, :person_last_name, :person_email)
end
end
end
14 changes: 14 additions & 0 deletions lib/shipsurance/claim_status_response.rb
@@ -0,0 +1,14 @@
module Shipsurance
class ClaimStatusResponse < Response
def initialize(response)
@response = response.body.split("|")
@code = @response[0]
@reason_code = @response[1]
@claim_status_type = @response[2]
@claim_status_type_description = @response[3]
@claim_comments = @response[4]
@last_updated = @response[5]
self
end
end
end
1 change: 0 additions & 1 deletion lib/shipsurance/record_shipment.rb
Expand Up @@ -118,7 +118,6 @@ def add_departure_address(address, post = {})
end

def add_departure_address_1(departure_address_1, post = {})
puts "Invoked #{add_departure_address_1}"
post[:departure_address_1] = departure_address_1
post
end
Expand Down
13 changes: 13 additions & 0 deletions lib/shipsurance/record_shipment_response.rb
@@ -0,0 +1,13 @@
module Shipsurance
class RecordShipmentResponse < Response
attr_reader :recorded_shipment_id

def initialize(response)
@response = parse(response)
@code = @response[0]
@reason_code = @response[1]
@recorded_shipment_id = @response[2]
self
end
end
end
22 changes: 19 additions & 3 deletions lib/shipsurance/response.rb
@@ -1,14 +1,30 @@
module Shipsurance
class Response
attr_reader :code, :body, :api_response_code, :success, :recorded_shipment_id
attr_reader :code, :body, :reason_code, :api_response_code

def initialize(response)
response_arry = parse(response)
@code, @api_response_code, @body, @recorded_shipment_id = response.code.to_i, response_arry[0].to_i, response_arry[1], response_arry[2]
@response_arry = parse(response)
@code, @api_response_code, @body, @recorded_shipment_id = response.code.to_i, @response_arry[0].to_i, @response_arry[1], @response_arry[2]
@success = (@api_response_code == 1)
self
end

def code
@code.to_i == 1 ? 200 : @code
end

def body
@reason_code || @body
end

def success
@code.to_i == 1
end

def success?
@success ||= success
end

def parse(response)
response.body.split(",")
end
Expand Down
19 changes: 19 additions & 0 deletions lib/shipsurance/validation_response.rb
@@ -0,0 +1,19 @@
module Shipsurance
class ValidationResponse < Response

attr_reader :validation_type, :response_reason, :response

def initialize(response)
@response = parse(response)
@code = @response[0]
@reason_code = @response[1]
@validation_type = @response[2]
@response = @response[4]
self
end

def body
@reason_code
end
end
end
11 changes: 11 additions & 0 deletions lib/shipsurance/void_record_shipment_response.rb
@@ -0,0 +1,11 @@
module Shipsurance
class VoidRecordShipmentResponse < Response

def initialize(response)
@response = parse(response)
@code = @response[0]
@reason_code = @response[1]
self
end
end
end
58 changes: 58 additions & 0 deletions spec/lib/shipsurance/claim_status_spec.rb
@@ -0,0 +1,58 @@
require File.dirname(__FILE__) + "/../../spec_helper"

describe Shipsurance::ClaimStatus do
before(:each) do
@claim_status = Shipsurance::ClaimStatus.new
@claim = Shipsurance::Claim.new
@shipment_id = Shipsurance::RecordShipment.new.commit(valid_record_shipment_params).recorded_shipment_id
@claim_response = @claim.commit({:recorded_shipment_id => @shipment_id}.merge!(add_params(valid_record_shipment_params)))
end

it "should return success" do
post = {}
post[:person_last_name] = "Matheson"
post[:person_email] = CREDENTIALS[:person_email]
post[:ext_claim_id] = @claim_response.claim_code
post[:claim_id] = @claim_response.claim_id
response = @claim_status.commit(post)
puts post.inspect
response.reason_code.should == "foo"
end

private

def add_params(post = {})
post[:person_source_identifier] = '1'
post[:person_first_name] = 'Tim'
post[:person_last_name] = 'Matheson'
post[:ext_carrier_id] = '2'
post[:consignee_full_name] = 'Tim Matheson'
post[:claim_payment_full_name] = 'Bill Smith'
post[:claim_payment_address_1] = '123 Test Address'
post[:claim_payment_city] = 'RSM'
post[:claim_payment_postal_code] = '92688'
post[:claim_payment_country] = 'US'
post[:claim_payment_phone] = '9492945624'
post[:file_date] = Time.now.strftime("%m/%d/%Y")
post[:loss_discovered_date] = Time.now.strftime("%m/%d/%Y")
post[:ext_claim_type_id] = '1'
post[:claim_description] = 'Test Description'
post[:claim_amount] = '11.99'
post[:certify_correct] = '1'
post[:claim_shipment_date] = Time.now.strftime("%m/%d/%Y")
post
end


def valid_record_shipment_params
{
:ext_shipment_type_id => 1,
:ext_carrier_id => 1,
:carrier_service_name => "UPS",
:declared_value => 50.00,
:shipment_date => Time.now.strftime("%m/%d/%Y"),
:person_email => CREDENTIALS[:person_email],
:package_description => "Test Description #{rand(9999999)}"
}
end
end
3 changes: 2 additions & 1 deletion spec/lib/shipsurance/record_shipment_spec.rb
Expand Up @@ -14,6 +14,7 @@

it "should return a transaction key given valid params" do
response = @record_shipment.commit(valid_request_params)
response.body.should == "The transaction was accepted."
response.recorded_shipment_id.should_not be_nil
end

Expand All @@ -24,7 +25,7 @@

it "should return an api response code of 1 given valid params" do
response = @record_shipment.commit(valid_request_params)
response.api_response_code.should == 1
response.code.should == 200
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/lib/shipsurance/validation_spec.rb
Expand Up @@ -12,7 +12,7 @@

it "should return an api_response_code of 1 given a valid validation type" do
response = @validation.commit({ :validation_type => "activePolicy" })
response.api_response_code.should == 1
response.code.should == 200
end

it "should return an error given an invalid validation type" do
Expand All @@ -22,6 +22,6 @@

it "should return an api_response_code of 2 given an invalid validation type" do
response = @validation.commit({ :validation_type => "foo" })
response.api_response_code.should == 2
response.code.should == "2"
end
end
1 change: 1 addition & 0 deletions spec/rcov.opts
@@ -0,0 +1 @@
--exclude "spec/*,gems/*"
13 changes: 13 additions & 0 deletions tasks/rspec.rake
@@ -0,0 +1,13 @@
require 'spec/rake/spectask'

namespace :spec do
desc "Run all specs in spec directory with RCov (excluding plugin specs)"
Spec::Rake::SpecTask.new(:rcov) do |t|
t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/../spec/spec.opts\""]
t.spec_files = FileList['spec/**/*/*_spec.rb']
t.rcov = true
t.rcov_opts = lambda do
IO.readlines(File.dirname(__FILE__) + "/../spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
end
end
end

0 comments on commit cb2dd8d

Please sign in to comment.