sfax 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 74afcc0d5d5f12617755eb6a4f7e278ec822550e
4
+ data.tar.gz: 2f3847a6d32c964084bec36f96c856bf6791e909
5
+ SHA512:
6
+ metadata.gz: 0f9dfb22c0fa53fac0f78aef202835ccf7e4225d9eec7b5e00be899c0ce6123500ab5b5dfa2c16fb3bdf121eb53f126b60c687c01db1025cf6a860e031d6c4f2
7
+ data.tar.gz: c2abb34593bb8fbe8f2d301f3806581e5a4e1597435ae6062a7fff39b8f17a78891c7e223bc2b0b80833226844a08176caba100ac962fc353fa0f4c089b281bb
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format=documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sfax.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Derek Schneider
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # SFax API Client
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'sfax'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sfax
18
+
19
+ ## Usage
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ desc "Open an irb session preloaded with this library"
7
+ task :console do
8
+ sh "irb -rubygems -I lib -I"
9
+ end
data/lib/sfax.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'sfax/client'
2
+ require 'sfax/encryptor'
3
+ require 'sfax/version'
4
+ require 'faraday'
5
+ require 'json'
6
+
7
+ module SFax
8
+ end
@@ -0,0 +1,72 @@
1
+ require 'sfax/encryptor'
2
+ require 'sfax/version'
3
+ require 'sfax/constants'
4
+ require 'faraday'
5
+ require 'json'
6
+
7
+ module SFax
8
+ class Client
9
+ include Constants
10
+
11
+ def initialize(username:, api_key:, encryption_key:)
12
+ @username = username
13
+ @api_key = api_key
14
+ @encryptor = SFax::Encryptor.new(key: encryption_key)
15
+ end
16
+
17
+ def send_fax(fax_number:, file:, recipient_name:)
18
+ validate_fax_number fax_number
19
+
20
+ response = connection.post SEND_FAX_PATH do |request|
21
+ request.params = {
22
+ 'token' => generate_token,
23
+ 'ApiKey' => api_key,
24
+ 'RecipientFax' => fax_number,
25
+ 'RecipientName' => recipient_name,
26
+ }
27
+ request.body = {
28
+ file: Faraday::UploadIO.new(file, 'application/pdf', "#{gen_dt}.pdf")
29
+ }
30
+ end
31
+
32
+ response_object = JSON.parse(response.body)
33
+ # TODO: Refactor into proper response objec.
34
+ def response_object.fax_id
35
+ fetch('SendFaxQueueId')
36
+ end
37
+
38
+ response_object.tap do |o|
39
+ raise SendFaxError if o.fax_id == -1
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def connection
46
+ @connection ||= Faraday.new(url: SFAX_URL) do |faraday|
47
+ faraday.request :multipart
48
+ faraday.request :url_encoded
49
+ faraday.adapter Faraday.default_adapter
50
+ end
51
+ end
52
+
53
+ attr_reader :username, :api_key, :encryptor
54
+
55
+ def generate_token
56
+ encryptor.encrypt "Username=#{username}&ApiKey=#{api_key}&GenDT=#{gen_dt}"
57
+ end
58
+
59
+ def gen_dt
60
+ Time.now.utc.iso8601
61
+ end
62
+
63
+ def validate_fax_number(number)
64
+ if number.to_i == 0 || number.length != 11
65
+ raise InvalidFaxNumberError, "Expected an 11 digit fax number. Got: #{number}"
66
+ end
67
+ end
68
+
69
+ InvalidFaxNumberError = Class.new(StandardError)
70
+ SendFaxError = Class.new(StandardError)
71
+ end
72
+ end
@@ -0,0 +1,6 @@
1
+ module SFax
2
+ module Constants
3
+ SFAX_URL = 'https://api.sfaxme.com'.freeze
4
+ SEND_FAX_PATH = '/api/sendfax'.freeze
5
+ end
6
+ end
@@ -0,0 +1,34 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ module SFax
5
+ class Encryptor
6
+ DEFAULT_IV = 'x49e*wJVXr8BrALE'.freeze
7
+ OPEN_SSL_CIPHER = 'aes-256-cbc'.freeze
8
+
9
+ def initialize(key:, iv: DEFAULT_IV)
10
+ @key = key.dup
11
+ @iv = iv
12
+ end
13
+
14
+ attr_reader :key, :iv
15
+
16
+ def encrypt(plain)
17
+ cipher = new_encrypt_cipher
18
+
19
+ encrypted_data = cipher.update plain
20
+ encrypted_data << cipher.final
21
+
22
+ Base64.encode64 encrypted_data
23
+ end
24
+
25
+ def new_encrypt_cipher
26
+ OpenSSL::Cipher::Cipher.new(OPEN_SSL_CIPHER).tap do |c|
27
+ c.encrypt
28
+
29
+ c.key = key
30
+ c.iv = iv
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module SFax
2
+ VERSION = '0.1.1'
3
+ end
data/sfax.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sfax/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sfax'
8
+ spec.version = SFax::VERSION
9
+ spec.authors = ['Derek Schneider']
10
+ spec.email = ['dschneider.641@gmail.com']
11
+ spec.summary = %q{Ruby client for SFax API}
12
+ spec.description = %q{Ruby client for SFax API}
13
+ spec.homepage = 'https://github.com/schneiderderek/sfax'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'faraday', '~> 0.9'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.7'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.5'
26
+ spec.add_development_dependency 'webmock', '~> 2.1'
27
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe SFax::Client do
4
+ subject do
5
+ described_class.new(
6
+ username: username,
7
+ api_key: api_key,
8
+ encryption_key: encryption_key,
9
+ )
10
+ end
11
+ let(:username) { SecureRandom.hex }
12
+ let(:api_key) { SecureRandom.hex }
13
+ let(:encryption_key) { SecureRandom.hex }
14
+
15
+ describe '#send_fax' do
16
+ let(:fax_number) { nil }
17
+ let(:file) { StringIO.new('data') }
18
+ let(:recipient_name) { nil }
19
+ let(:token) { SecureRandom.hex }
20
+ let(:send_fax_queue_id) { rand(1_000_000) }
21
+ let!(:send_fax_request) do
22
+ stub_request(:post, 'https://api.sfaxme.com/api/sendfax').with(query: {
23
+ 'token' => token,
24
+ 'ApiKey' => api_key,
25
+ 'RecipientFax' => fax_number,
26
+ 'RecipientName' => recipient_name,
27
+ }).and_return(body: {
28
+ 'SendFaxQueueId' => send_fax_queue_id,
29
+ }.to_json)
30
+ end
31
+
32
+ before do
33
+ # I'd prefer not to stub this, but I think this is the easiest way
34
+ # currently.
35
+ allow(subject).to receive(:generate_token).and_return(token)
36
+ end
37
+
38
+ context 'valid fax number' do
39
+ let(:fax_number) { '15555555555' }
40
+
41
+ context 'valid send_fax_queue_id in response' do
42
+ let(:send_fax_queue_id) { rand(1_000_000) }
43
+
44
+ specify do
45
+ response = subject.send_fax(fax_number: fax_number, file: file, recipient_name: recipient_name)
46
+
47
+ expect(response.fax_id).to eq send_fax_queue_id
48
+ expect(send_fax_request).to have_been_requested
49
+ end
50
+ end
51
+
52
+ context 'send_fax_queue_id is -1' do
53
+ let(:send_fax_queue_id) { -1 }
54
+
55
+ specify do
56
+ expect {
57
+ subject.send_fax(fax_number: fax_number, file: file, recipient_name: recipient_name)
58
+ }.to raise_error(described_class::SendFaxError)
59
+
60
+ expect(send_fax_request).to have_been_requested
61
+ end
62
+ end
63
+ end
64
+
65
+ shared_examples :invdalid_fax_number_error do
66
+ specify do
67
+ expect {
68
+ subject.send_fax(fax_number: fax_number, file: file, recipient_name: recipient_name)
69
+ }.to raise_error(described_class::InvalidFaxNumberError)
70
+ end
71
+ end
72
+
73
+ context 'fax number 10 digits' do
74
+ let(:fax_number) { '5555555555' }
75
+
76
+ include_examples :invdalid_fax_number_error
77
+ end
78
+
79
+ context 'fax number not a number' do
80
+ let(:fax_number) { 'a' }
81
+
82
+ include_examples :invdalid_fax_number_error
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe SFax::Encryptor do
4
+ let(:key) { SecureRandom.hex }
5
+ subject do
6
+ described_class.new(key: key)
7
+ end
8
+
9
+ describe '#encrypt' do
10
+ specify do
11
+ expect { subject.encrypt('a') }.not_to raise_error
12
+ end
13
+
14
+ specify do
15
+ expect(subject.encrypt('a')).to be_a String
16
+ end
17
+ end
18
+ end
data/spec/sfax_spec.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe SFax do
4
+ end
@@ -0,0 +1,2 @@
1
+ require 'sfax'
2
+ require 'webmock/rspec'
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sfax
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Derek Schneider
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.1'
83
+ description: Ruby client for SFax API
84
+ email:
85
+ - dschneider.641@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/sfax.rb
97
+ - lib/sfax/client.rb
98
+ - lib/sfax/constants.rb
99
+ - lib/sfax/encryptor.rb
100
+ - lib/sfax/version.rb
101
+ - sfax.gemspec
102
+ - spec/sfax_client_spec.rb
103
+ - spec/sfax_encryptor_spec.rb
104
+ - spec/sfax_spec.rb
105
+ - spec/spec_helper.rb
106
+ homepage: https://github.com/schneiderderek/sfax
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.5.1
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Ruby client for SFax API
130
+ test_files:
131
+ - spec/sfax_client_spec.rb
132
+ - spec/sfax_encryptor_spec.rb
133
+ - spec/sfax_spec.rb
134
+ - spec/spec_helper.rb