interfax-ruby 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2@interfax-ruby --create
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "savon", "0.9.9"
4
+
5
+ group :development do
6
+ gem 'ruby-debug19', '0.11.6'
7
+ gem "rspec", "2.8.0"
8
+ gem "rdoc", "3.12"
9
+ gem "bundler", "1.0.22"
10
+ gem "jeweler", "1.8.3"
11
+ gem "simplecov", "0.5.3", :require => false
12
+ gem "fakeweb", "1.3.0"
13
+ gem "vcr", "1.11.3"
14
+ end
@@ -0,0 +1,77 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ akami (1.0.0)
5
+ gyoku (>= 0.4.0)
6
+ archive-tar-minitar (0.5.2)
7
+ builder (3.0.0)
8
+ columnize (0.3.6)
9
+ diff-lcs (1.1.3)
10
+ fakeweb (1.3.0)
11
+ git (1.2.5)
12
+ gyoku (0.4.4)
13
+ builder (>= 2.1.2)
14
+ httpi (0.9.5)
15
+ rack
16
+ jeweler (1.8.3)
17
+ bundler (~> 1.0)
18
+ git (>= 1.2.5)
19
+ rake
20
+ rdoc
21
+ json (1.6.5)
22
+ linecache19 (0.5.12)
23
+ ruby_core_source (>= 0.1.4)
24
+ multi_json (1.0.4)
25
+ nokogiri (1.5.0)
26
+ nori (1.1.0)
27
+ rack (1.4.1)
28
+ rake (0.9.2.2)
29
+ rdoc (3.12)
30
+ json (~> 1.4)
31
+ rspec (2.8.0)
32
+ rspec-core (~> 2.8.0)
33
+ rspec-expectations (~> 2.8.0)
34
+ rspec-mocks (~> 2.8.0)
35
+ rspec-core (2.8.0)
36
+ rspec-expectations (2.8.0)
37
+ diff-lcs (~> 1.1.2)
38
+ rspec-mocks (2.8.0)
39
+ ruby-debug-base19 (0.11.25)
40
+ columnize (>= 0.3.1)
41
+ linecache19 (>= 0.5.11)
42
+ ruby_core_source (>= 0.1.4)
43
+ ruby-debug19 (0.11.6)
44
+ columnize (>= 0.3.1)
45
+ linecache19 (>= 0.5.11)
46
+ ruby-debug-base19 (>= 0.11.19)
47
+ ruby_core_source (0.1.5)
48
+ archive-tar-minitar (>= 0.5.2)
49
+ savon (0.9.9)
50
+ akami (~> 1.0)
51
+ builder (>= 2.1.2)
52
+ gyoku (>= 0.4.0)
53
+ httpi (~> 0.9)
54
+ nokogiri (>= 1.4.0)
55
+ nori (~> 1.1)
56
+ wasabi (~> 2.1)
57
+ simplecov (0.5.3)
58
+ multi_json (~> 1.0.3)
59
+ simplecov-html (~> 0.5.3)
60
+ simplecov-html (0.5.3)
61
+ vcr (1.11.3)
62
+ wasabi (2.1.0)
63
+ nokogiri (>= 1.4.0)
64
+
65
+ PLATFORMS
66
+ ruby
67
+
68
+ DEPENDENCIES
69
+ bundler (= 1.0.22)
70
+ fakeweb (= 1.3.0)
71
+ jeweler (= 1.8.3)
72
+ rdoc (= 3.12)
73
+ rspec (= 2.8.0)
74
+ ruby-debug19 (= 0.11.6)
75
+ savon (= 0.9.9)
76
+ simplecov (= 0.5.3)
77
+ vcr (= 1.11.3)
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2013 Rafael Lima
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # Ruby Gem for Interfax SOAP API
2
+
3
+ This is a ruby wrapper to send fax using [Interfax](http://www.interfax.net/)
4
+
5
+ API Reference: [Outbound Fax SOAP API](http://www.interfax.net/en/dev/webservice/reference)
6
+
7
+ ## Install
8
+
9
+ gem install interfax-ruby
10
+
11
+ ### Usage
12
+
13
+ ```ruby
14
+ require 'interfax'
15
+
16
+ interfax = Interfax.new(username: 'myuser', password: 'mypassword')
17
+ source_path = "/tmp/myfile.pdf"
18
+ if interfax.upload_file_chunk(source_path)
19
+ result = interfax.sendfax_ex_2(
20
+ fax_numbers: '+55-21-99999999',
21
+ postpone: '2000-01-01T10:00:00.000000-03:00',
22
+ retries_to_perform: 1,
23
+ csid: 'YOURAPP',
24
+ page_header: '{Datedd/MM/yy} {TimeHHmmss} - https://www.webfax.me',
25
+ subject: "whatever",
26
+ )
27
+ if result > 0
28
+ puts "Success :)"
29
+ else
30
+ puts "Error :("
31
+ end
32
+ end
33
+
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
39
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
40
+ * Fork the project.
41
+ * Start a feature/bugfix branch.
42
+ * Commit and push until you are happy with your contribution.
43
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
44
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
45
+
46
+ ## License
47
+
48
+ This code is released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
49
+
50
+ ## Bugs, Issues, Thanks, etc
51
+
52
+ Comments and feedbacks are welcome through project's [issue tracker](http://github.com/rafaelp/interfax-ruby/issues)
53
+
54
+ ## Author
55
+
56
+ [**Rafael Lima**](http://github.com/rafaelp) working for [BielSystems](http://bielsystems.com.br)
57
+
58
+ Blog: [http://rafael.adm.br](http://rafael.adm.br)
59
+
60
+ Twitter: [http://twitter.com/rafaelp](http://twitter.com/rafaelp)
61
+
62
+ ### Did you like it?
63
+
64
+ [Recommend me on Working With Rails](http://workingwithrails.com/recommendation/new/person/14248-rafael-lima)
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "interfax-ruby"
18
+ gem.homepage = "http://github.com/rafaelp/interfax-ruby"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Ruby wrapper based on savon for Interfax SOAP API}
21
+ gem.description = %Q{Ruby wrapper based on savon for Interfax SOAP API}
22
+ gem.email = "contato@rafael.adm.br"
23
+ gem.authors = ["Rafael Lima"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "interfax-ruby #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1,86 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "interfax-ruby"
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Rafael Lima"]
12
+ s.date = "2013-03-14"
13
+ s.description = "Ruby wrapper based on savon for Interfax SOAP API"
14
+ s.email = "contato@rafael.adm.br"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ ".rvmrc",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "interfax-ruby.gemspec",
30
+ "lib/interfax.rb",
31
+ "spec/fixtures/mickey01.jpg",
32
+ "spec/fixtures/vcr_cassettes/cancel_file_upload.yml",
33
+ "spec/fixtures/vcr_cassettes/cancel_file_upload_with_session.yml",
34
+ "spec/fixtures/vcr_cassettes/sendfax_ex_2_authorized_default_values.yml",
35
+ "spec/fixtures/vcr_cassettes/sendfax_ex_2_authorized_required_values.yml",
36
+ "spec/fixtures/vcr_cassettes/start_file_upload_authorized.yml",
37
+ "spec/fixtures/vcr_cassettes/start_file_upload_unauthorized.yml",
38
+ "spec/fixtures/vcr_cassettes/upload_file_chunk_authorized.yml",
39
+ "spec/interfax_spec.rb",
40
+ "spec/spec_helper.rb",
41
+ "spec/support/savon.rb",
42
+ "spec/support/vcr.rb"
43
+ ]
44
+ s.homepage = "http://github.com/rafaelp/interfax-ruby"
45
+ s.licenses = ["MIT"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = "1.8.24"
48
+ s.summary = "Ruby wrapper based on savon for Interfax SOAP API"
49
+
50
+ if s.respond_to? :specification_version then
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
+ s.add_runtime_dependency(%q<savon>, ["= 0.9.9"])
55
+ s.add_development_dependency(%q<ruby-debug19>, ["= 0.11.6"])
56
+ s.add_development_dependency(%q<rspec>, ["= 2.8.0"])
57
+ s.add_development_dependency(%q<rdoc>, ["= 3.12"])
58
+ s.add_development_dependency(%q<bundler>, ["= 1.0.22"])
59
+ s.add_development_dependency(%q<jeweler>, ["= 1.8.3"])
60
+ s.add_development_dependency(%q<simplecov>, ["= 0.5.3"])
61
+ s.add_development_dependency(%q<fakeweb>, ["= 1.3.0"])
62
+ s.add_development_dependency(%q<vcr>, ["= 1.11.3"])
63
+ else
64
+ s.add_dependency(%q<savon>, ["= 0.9.9"])
65
+ s.add_dependency(%q<ruby-debug19>, ["= 0.11.6"])
66
+ s.add_dependency(%q<rspec>, ["= 2.8.0"])
67
+ s.add_dependency(%q<rdoc>, ["= 3.12"])
68
+ s.add_dependency(%q<bundler>, ["= 1.0.22"])
69
+ s.add_dependency(%q<jeweler>, ["= 1.8.3"])
70
+ s.add_dependency(%q<simplecov>, ["= 0.5.3"])
71
+ s.add_dependency(%q<fakeweb>, ["= 1.3.0"])
72
+ s.add_dependency(%q<vcr>, ["= 1.11.3"])
73
+ end
74
+ else
75
+ s.add_dependency(%q<savon>, ["= 0.9.9"])
76
+ s.add_dependency(%q<ruby-debug19>, ["= 0.11.6"])
77
+ s.add_dependency(%q<rspec>, ["= 2.8.0"])
78
+ s.add_dependency(%q<rdoc>, ["= 3.12"])
79
+ s.add_dependency(%q<bundler>, ["= 1.0.22"])
80
+ s.add_dependency(%q<jeweler>, ["= 1.8.3"])
81
+ s.add_dependency(%q<simplecov>, ["= 0.5.3"])
82
+ s.add_dependency(%q<fakeweb>, ["= 1.3.0"])
83
+ s.add_dependency(%q<vcr>, ["= 1.11.3"])
84
+ end
85
+ end
86
+
@@ -0,0 +1,81 @@
1
+ require 'savon'
2
+ require 'base64'
3
+
4
+ class Interfax
5
+ WSDL_URL = "https://ws.interfax.net/dfs.asmx?WSDL"
6
+ def initialize(options = {})
7
+ @username = options[:username] || ENV['INTERFAX_USERNAME']
8
+ @password = options[:password] || ENV['INTERFAX_PASSWORD']
9
+ @raw_response = {}
10
+ @bytes_uploaded = 0
11
+ Gyoku.convert_symbols_to(:camelcase)
12
+ end
13
+
14
+ def client
15
+ @client ||= ::Savon::Client.new(WSDL_URL)
16
+ end
17
+
18
+ def start_file_upload
19
+ @last_session_id = request :start_file_upload do |response|
20
+ response[:session_id]
21
+ end
22
+ end
23
+
24
+ def upload_file_chunk(file_path, options = {})
25
+ chunk_size = options.delete(:chunk_size) || 200000
26
+ session_id = options.delete(:session_id) || @last_session_id || start_file_upload
27
+ @file_path = file_path
28
+
29
+ each_chunk(chunk_size) do |chunk, is_last|
30
+ request :upload_file_chunk, {
31
+ :chunk => Base64.encode64(chunk),
32
+ :SessionID => session_id,
33
+ :IsLast => is_last ? 1 : 0
34
+ } do |response, result|
35
+ @bytes_uploaded = result
36
+ end
37
+ end
38
+ return @bytes_uploaded == File.size(@file_path)
39
+ end
40
+
41
+ def cancel_file_upload(options = {})
42
+ session_id = options.delete(:session_id) || @last_session_id
43
+ return if session_id.nil?
44
+ request :cancel_file_upload, :session_id => session_id
45
+ end
46
+
47
+ def sendfax_ex_2(options = {})
48
+ default_options = {
49
+ :retries_to_perform => 1,
50
+ :page_size => 'A4',
51
+ :page_orientation => 'Portrait',
52
+ :is_high_resolution => false,
53
+ :is_fine_rendering => true,
54
+ }
55
+ default_options.merge!(
56
+ :file_types => File.extname(@file_path)[1..4].upcase,
57
+ :file_sizes => "#{@bytes_uploaded}/sessionID=#{@last_session_id}"
58
+ ) unless @bytes_uploaded.zero?
59
+ options = default_options.merge(options)
60
+ options[:is_high_resolution] = options[:is_high_resolution] ? 1 : 0 unless options[:is_high_resolution].is_a? Integer
61
+ options[:is_fine_rendering] = options[:is_fine_rendering] ? 1 : 0 unless options[:is_fine_rendering].is_a? Integer
62
+ request :sendfax_ex_2, options
63
+ end
64
+
65
+ private
66
+ def request(method_name, options = {}, &block)
67
+ raw_response = client.request :int, method_name, {:body => {:username => @username, :password => @password}.merge(options)}
68
+ return false unless raw_response.success?
69
+ result = raw_response.to_hash["#{method_name}_response".to_sym]["#{method_name}_result".to_sym].to_i
70
+ response = raw_response.to_hash["#{method_name}_response".to_sym]
71
+ return result unless block_given?
72
+ yield response, result
73
+ end
74
+
75
+ # reads the given +file+ in chunks set though +chunk_size+ and yields
76
+ # the current chunk and whether it's the last chunk to iterate over.
77
+ def each_chunk(chunk_size)
78
+ File.open(@file_path, "rb") { |f| yield(f.read(chunk_size), f.eof?) until f.eof? }
79
+ end
80
+
81
+ end
@@ -0,0 +1,42 @@
1
+ ---
2
+ - !ruby/struct:VCR::HTTPInteraction
3
+ request: !ruby/struct:VCR::Request
4
+ method: :post
5
+ uri: https://ws.interfax.net:443/dfs.asmx
6
+ body: <?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
7
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.interfax.cc"
8
+ xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins0="http://www.interfax.cc"
9
+ xmlns:ins1="http://www.interfax.cc/AbstractTypes"><env:Body><ins0:CancelFileUpload><int:Username>defaultusername</int:Username><int:Password>defaultpassword</int:Password><int:SessionId>2FE69E6C86DB4CB4926A94E63B68039AD83BCBB7F10C407F9641FEAEF5EA38EE</int:SessionId></ins0:CancelFileUpload></env:Body></env:Envelope>
10
+ headers:
11
+ soapaction:
12
+ - ! '"http://www.interfax.cc/CancelFileUpload"'
13
+ content-type:
14
+ - text/xml;charset=UTF-8
15
+ content-length:
16
+ - '592'
17
+ response: !ruby/struct:VCR::Response
18
+ status: !ruby/struct:VCR::ResponseStatus
19
+ code: 200
20
+ message: OK
21
+ headers:
22
+ date:
23
+ - Wed, 22 Feb 2012 23:56:41 GMT
24
+ server:
25
+ - Microsoft-IIS/6.0
26
+ iisexport:
27
+ - This web site was exported using IIS Export v4.2
28
+ - This web site was exported using IIS Export v4.2
29
+ x-powered-by:
30
+ - ASP.NET
31
+ x-aspnet-version:
32
+ - 2.0.50727
33
+ cache-control:
34
+ - private, max-age=0
35
+ content-type:
36
+ - text/xml; charset=utf-8
37
+ content-length:
38
+ - '380'
39
+ body: <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
40
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CancelFileUploadResponse
41
+ xmlns="http://www.interfax.cc"><CancelFileUploadResult>0</CancelFileUploadResult></CancelFileUploadResponse></soap:Body></soap:Envelope>
42
+ http_version: '1.1'