postalmethods 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe "Document Processing" do
4
+
5
+ before :each do
6
+ @client = PostalMethods::Client.new(PM_OPTS)
7
+ end
8
+
9
+ it "should open a valid pre-opened document" do
10
+ @client.document = open(File.dirname(__FILE__) + '/../doc/sample.pdf')
11
+ @client.document.class.should == Hash
12
+ end
13
+
14
+ it "should open a valid document path" do
15
+ @client.document = File.dirname(__FILE__) + '/../doc/sample.pdf'
16
+ @client.document.class.should == Hash
17
+ end
18
+
19
+ it "should create a hash with the right elements" do
20
+ @client.document = File.dirname(__FILE__) + '/../doc/sample.pdf'
21
+ @client.document[:extension].should == "pdf"
22
+ @client.document[:bytes].length.should == 213312
23
+ @client.document[:name].should == "sample.pdf"
24
+ @client.document[:file_obj].class.should == File
25
+ end
26
+
27
+ it "should return true on a valid document path" do
28
+ @client.document = File.dirname(__FILE__) + '/../doc/sample.pdf'
29
+ @client.document?.should == true
30
+ end
31
+
32
+ it "should throw an exception on a false path" do
33
+ @doc = File.dirname(__FILE__) + '/../doc/does_not_exist.pdf'
34
+ lambda {@client.document = @doc}.should raise_error(Errno::ENOENT)
35
+ end
36
+
37
+ end
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe "Get Letter Status" do
4
+
5
+ before :each do
6
+ @doc = open(File.dirname(__FILE__) + '/../doc/sample.pdf')
7
+ @client = PostalMethods::Client.new(PM_OPTS)
8
+ @client.prepare!
9
+ sleep(10)
10
+ end
11
+
12
+ it "should send a letter and get status" do
13
+ ## use a static known good id that has gone thru in dev mode
14
+ f = @client.get_letter_status(1023577)
15
+ f.length.should == 2
16
+ f.first.to_i.should == -1002
17
+ end
18
+
19
+ it "should try to get status of a letter i don't have access to" do
20
+ lambda { @client.get_letter_status(1)}.should raise_error(PostalMethods::APIStatusCode3001Exception)
21
+ end
22
+
23
+
24
+ it "should send multiple letters and get their status" do
25
+ letters = []
26
+ 1.upto(3) do
27
+ @doc = open(File.dirname(__FILE__) + '/../doc/sample.pdf')
28
+ @client = PostalMethods::Client.new(PM_OPTS)
29
+ @client.prepare!
30
+ rv = @client.send_letter(@doc, "the long goodbye").sendLetterResult
31
+ rv.to_i.should > 0
32
+ letters << rv
33
+ #sleep(10) # api needs some time
34
+ end
35
+
36
+ ret = @client.get_letter_status_multiple(letters)
37
+ ret.should be_an_instance_of(Array)
38
+
39
+ # the return is an array [results, status]
40
+ recv_letters = ret.collect { |r| r.iD }
41
+
42
+ recv_letters.should == letters
43
+ end
44
+
45
+ it "should attempt to request a multiple array of invalid letters" do
46
+ lambda { @client.get_letter_status_multiple([1,2,3]) }.should raise_error(PostalMethods::APIStatusCode3115Exception)
47
+ end
48
+
49
+ it "should request a range of letters and get their status" do
50
+ letters = []
51
+ 1.upto(3) do
52
+ @doc = open(File.dirname(__FILE__) + '/../doc/sample.pdf')
53
+ @client = PostalMethods::Client.new(PM_OPTS)
54
+ @client.prepare!
55
+ rv = @client.send_letter(@doc, "the long goodbye").sendLetterResult
56
+ rv.to_i.should > 0
57
+ letters << rv
58
+ #sleep(10) # api needs some time
59
+ end
60
+
61
+ ret = @client.get_letter_status_range(letters.first, letters.last)
62
+ ret.should be_an_instance_of(Array)
63
+
64
+ # the return is an array [results, status]
65
+ recv_letters = ret.collect { |r| r.iD }
66
+
67
+ recv_letters.should == letters
68
+ end
69
+
70
+ it "should attempt to request a range of invalid letters" do
71
+ lambda { @client.get_letter_status_range(1,3) }.should raise_error(PostalMethods::APIStatusCode3115Exception)
72
+ end
73
+
74
+ end
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe "Client" do
4
+
5
+ it "should instantiate a client with a username and password" do
6
+ c = PostalMethods::Client.new(PM_OPTS)
7
+ c.class.should == PostalMethods::Client
8
+ end
9
+
10
+ it "should fail without a user/pass on instantiation" do
11
+ lambda {PostalMethods::Client.new()}.should raise_error(PostalMethods::NoCredentialsException)
12
+ end
13
+
14
+ it "should create a driver client thru the factory" do
15
+ c = PostalMethods::Client.new(PM_OPTS)
16
+ c.prepare!
17
+ c.rpc_driver.class.should == SOAP::RPC::Driver
18
+ end
19
+
20
+ it "should raise a connection error exception when the api is unreachable" do
21
+ c = PostalMethods::Client.new(PM_OPTS)
22
+ c.stubs(:api_uri).returns("http://invaliduri.tld/api_endpoint.wtf?")
23
+ lambda {c.prepare!}.should raise_error(PostalMethods::NoConnectionError)
24
+ end
25
+
26
+ end
data/spec/rcov.opts ADDED
@@ -0,0 +1 @@
1
+ --exclude "spec/*,gems/*"
@@ -0,0 +1,75 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe "Send Letter" do
4
+
5
+ before :each do
6
+ @doc = open(File.dirname(__FILE__) + '/../doc/sample.pdf')
7
+ @client = PostalMethods::Client.new(PM_OPTS)
8
+ end
9
+
10
+ it "should instantiate and send a letter" do
11
+ @client.prepare!
12
+ rv = @client.send_letter(@doc, "the long goodbye")
13
+ rv.sendLetterResult.to_i.should > 0
14
+ end
15
+
16
+ it "should refuse to send letter before prepare" do
17
+ lambda {@client.send_letter(@doc, "the long goodbye")}.should raise_error(PostalMethods::NoPreparationException)
18
+ end
19
+
20
+ it "should raise the proper exception when trying to send textfile" do
21
+ @doc = open(File.dirname(__FILE__) + '/../README.txt')
22
+ @client.prepare!
23
+ lambda {@client.send_letter(@doc, "the long goodbye")}.should raise_error(PostalMethods::APIStatusCode3004Exception)
24
+ end
25
+
26
+ it "should raise the proper exception when trying to send an empty string" do
27
+ @client.prepare!
28
+ lambda {@client.send_letter("", "the long goodbye")}.should raise_error(Errno::ENOENT)
29
+ end
30
+
31
+ #it "should raise the proper exception when trying to send no description" do
32
+ # @client.prepare!
33
+ # lambda {@client.send_letter(@doc, nil)}.should raise_error(PostalMethods::APIStatusCode3004Exception)
34
+ #end
35
+
36
+ end
37
+
38
+ describe "Send Letter With Address" do
39
+
40
+ before :each do
41
+ @doc = open(File.dirname(__FILE__) + '/../doc/sample.pdf')
42
+ @addr = {:AttentionLine1 => "The Fonz", :Address1 => "Happy Days", :City => "Baja", :State => "CA",
43
+ :PostalCode => "90210", :Country => "USA"}
44
+ @client = PostalMethods::Client.new(PM_OPTS)
45
+ end
46
+
47
+ it "should instantiate and send a letter with address" do
48
+ @client.prepare!
49
+ rv = @client.send_letter_and_address(@doc, "Shark Jumping Notes", @addr)
50
+ rv.sendLetterAndAddressResult.to_i.should > 0
51
+ end
52
+
53
+ it "should raise the proper exception when trying to send letter without valid attention line" do
54
+ @client.prepare!
55
+ addr = @addr.except(:AttentionLine1)
56
+ lambda {@client.send_letter_and_address(@doc, "the long goodbye", addr)}.should raise_error(PostalMethods::APIStatusCode4008Exception)
57
+ end
58
+
59
+ it "should refuse to send letter before prepare" do
60
+ lambda {@client.send_letter_and_address(@doc, "the long goodbye", @addr)}.should raise_error(PostalMethods::NoPreparationException)
61
+ end
62
+
63
+ it "should raise the proper exception when trying to send textfile" do
64
+ @doc = open(File.dirname(__FILE__) + '/../README.txt')
65
+ @client.prepare!
66
+ lambda {@client.send_letter_and_address(@doc, "the long goodbye", @addr)}.should raise_error(PostalMethods::APIStatusCode3004Exception)
67
+ end
68
+
69
+ it "should raise the proper exception when trying to send an empty string" do
70
+ @client.prepare!
71
+ lambda {@client.send_letter_and_address("", "the long goodbye", @addr)}.should raise_error(Errno::ENOENT)
72
+ end
73
+
74
+
75
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,7 @@
1
+ --colour
2
+ --format
3
+ specdoc
4
+ --loadby
5
+ mtime
6
+ --reverse
7
+ --backtrace
@@ -0,0 +1,30 @@
1
+ begin
2
+ require 'spec'
3
+ require 'mocha'
4
+ rescue LoadError
5
+ require 'rubygems'
6
+ require 'spec'
7
+ require 'mocha'
8
+ end
9
+
10
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
11
+ require 'postalmethods'
12
+
13
+ $VERBOSE = nil ##silence annoying warnings from soap4r
14
+
15
+ PM_OPTS = {:username => 'imajes', :password => 'rubyr00ls'}
16
+
17
+ # hash hacks to make hacking in specs easier
18
+ class Hash
19
+ # for excluding keys
20
+ def except(*exclusions)
21
+ self.reject { |key, value| exclusions.include? key.to_sym }
22
+ end
23
+
24
+ # for overriding keys
25
+ def with(overrides = {})
26
+ self.merge overrides
27
+ end
28
+ end
29
+
30
+ require "ruby-debug"
@@ -0,0 +1,62 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ require 'base64'
3
+
4
+ describe "Utility Methods" do
5
+
6
+ before :each do
7
+ @doc = open(File.dirname(__FILE__) + '/../doc/sample.pdf')
8
+ @client = PostalMethods::Client.new(PM_OPTS)
9
+ @client.prepare!
10
+ end
11
+
12
+ it "Should get a pdf returned for a file sent" do
13
+ rv = @client.get_pdf(1023117) ## use known static / magic file. sucks, but what can you do?
14
+ rv.length.should == 49606
15
+ f = open("test_output.pdf", "w")
16
+
17
+ f.write(rv)
18
+ (`file test_output.pdf`).should == "test_output.pdf: PDF document, version 1.3\n"
19
+ system("rm test_output.pdf")
20
+ end
21
+
22
+ it "should check for error for when there is no file yet" do
23
+ res = @client.send_letter(@doc, "the long goodbye")
24
+ id = res.sendLetterResult.to_i
25
+
26
+ id.should > 0
27
+ #
28
+ lambda {@client.get_pdf(id)}.should raise_error(PostalMethods::APIStatusCode3020Exception)
29
+ end
30
+
31
+ it "should get the details of a letter" do
32
+ id = @client.send_letter(@doc, "the long goodbye").sendLetterResult.to_i
33
+ sleep(10) # because it's a tired little clients
34
+ details = @client.get_letter_details(id)
35
+
36
+ # check the return...
37
+ details.should be_an_instance_of(Array)
38
+
39
+ # and now the item...
40
+ details.first.should be_an_instance_of(SOAP::Mapping::Object)
41
+
42
+ details.first.price.should == "0.00"
43
+ details.first.iD.to_i.should == id ## be careful not to confuse with class id
44
+ end
45
+
46
+ it "should raise an error when trying to get details of an invalid letter" do
47
+ sleep(6) # got to wait 6 seconds...
48
+ lambda { @client.get_letter_details(1)}.should raise_error(PostalMethods::APIStatusCode3001Exception)
49
+ end
50
+
51
+ it "should cancel delivery of a letter" do
52
+ id = @client.send_letter(@doc, "the long goodbye").sendLetterResult.to_i
53
+ rv = @client.cancel_delivery(id)
54
+ rv.should be_true
55
+ end
56
+
57
+ it "should raise an error when trying to cancel an invalid letter" do
58
+ lambda { @client.cancel_delivery(1)}.should raise_error(PostalMethods::APIStatusCode3001Exception)
59
+ end
60
+
61
+
62
+ end
@@ -0,0 +1,34 @@
1
+ desc 'Release the website and new gem version'
2
+ task :deploy => [:check_version, :website, :release] do
3
+ puts "Remember to create SVN tag:"
4
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
5
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
6
+ puts "Suggested comment:"
7
+ puts "Tagging release #{CHANGES}"
8
+ end
9
+
10
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
11
+ task :local_deploy => [:website_generate, :install_gem]
12
+
13
+ task :check_version do
14
+ unless ENV['VERSION']
15
+ puts 'Must pass a VERSION=x.y.z release version'
16
+ exit
17
+ end
18
+ unless ENV['VERSION'] == VERS
19
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
20
+ exit
21
+ end
22
+ end
23
+
24
+ desc 'Install the package as a gem, without generating documentation(ri/rdoc)'
25
+ task :install_gem_no_doc => [:clean, :package] do
26
+ sh "#{'sudo ' unless Hoe::WINDOZE }gem install pkg/*.gem --no-rdoc --no-ri"
27
+ end
28
+
29
+ namespace :manifest do
30
+ desc 'Recreate Manifest.txt to include ALL files'
31
+ task :refresh do
32
+ `rake check_manifest | patch -p0 > Manifest.txt`
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ task :ruby_env do
2
+ RUBY_APP = if RUBY_PLATFORM =~ /java/
3
+ "jruby"
4
+ else
5
+ "ruby"
6
+ end unless defined? RUBY_APP
7
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,33 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
22
+
23
+ namespace :spec do
24
+ desc "Run all specs in spec directory with RCov (excluding plugin specs)"
25
+ Spec::Rake::SpecTask.new(:rcov) do |t|
26
+ t.spec_opts = ['--options', "\"spec/spec.opts\""]
27
+ t.spec_files = FileList['spec/**/*_spec.rb']
28
+ t.rcov = true
29
+ t.rcov_opts = lambda do
30
+ IO.readlines("spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postalmethods
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - James Cox
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-09-22 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.7.0
24
+ version:
25
+ description: Wrapper for the Postal Methods API
26
+ email:
27
+ - james-at-imaj.es
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - License.txt
35
+ - Manifest.txt
36
+ - PostInstall.txt
37
+ - README.txt
38
+ files:
39
+ - History.txt
40
+ - License.txt
41
+ - Manifest.txt
42
+ - PostInstall.txt
43
+ - README.txt
44
+ - Rakefile
45
+ - config/hoe.rb
46
+ - config/requirements.rb
47
+ - lib/postalmethods.rb
48
+ - lib/postalmethods/version.rb
49
+ - lib/postalmethods/document_processor.rb
50
+ - lib/postalmethods/exceptions.rb
51
+ - lib/postalmethods/get_letter_status.rb
52
+ - lib/postalmethods/send_letter.rb
53
+ - lib/postalmethods/utility.rb
54
+ - script/console
55
+ - script/destroy
56
+ - script/generate
57
+ - setup.rb
58
+ - spec/document_processor_spec.rb
59
+ - spec/get_letter_status_spec.rb
60
+ - spec/postalmethods_spec.rb
61
+ - spec/send_letter_spec.rb
62
+ - spec/utility_spec.rb
63
+ - spec/rcov.opts
64
+ - spec/spec.opts
65
+ - spec/spec_helper.rb
66
+ - tasks/deployment.rake
67
+ - tasks/environment.rake
68
+ - tasks/rspec.rake
69
+ has_rdoc: true
70
+ homepage: http://postalmethods.rubyforge.org
71
+ post_install_message: |+
72
+
73
+ For more information on working with Postal Methods,
74
+ see the API reference here:
75
+
76
+ http://www.postalmethods.com/resources/quickstart
77
+
78
+ rdoc_options:
79
+ - --main
80
+ - README.txt
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ version:
95
+ requirements: []
96
+
97
+ rubyforge_project: postalmethods
98
+ rubygems_version: 1.2.0
99
+ signing_key:
100
+ specification_version: 2
101
+ summary: Wrapper for the Postal Methods API
102
+ test_files: []
103
+