interfax 0.2.1 → 1.0.0.beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/interfax.rb CHANGED
@@ -1,9 +1,14 @@
1
- require 'time'
2
- require "soap/wsdlDriver"
1
+ require 'interfax/version'
2
+ require 'interfax/client'
3
+ require 'interfax/account'
4
+ require 'interfax/object'
5
+ require 'interfax/file'
6
+ require 'interfax/forwarding_email'
7
+ require 'interfax/outbound'
8
+ require 'interfax/outbound/fax'
9
+ require 'interfax/outbound/delivery'
10
+ require 'interfax/inbound'
11
+ require 'interfax/inbound/fax'
12
+ require 'interfax/image'
3
13
 
4
- $:.unshift(File.dirname(__FILE__)) unless
5
- $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
6
-
7
- require 'interfax/fax_item'
8
- require 'interfax/base'
9
- require 'interfax/incoming'
14
+ require 'mimemagic'
@@ -0,0 +1,10 @@
1
+ class InterFAX::Account
2
+
3
+ def initialize client
4
+ @client = client
5
+ end
6
+
7
+ def balance
8
+ @client.get('/accounts/self/ppcards/balance').to_f
9
+ end
10
+ end
@@ -0,0 +1,124 @@
1
+ require 'net/https'
2
+ require 'json'
3
+
4
+
5
+ module InterFAX
6
+ class Client
7
+ class ServerError < StandardError; end
8
+ class NotFoundError < StandardError; end
9
+ class BadRequestError < StandardError; end
10
+ class UnauthorizedError < StandardError; end
11
+
12
+ attr_accessor :username, :password, :http
13
+ attr_writer :outbound, :inbound, :account
14
+
15
+ DOMAIN = "rest.interfax.net".freeze
16
+ USER_AGENT = "InterFAX Ruby #{InterFAX::VERSION}".freeze
17
+
18
+ def initialize(options = {})
19
+ self.username = options.fetch(:username) do
20
+ ENV['INTERFAX_USERNAME'] || raise(KeyError, "Missing required argument: username")
21
+ end
22
+
23
+ self.password = options.fetch(:password) do
24
+ ENV['INTERFAX_PASSWORD'] || raise(KeyError, "Missing required argument: password")
25
+ end
26
+
27
+ self.http = Net::HTTP.new(DOMAIN, Net::HTTP.https_default_port)
28
+ http.set_debug_output $stdout if options[:debug]
29
+ http.use_ssl = true
30
+ end
31
+
32
+ def account
33
+ @account ||= InterFAX::Account.new(self)
34
+ end
35
+
36
+ def deliver params = {}
37
+ outbound.deliver(params)
38
+ end
39
+
40
+ def outbound
41
+ @outbound ||= InterFAX::Outbound.new(self)
42
+ end
43
+
44
+ def inbound
45
+ @inbound ||= InterFAX::Inbound.new(self)
46
+ end
47
+
48
+ def get path, params = {}, valid_keys = {}
49
+ uri = uri_for(path, params, valid_keys)
50
+ request = Net::HTTP::Get.new(uri.request_uri)
51
+ transmit(request)
52
+ end
53
+
54
+ def post path, params = {}, valid_keys = {}, headers = {}, body = nil
55
+ uri = uri_for(path, params, valid_keys)
56
+ request = Net::HTTP::Post.new(uri.request_uri)
57
+ headers.each do |key, value|
58
+ request[key] = value
59
+ end
60
+ request.body = body if body
61
+ transmit(request)
62
+ end
63
+
64
+ private
65
+
66
+ def uri_for(path, params = {}, keys = {})
67
+ params = validate(params, keys)
68
+ uri = URI("https://#{DOMAIN}#{path}")
69
+ uri.query = URI.encode_www_form(params)
70
+ uri
71
+ end
72
+
73
+ def validate params = {}, keys = {}
74
+ params.each do |key, value|
75
+ if !keys.include? key.to_sym
76
+ raise ArgumentError.new("Unexpected argument: #{key} - please make to use camelCase arguments")
77
+ end
78
+ end
79
+ params
80
+ end
81
+
82
+ def transmit(request)
83
+ request["User-Agent"] = USER_AGENT
84
+ request.basic_auth username, password
85
+ parse(http.request(request))
86
+ end
87
+
88
+ def parse response
89
+ case response
90
+ when Net::HTTPSuccess
91
+ if response['location']
92
+ response['location']
93
+ elsif json?(response)
94
+ begin
95
+ JSON.parse(response.body)
96
+ rescue JSON::ParserError
97
+ response.body
98
+ end
99
+ else
100
+ response.body
101
+ end
102
+ when Net::HTTPNotFound
103
+ raise NotFoundError, "Record not found: #{response.body}"
104
+ when Net::HTTPBadRequest
105
+ raise BadRequestError, "Bad request (400): #{response.body}"
106
+ when Net::HTTPUnauthorized
107
+ raise UnauthorizedError, "Access Denied (401): #{response.body}"
108
+ else
109
+ if json?(response)
110
+ raise ServerError, "HTTP #{response.code}: #{JSON.parse(response.body)}"
111
+ else
112
+ raise ServerError, "HTTP #{response.code}: #{response.body}"
113
+ end
114
+ end
115
+ end
116
+
117
+ def json?(response)
118
+ content_type = response['Content-Type']
119
+ json_header = content_type && content_type.split(';').first == 'text/json'
120
+ has_body = response.body && response.body.length > 0
121
+ json_header && has_body
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,31 @@
1
+ class InterFAX::File
2
+ attr_accessor :header, :body
3
+
4
+ def initialize location, options = {}
5
+ if options[:mime_type]
6
+ initialize_binary(location, options[:mime_type])
7
+ elsif location.start_with?('http://') || location.start_with?('https://')
8
+ initialize_url(location)
9
+ else
10
+ initialize_path(location)
11
+ end
12
+ end
13
+
14
+ def initialize_binary(data, mime_type)
15
+ self.header = "Content-Type: #{mime_type}"
16
+ self.body = data
17
+ end
18
+
19
+ def initialize_url(url)
20
+ self.header = "Content-Location: #{url}"
21
+ self.body = nil
22
+ end
23
+
24
+ def initialize_path(path)
25
+ file = File.open(path)
26
+ mime_type = MimeMagic.by_magic(file)
27
+
28
+ self.header = "Content-Type: #{mime_type}"
29
+ self.body = File.open(path, 'rb').read
30
+ end
31
+ end
@@ -0,0 +1,2 @@
1
+ class InterFAX::ForwardingEmail < InterFAX::Object
2
+ end
@@ -0,0 +1,15 @@
1
+ class InterFAX::Image < InterFAX::Object
2
+ def inspect
3
+ _data = data
4
+ self.data = "#{data[0..20]}..."
5
+ result = super
6
+ self.data = _data
7
+ result
8
+ end
9
+
10
+ def save filename
11
+ File.open(filename, 'wb') do |file|
12
+ file.write(data)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,45 @@
1
+ class InterFAX::Inbound
2
+ def initialize client
3
+ @client = client
4
+ end
5
+
6
+ def all params = {}
7
+ valid_keys = [:unreadOnly, :limit, :lastId, :allUsers]
8
+ @client.get('/inbound/faxes', params, valid_keys).map do |fax|
9
+ fax[:client] = @client
10
+ InterFAX::Inbound::Fax.new(fax)
11
+ end
12
+ end
13
+
14
+ def find id
15
+ fax = @client.get("/inbound/faxes/#{id}")
16
+ fax[:client] = @client
17
+ InterFAX::Inbound::Fax.new(fax)
18
+ end
19
+
20
+ def image fax_id
21
+ data = @client.get("/inbound/faxes/#{fax_id}/image")
22
+ InterFAX::Image.new(data: data, client: @client)
23
+ end
24
+
25
+ def mark fax_id, options = {}
26
+ read = options.fetch(:read, true)
27
+ valid_keys = [:unread]
28
+ @client.post("/inbound/faxes/#{fax_id}/mark", {unread: !read}, valid_keys)
29
+ true
30
+ end
31
+
32
+ def resend fax_id, options = {}
33
+ options = options.delete_if {|k,v| k != :email }
34
+ valid_keys = [:email]
35
+ @client.post("/inbound/faxes/#{fax_id}/resend", options, valid_keys)
36
+ true
37
+ end
38
+
39
+ def emails fax_id
40
+ @client.get("/inbound/faxes/#{fax_id}/emails").map do |email|
41
+ email[:client] = @client
42
+ InterFAX::ForwardingEmail.new(email)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,21 @@
1
+ class InterFAX::Inbound::Fax < InterFAX::Object
2
+ def image
3
+ client.inbound.image(messageId)
4
+ end
5
+
6
+ def reload
7
+ client.inbound.find(messageId)
8
+ end
9
+
10
+ def mark status = true
11
+ client.inbound.mark(messageId, read: status)
12
+ end
13
+
14
+ def resend email = nil
15
+ client.inbound.resend(messageId, email: email)
16
+ end
17
+
18
+ def emails
19
+ client.inbound.emails(messageId)
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ class InterFAX::Object < OpenStruct
2
+ def attributes
3
+ hash = to_h
4
+ hash.delete(:client)
5
+ hash
6
+ end
7
+
8
+ def inspect
9
+ _client = client
10
+ self.delete_field('client')
11
+ result = super
12
+ self.client = _client
13
+ result
14
+ end
15
+ end
@@ -0,0 +1,58 @@
1
+ class InterFAX::Outbound
2
+
3
+ attr_writer :delivery
4
+
5
+ def initialize client
6
+ @client = client
7
+ end
8
+
9
+ def delivery
10
+ @delivery ||= InterFAX::Outbound::Delivery.new(@client)
11
+ end
12
+
13
+ def deliver params = {}
14
+ delivery.deliver(params)
15
+ end
16
+
17
+ def all params = {}
18
+ valid_keys = [:limit, :lastId, :sortOrder, :userId]
19
+ @client.get('/outbound/faxes', params, valid_keys).map do |fax|
20
+ fax[:client] = @client
21
+ InterFAX::Outbound::Fax.new(fax)
22
+ end
23
+ end
24
+
25
+ def completed *ids
26
+ params = { ids: [ids].flatten }
27
+ valid_keys = [:ids]
28
+ @client.get('/outbound/faxes/completed', params, valid_keys).map do |fax|
29
+ fax[:client] = @client
30
+ InterFAX::Outbound::Fax.new(fax)
31
+ end
32
+ end
33
+
34
+ def find id
35
+ fax = @client.get("/outbound/faxes/#{id}")
36
+ fax[:client] = @client
37
+ InterFAX::Outbound::Fax.new(fax)
38
+ end
39
+
40
+ def image fax_id
41
+ data = @client.get("/outbound/faxes/#{fax_id}/image")
42
+ InterFAX::Image.new(data: data, client: @client)
43
+ end
44
+
45
+ def cancel fax_id
46
+ fax = @client.get("/outbound/faxes/#{fax_id}/cancel")
47
+ fax[:client] = @client
48
+ InterFAX::Outbound::Fax.new(fax)
49
+ end
50
+
51
+ def search params = {}
52
+ valid_keys = [:ids, :reference, :dateFrom, :dateTo, :status, :userId, :faxNumber, :limit, :offset]
53
+ @client.get('/outbound/search', params, valid_keys).map do |fax|
54
+ fax[:client] = @client
55
+ InterFAX::Outbound::Fax.new(fax)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,52 @@
1
+ class InterFAX::Outbound::Delivery
2
+ attr_accessor :client, :params, :files, :fax_number
3
+
4
+ VALID_KEYS = [:faxNumber, :contact, :postponeTime, :retriesToPerform, :csid, :pageHeader, :reference, :replyAddress, :pageSize, :fitToPage, :pageOrientation, :resolution, :rendering]
5
+ BOUNDARY = "43e578690a6d14bf1d776cd55e7d7e29"
6
+ HEADERS = {
7
+ "Content-Type" => "multipart/mixed; boundary=#{BOUNDARY}"
8
+ }.freeze
9
+
10
+ def initialize client
11
+ self.client = client
12
+ end
13
+
14
+ def deliver params
15
+ params, files = validate_params(params)
16
+
17
+ file_objects = generate_file_objects(files)
18
+ body = body_for(file_objects)
19
+
20
+ result = client.post('/outbound/faxes', params, VALID_KEYS, HEADERS, body)
21
+ InterFAX::Outbound::Fax.new(client: client, id: result.split('/').last)
22
+ end
23
+
24
+ private
25
+
26
+ def validate_params params
27
+ self.fax_number = params[:faxNumber] || raise(ArgumentError.new('Missing argument: faxNumber'))
28
+ files = [params[:file] || params[:files] || raise(ArgumentError.new('Missing argument: file or files'))].flatten
29
+
30
+ params.delete(:fax_number)
31
+ params.delete(:file)
32
+ params.delete(:files)
33
+
34
+ [params, files]
35
+ end
36
+
37
+ def generate_file_objects files
38
+ files.map do |file|
39
+ if file.kind_of?(String)
40
+ InterFAX::File.new(file)
41
+ elsif file.kind_of?(InterFAX::File)
42
+ file
43
+ end
44
+ end
45
+ end
46
+
47
+ def body_for(files)
48
+ files.map do |file|
49
+ "--#{BOUNDARY}\r\n#{file.header}\r\n\r\n#{file.body}\r\n"
50
+ end.join + "--#{BOUNDARY}\r\n"
51
+ end
52
+ end
@@ -0,0 +1,13 @@
1
+ class InterFAX::Outbound::Fax < InterFAX::Object
2
+ def image
3
+ client.outbound.image(id)
4
+ end
5
+
6
+ def cancel
7
+ client.outbound.cancel(id)
8
+ end
9
+
10
+ def reload
11
+ client.outbound.find(id)
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module InterFAX
2
+ VERSION = '1.0.0.beta.1'
3
+ end
metadata CHANGED
@@ -1,94 +1,132 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: interfax
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 2
8
- - 1
9
- version: 0.2.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.beta.1
10
5
  platform: ruby
11
- authors:
12
- - Sascha Brink
6
+ authors:
7
+ - InterFAX
8
+ - Cristiano Betta
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-05-04 00:00:00 +02:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: rspec
12
+ date: 2016-08-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mimemagic
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 0.3.1
21
+ type: :runtime
22
22
  prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 0.3.1
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: fakefs
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: webmock
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
26
60
  - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 1
30
- - 2
31
- - 9
32
- version: 1.2.9
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
33
63
  type: :development
34
- version_requirements: *id001
35
- description: A ruby wrapper for the interfax webservice (SOAP)
36
- email: sascha.brink@gmail.com
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: minitest
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: A wrapper around the InterFAX REST API for sending and receiving faxes.
85
+ email:
86
+ - dev@interfax.net
87
+ - cbetta@gmail.com
37
88
  executables: []
38
-
39
89
  extensions: []
40
-
41
- extra_rdoc_files:
90
+ extra_rdoc_files: []
91
+ files:
42
92
  - LICENSE
43
- - README.markdown
44
- files:
45
- - LICENSE
46
- - README.markdown
47
- - Rakefile
48
- - VERSION
93
+ - README.md
94
+ - interfax.gemspec
49
95
  - lib/interfax.rb
50
- - lib/interfax/base.rb
51
- - lib/interfax/fax_item.rb
52
- - lib/interfax/incoming.rb
53
- - spec/incoming_helper.rb
54
- - spec/incoming_spec.rb
55
- - spec/interfax_spec.rb
56
- - spec/spec.opts
57
- - spec/spec_helper.rb
58
- has_rdoc: true
59
- homepage: http://github.com/sbrink/interfax
60
- licenses: []
61
-
96
+ - lib/interfax/account.rb
97
+ - lib/interfax/client.rb
98
+ - lib/interfax/file.rb
99
+ - lib/interfax/forwarding_email.rb
100
+ - lib/interfax/image.rb
101
+ - lib/interfax/inbound.rb
102
+ - lib/interfax/inbound/fax.rb
103
+ - lib/interfax/object.rb
104
+ - lib/interfax/outbound.rb
105
+ - lib/interfax/outbound/delivery.rb
106
+ - lib/interfax/outbound/fax.rb
107
+ - lib/interfax/version.rb
108
+ homepage: https://github.com/interfax/interfax-ruby
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
62
112
  post_install_message:
63
113
  rdoc_options: []
64
-
65
- require_paths:
114
+ require_paths:
66
115
  - lib
67
- required_ruby_version: !ruby/object:Gem::Requirement
68
- none: false
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- segments:
73
- - 0
74
- version: "0"
75
- required_rubygems_version: !ruby/object:Gem::Requirement
76
- none: false
77
- requirements:
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
78
118
  - - ">="
79
- - !ruby/object:Gem::Version
80
- segments:
81
- - 0
82
- version: "0"
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">"
124
+ - !ruby/object:Gem::Version
125
+ version: 1.3.1
83
126
  requirements: []
84
-
85
127
  rubyforge_project:
86
- rubygems_version: 1.3.7
128
+ rubygems_version: 2.5.1
87
129
  signing_key:
88
- specification_version: 3
89
- summary: A ruby wrapper for the interfax webservice (SOAP)
90
- test_files:
91
- - spec/incoming_helper.rb
92
- - spec/incoming_spec.rb
93
- - spec/interfax_spec.rb
94
- - spec/spec_helper.rb
130
+ specification_version: 4
131
+ summary: Send and receive faxes with InterFAX
132
+ test_files: []