dopis_online_client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jiri Kubicek, KRAXNET s.r.o.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,20 @@
1
+ = dopis_online_client
2
+
3
+ DopisOnline je sluzba Ceske posty, s.p., ktera "umoznuje zakaznikovi podani dokumentu v elektronické formě (formát PDF), přičemž dodání je uskutečněno formou klasického vytištěného dopisu."
4
+
5
+ Vice informaci na https://online.postservis.cz/Download/DopisOnline/DopisOnline.pdf
6
+
7
+ == Usage:
8
+
9
+ require 'lib/dopis_online_client'
10
+
11
+ DopisOnlineClient::Request.base_uri 'http://localhost:8000'
12
+ DopisOnlineClient::Request.auth('jmeno','heslo')
13
+
14
+ DopisOnlineClient::Request.send(
15
+ :pdf_file_path=>"letter.pdf"
16
+ )
17
+
18
+ == Copyright
19
+
20
+ Copyright (c) 2009 Jiri Kubicek, KRAXNET s.r.o.. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,65 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "dopis_online_client"
8
+ gem.summary = %Q{Dopis Online Client Library}
9
+ gem.email = "jiri.kubicek@kraxnet.cz"
10
+ gem.homepage = "http://github.com/kraxnet/dopis_online_client"
11
+ gem.authors = ["Jiri Kubicek"]
12
+ gem.description = "Klientská knihovna pro práci se službou Dopis Online České pošty"
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ begin
41
+ require 'cucumber/rake/task'
42
+ Cucumber::Rake::Task.new(:features)
43
+ rescue LoadError
44
+ task :features do
45
+ abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
46
+ end
47
+ end
48
+
49
+ task :default => :test
50
+
51
+ require 'rake/rdoctask'
52
+ Rake::RDocTask.new do |rdoc|
53
+ if File.exist?('VERSION.yml')
54
+ config = YAML.load(File.read('VERSION.yml'))
55
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
56
+ else
57
+ version = ""
58
+ end
59
+
60
+ rdoc.rdoc_dir = 'rdoc'
61
+ rdoc.title = "frest_client #{version}"
62
+ rdoc.rdoc_files.include('README*')
63
+ rdoc.rdoc_files.include('lib/**/*.rb')
64
+ end
65
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,12 @@
1
+ begin
2
+ require 'rubygems'
3
+ gem 'crack'
4
+ require 'crack'
5
+ # raise LoadError, "You must have crack installed"
6
+ end
7
+
8
+ module DopisOnlineClient
9
+ end
10
+
11
+ require File.dirname(__FILE__) + '/dopis_online_client/request.rb'
12
+ require File.dirname(__FILE__) + '/dopis_online_client/response.rb'
@@ -0,0 +1,58 @@
1
+ require 'httpclient'
2
+
3
+ module DopisOnlineClient
4
+ class Request
5
+
6
+ attr_reader :username, :password, :color, :postage_type, :payment_type, :response_format, :pdf_file
7
+
8
+ def initialize(params)
9
+ @username = params[:username]
10
+ @password = params[:password]
11
+ @color = params[:color] || 0 # cernobile
12
+ @postage_type = params[:postage_type] || 66 # obycejne
13
+ @payment_type = params[:payment_type] || 0 # fakturou
14
+ @format = params[:format] || :xml
15
+ @pdf_file_path = params[:pdf_file_path]
16
+ end
17
+
18
+ def deliver
19
+ client = HTTPClient.new
20
+ client.ssl_config.verify_mode=OpenSSL::SSL::VERIFY_NONE
21
+ response = client.post @@base_uri, {
22
+ :user => @@username,
23
+ :passwd => @@password,
24
+ :barvatisku => @color,
25
+ :typvyplatneho => @postage_type,
26
+ :typuhrady => @payment_type,
27
+ :typvystupu => @format.to_s,
28
+ :soubor => File.new(@pdf_file_path)
29
+ }
30
+ parsed_response = parse_response(response.body.content)
31
+ DopisOnlineClient::Response.new(parsed_response, response.body, response.code)
32
+ end
33
+
34
+ def parse_response(body)
35
+ return nil if body.nil? or body.empty?
36
+ case @format
37
+ when :xml
38
+ Crack::XML.parse(body)
39
+ else
40
+ body
41
+ end
42
+ end
43
+
44
+ def self.send(params)
45
+ @request = new(params).deliver
46
+ end
47
+
48
+ def self.base_uri(uri)
49
+ @@base_uri=uri
50
+ end
51
+
52
+ def self.auth(username, password)
53
+ @@username=username
54
+ @@password=password
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,75 @@
1
+ module DopisOnlineClient
2
+ class Response
3
+ attr_accessor :body, :code
4
+ attr_reader :delegate
5
+
6
+ def initialize(delegate, body, code)
7
+ @delegate = delegate
8
+ @body = body
9
+ @code = code.to_i
10
+ end
11
+
12
+ def method_missing(name, *args, &block)
13
+ @delegate.send(name, *args, &block)
14
+ end
15
+
16
+ def success?
17
+ code==200 && !has_key?("chyba") && has_key?("zakazka")
18
+ end
19
+
20
+ def filename
21
+ success_result["soubor"]
22
+ end
23
+
24
+ def pages_count
25
+ success_result["pocetstranek"].to_i
26
+ end
27
+
28
+ def order_code
29
+ success_result["kodobjednavky"].to_i
30
+ end
31
+
32
+ def tracking_number
33
+ success_result["podacicislo"]
34
+ end
35
+
36
+ def posting_date
37
+ success_result["datumpodani"]
38
+ end
39
+
40
+ def posting_zip
41
+ success_result["podaciposta"]
42
+ end
43
+
44
+ def price
45
+ success_result["cena"]
46
+ end
47
+
48
+ def color
49
+ success_result["barvatisku"]=="1"
50
+ end
51
+
52
+ def shipping_method
53
+ success_result["typvyplatneho"]
54
+ end
55
+
56
+ def failure_message
57
+ failure_result["popis"]
58
+ end
59
+
60
+ def failure_code
61
+ failure_result["kod"].to_i
62
+ end
63
+
64
+ private
65
+ def success_result
66
+ return {} unless has_key?("zakazka")
67
+ fetch("zakazka")
68
+ end
69
+
70
+ def failure_result
71
+ return {} unless has_key?("chyba")
72
+ fetch("chyba")
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class DopisOnlineResponseTest < Test::Unit::TestCase
4
+
5
+ should "parse success correctly" do
6
+ response = load_response_from_file("success.xml")
7
+
8
+ response.code.should == 200
9
+ response.success?.should == true
10
+
11
+ response.failure_code.should == 0
12
+ response.failure_message.should == nil
13
+
14
+ response.filename.should == "testing.pdf"
15
+ response.pages_count.should == 1
16
+ response.order_code.should == 20090608050001
17
+ response.tracking_number.should == ""
18
+ response.datumpodani.should == Date.parse("2009-06-08")
19
+ response.price.should_not == nil
20
+ response.color.should == false
21
+ response.shipping_method.should == 67
22
+ end
23
+
24
+ should "parse failure correctly" do
25
+ response = load_response_from_file("failure.xml")
26
+
27
+ response.code.should == 200
28
+ response.success?.should == false
29
+ response.failure_code.should == 8
30
+ response.failure_message.should_not == nil
31
+ end
32
+
33
+ def load_response_from_file(filename)
34
+ body = File.read(File.join(File.dirname(__FILE__),'fixtures',filename))
35
+ parsed_response = Crack::XML.parse(body)
36
+ DopisOnlineClient::Response.new(parsed_response, body, 200)
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ <?xml version="1.0" encoding="windows-1250"?>
2
+ <chyba>
3
+ <kod>8</kod>
4
+ <popis>Promìnná je prázdná</popis>
5
+ </chyba>
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="windows-1250"?>
2
+ <zakazka>
3
+ <soubor type="string">testing.pdf</soubor>
4
+ <pocetstranek type="integer">1</pocetstranek>
5
+ <kodobjednavky type="string">20090608050001</kodobjednavky>
6
+ <podacicislo type="string"></podacicislo>
7
+ <datumpodani type="date">2009-06-08</datumpodani>
8
+ <podaciposta type="string">370 20</podaciposta>
9
+ <cena mena="CZK" type="decimal">14.05</cena>
10
+ <barvatisku type="integer">0</barvatisku>
11
+ <typvyplatneho type="integer">67</typvyplatneho>
12
+ </zakazka>
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'matchy'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'dopis_online_client'
9
+
10
+ class Test::Unit::TestCase
11
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dopis_online_client
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jiri Kubicek
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2009-09-30 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: "Klientsk\xC3\xA1 knihovna pro pr\xC3\xA1ci se slu\xC5\xBEbou Dopis Online \xC4\x8Cesk\xC3\xA9 po\xC5\xA1ty"
23
+ email: jiri.kubicek@kraxnet.cz
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - LICENSE
30
+ - README.rdoc
31
+ files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ - Rakefile
35
+ - VERSION
36
+ - lib/dopis_online_client.rb
37
+ - lib/dopis_online_client/request.rb
38
+ - lib/dopis_online_client/response.rb
39
+ - test/dopis_online_response_test.rb
40
+ - test/fixtures/failure.xml
41
+ - test/fixtures/success.xml
42
+ - test/test_helper.rb
43
+ has_rdoc: true
44
+ homepage: http://github.com/kraxnet/dopis_online_client
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.6.2
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Dopis Online Client Library
77
+ test_files:
78
+ - test/dopis_online_response_test.rb
79
+ - test/test_helper.rb