mifiel 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4771294e3b399b292c103b30308f6803e8ec8c7
4
- data.tar.gz: 4cc235a00d7d16efb58d900670f45a628c58b008
3
+ metadata.gz: 62a53025404e6eae0ad14166a1af2926695d4fae
4
+ data.tar.gz: f6ba1a0bd02be7b8211e6b404009942fb7f56e7d
5
5
  SHA512:
6
- metadata.gz: 2f9460d1b14fb42270b49eb8a6edc945563dc0505a7ec8902f75e868253b1713632bbaec715cf74eb8eca422d3cb015527f15cec36716bbb62fef52eb9622137
7
- data.tar.gz: ee57285f4b61a68110d254fb5baba7c8ebdeef5e0cbc149fbdaa7801b67d1f3f680de6a414f282f66299137bca3314706081897f25aa5ddc92f6370846f40e9f
6
+ metadata.gz: 1108db05b0ea2babce2d13fd32d9bc5ea4c6339cebbdee2db7b0a81b1bc42fe0f261390c0b6bfc39f17634741164fe2f2322444619003d54a43034421204d5a5
7
+ data.tar.gz: b614aba324ae18a85aa60551d7c428fdb0d234ca97351a445598469a703e5b3df26680a76cebf87785aaa1181ad07d4475f843fe5de4b82134dd9ff9a1b621c5
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ rubocop.html
@@ -1,7 +1,7 @@
1
- Style/IndentationConsistency:
2
- Enabled: false
1
+ AllCops:
2
+ TargetRubyVersion: 2.1
3
3
 
4
- Style/SingleSpaceBeforeFirstArg:
4
+ Style/IndentationConsistency:
5
5
  Enabled: false
6
6
 
7
7
  Style/AlignHash:
@@ -17,4 +17,8 @@ Style/EmptyLinesAroundBlockBody:
17
17
  Enabled: false
18
18
 
19
19
  Metrics/LineLength:
20
- Max: 1000
20
+ Max: 190
21
+
22
+ Style/HashSyntax:
23
+ EnforcedStyle: ruby19
24
+
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ cache: bundler
3
+ sudo: false
4
+ rvm:
5
+ - 2.1
6
+ - 2.2
7
+ - 2.3.0
8
+ notifications:
9
+ email:
10
+ on_success: change
11
+ on_failure: always
12
+ script:
13
+ - bundle exec rubocop
14
+ - bundle exec rspec spec
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Mifiel
2
2
 
3
+ [![Gem Version][gem-version-image]][gem-version-url]
4
+ [![Build Status][travis-image]][travis-url]
5
+ [![security][security-image]][security-url]
6
+
3
7
  Ruby SDK for [Mifiel](https://www.mifiel.com) API.
4
8
  Please read our [documentation](https://www.mifiel.com/api-docs/) for instructions on how to start using the API.
5
9
 
@@ -21,6 +25,19 @@ Or install it yourself as:
21
25
 
22
26
  ## Usage
23
27
 
28
+ To start using the API you will need an APP_ID and a APP_SECRET which will be provided upon request (contact us at hola@mifiel.com).
29
+
30
+ You will first need to create an account in [mifiel.com](https://www.mifiel.com) since the APP_ID and APP_SECRET will be linked to your account.
31
+
32
+ Then you can configure the gem with:
33
+
34
+ ```ruby
35
+ Mifiel.config do |config|
36
+ config.app_id = '<APP_ID>'
37
+ config.app_secret = '<APP_SECRET>'
38
+ end
39
+ ```
40
+
24
41
  Document methods:
25
42
 
26
43
  - Find:
@@ -115,3 +132,11 @@ Certificate methods:
115
132
  3. Commit your changes (`git commit -am 'Add some feature'`)
116
133
  4. Push to the branch (`git push origin my-new-feature`)
117
134
  5. Create a new Pull Request
135
+
136
+
137
+ [gem-version-image]: https://badge.fury.io/rb/mifiel.svg
138
+ [gem-version-url]: https://badge.fury.io/rb/mifiel
139
+ [security-url]: https://hakiri.io/github/Mifiel/ruby-api-client/master
140
+ [security-image]: https://hakiri.io/github/Mifiel/ruby-api-client/master.svg
141
+ [travis-image]: https://travis-ci.org/Mifiel/ruby-api-client.svg?branch=master
142
+ [travis-url]: https://travis-ci.org/Mifiel/ruby-api-client
@@ -7,7 +7,7 @@ module Mifiel
7
7
  autoload :Certificate, 'mifiel/certificate'
8
8
  autoload :Config, 'mifiel/config'
9
9
 
10
- BASE_URL = 'https://www.mifiel.com/api/v1'
10
+ BASE_URL = 'https://www.mifiel.com/api/v1'.freeze
11
11
 
12
12
  def self.config
13
13
  if block_given?
@@ -16,5 +16,4 @@ module Mifiel
16
16
  Mifiel::Config
17
17
  end
18
18
  end
19
-
20
19
  end
@@ -1,16 +1,17 @@
1
+ require 'rest-client'
2
+
1
3
  module Mifiel
2
4
  class Base < ActiveRestClient::Base
3
5
  after_request :rescue_errors
4
6
 
5
- def rescue_errors(name, response)
7
+ def rescue_errors(_name, response)
6
8
  if response.status == 400 # bad request
7
9
  result = JSON.load(response.body)
8
10
  message = result['errors'] || [result['error']]
9
- raise BadRequestError, message.to_a.join(', ')
10
- elsif (500..599).include?(response.status)
11
- raise ServerError, "Server could not process your request: status #{response.status}"
11
+ fail BadRequestError, message.to_a.join(', ')
12
+ elsif (500..599).cover?(response.status)
13
+ fail ServerError, "Could not process your request: status #{response.status}"
12
14
  end
13
15
  end
14
-
15
16
  end
16
17
  end
@@ -1,24 +1,21 @@
1
1
  module Mifiel
2
2
  class Certificate < Mifiel::Base
3
-
4
3
  get :all, '/keys'
5
4
  get :find, '/keys/:id'
6
5
  put :save, '/keys/:id'
7
6
  get :sat, '/keys/sat'
8
7
 
9
- def self.create(file:)
8
+ def self.create(cer_file)
10
9
  rest_request = RestClient::Request.new(
11
10
  url: "#{Mifiel.config.base_url}/keys",
12
11
  method: :post,
13
12
  payload: {
14
- cer_file: File.new(file)
13
+ cer_file: File.new(cer_file)
15
14
  },
16
15
  ssl_version: 'SSLv23'
17
16
  )
18
- response = ApiAuth.sign!(rest_request, Mifiel.config.app_id, Mifiel.config.app_secret).execute
19
- JSON.load(response)
17
+ req = ApiAuth.sign!(rest_request, Mifiel.config.app_id, Mifiel.config.app_secret)
18
+ JSON.load(req.execute)
20
19
  end
21
-
22
20
  end
23
-
24
21
  end
@@ -1,9 +1,7 @@
1
- require 'rest-client'
2
1
  require 'open3'
3
2
 
4
3
  module Mifiel
5
4
  class Document < Mifiel::Base
6
-
7
5
  get :all, '/documents'
8
6
  get :find, '/documents/:id'
9
7
  put :save, '/documents/:id'
@@ -11,7 +9,7 @@ module Mifiel
11
9
 
12
10
  def self.create(file:, signatories:, hash:nil)
13
11
  sgries = {}
14
- signatories.each_with_index {|s, i| sgries[i] = s}
12
+ signatories.each_with_index { |s, i| sgries[i] = s }
15
13
  rest_request = RestClient::Request.new(
16
14
  url: "#{Mifiel.config.base_url}/documents",
17
15
  method: :post,
@@ -22,8 +20,8 @@ module Mifiel
22
20
  },
23
21
  ssl_version: 'SSLv23'
24
22
  )
25
- response = ApiAuth.sign!(rest_request, Mifiel.config.app_id, Mifiel.config.app_secret).execute
26
- JSON.load(response)
23
+ req = ApiAuth.sign!(rest_request, Mifiel.config.app_id, Mifiel.config.app_secret)
24
+ JSON.load(req.execute)
27
25
  end
28
26
 
29
27
  def sign(certificate_id:nil, certificate:nil)
@@ -32,7 +30,13 @@ module Mifiel
32
30
  fail NoSignatureError, 'You must first call build_signature or provide a signature' unless signature
33
31
  params = { signature: signature }
34
32
  params[:key] = certificate_id if certificate_id
35
- params[:certificate] = certificate.unpack('H*')[0] if certificate
33
+ if certificate
34
+ params[:certificate] = if certificate.encoding.to_s == 'UTF-8'
35
+ certificate.unpack('H*')[0]
36
+ else
37
+ certificate
38
+ end
39
+ end
36
40
 
37
41
  Mifiel::Document._request("#{Mifiel.config.base_url}/documents/#{id}/sign", :post, params)
38
42
  rescue ActiveRestClient::HTTPClientException => e
@@ -42,6 +46,17 @@ module Mifiel
42
46
  raise MifielError, 'Server could not process request'
43
47
  end
44
48
 
49
+ def request_signature(email, cc:nil)
50
+ params = { email: email }
51
+ params[:cc] = cc if cc.is_a?(Array)
52
+ Mifiel::Document._request("#{Mifiel.config.base_url}/documents/#{id}/request_signature", :post, params)
53
+ rescue ActiveRestClient::HTTPClientException => e
54
+ message = e.result.errors || [e.result.error]
55
+ raise MifielError, message.to_a.join(', ')
56
+ rescue ActiveRestClient::HTTPServerException
57
+ raise MifielError, 'Server could not process request'
58
+ end
59
+
45
60
  def build_signature(private_key, private_key_pass)
46
61
  self.signature ||= sign_hash(private_key, private_key_pass, original_hash)
47
62
  end
@@ -57,7 +72,7 @@ module Mifiel
57
72
  def sign_hash(key, pass, hash)
58
73
  private_key = build_private_key(key, pass)
59
74
  unless private_key.private?
60
- raise NotPrivateKeyError, "The private key is not valid"
75
+ fail NotPrivateKeyError, 'The private key is not valid'
61
76
  end
62
77
  signature = private_key.sign(OpenSSL::Digest::SHA256.new, hash)
63
78
  signature.unpack('H*')[0]
@@ -74,11 +89,9 @@ module Mifiel
74
89
 
75
90
  # delete file from file system
76
91
  File.unlink private_file.path
77
- fail PrivateKeyError.new("#{error}, #{status}") unless error.empty?
92
+ fail PrivateKeyError, "#{error}, #{status}" unless error.empty?
78
93
 
79
94
  OpenSSL::PKey::RSA.new priv_pem_s
80
95
  end
81
-
82
96
  end
83
-
84
97
  end
@@ -1,3 +1,3 @@
1
1
  module Mifiel
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'.freeze
3
3
  end
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Mifiel::VERSION
9
9
  spec.authors = ['Genaro Madrid']
10
10
  spec.email = ['genmadrid@gmail.com']
11
- spec.summary = %q{Ruby SDK for mifiel.com.}
11
+ spec.summary = 'Ruby SDK for mifiel.com.'
12
12
  spec.homepage = 'https://github.com/Mifiel/ruby-api-client'
13
13
  spec.license = 'MIT'
14
14
 
@@ -17,16 +17,18 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ['lib']
19
19
 
20
- spec.add_runtime_dependency 'rest-client', '~> 1.7'
21
- spec.add_runtime_dependency 'json', '~> 1.8'
22
- spec.add_runtime_dependency 'api-auth', '~> 1.4'
20
+ spec.add_runtime_dependency 'rest-client', '~> 1.7'
21
+ spec.add_runtime_dependency 'json', '~> 1.8'
22
+ spec.add_runtime_dependency 'api-auth', '~> 1.4'
23
23
  spec.add_runtime_dependency 'active_rest_client', '~> 1.2'
24
24
 
25
- spec.add_development_dependency 'bundler', '~> 1.6'
26
- spec.add_development_dependency 'rake', '~> 10.0'
27
- spec.add_development_dependency 'rspec', '~> 3.1', '>= 3.1.7'
28
- spec.add_development_dependency 'pry', '~> 0.10', '>= 0.10.1'
29
- spec.add_development_dependency 'pry-byebug', '~> 3.3', '>= 3.3.0'
30
- spec.add_development_dependency 'bump', '~> 0.5', '>= 0.5.3'
31
- spec.add_development_dependency 'webmock', '~> 1.22', '>= 1.22.2'
25
+ spec.add_development_dependency 'bundler', '~> 1.6'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.1', '>= 3.1.7'
28
+ spec.add_development_dependency 'pry', '~> 0.10', '>= 0.10.1'
29
+ spec.add_development_dependency 'pry-byebug', '~> 3.3', '>= 3.3.0'
30
+ spec.add_development_dependency 'bump', '~> 0.5', '>= 0.5.3'
31
+ spec.add_development_dependency 'webmock', '~> 1.22', '>= 1.22.2'
32
+ spec.add_development_dependency 'sinatra', '~> 1.4', '>= 1.4.7'
33
+ spec.add_development_dependency 'rubocop', '~> 0.36'
32
34
  end
@@ -7,12 +7,12 @@ describe Mifiel::Document do
7
7
  context 'with a document' do
8
8
  let(:document) do
9
9
  Mifiel::Document.create(
10
- file: 'spec/fixtures/example.pdf',
11
- signatories: [
12
- { name: 'Signer 1', email: 'signer1@email.com', tax_id: 'AAA010101AAA' },
13
- { name: 'Signer 2', email: 'signer2@email.com', tax_id: 'AAA010102AAA' }
14
- ]
15
- )
10
+ file: 'spec/fixtures/example.pdf',
11
+ signatories: [
12
+ { name: 'Signer 1', email: 'signer1@email.com', tax_id: 'AAA010101AAA' },
13
+ { name: 'Signer 2', email: 'signer2@email.com', tax_id: 'AAA010102AAA' }
14
+ ]
15
+ )
16
16
  end
17
17
 
18
18
  it { expect(document).to be_a(Hash) }
@@ -22,31 +22,49 @@ describe Mifiel::Document do
22
22
  describe '#sign' do
23
23
  let!(:signature) { 'signature' }
24
24
  let!(:document) { Mifiel::Document.all.first }
25
+ let!(:certificate_id) { SecureRandom.uuid }
25
26
 
26
27
  context 'without build_signature first called' do
27
28
  it do
28
- expect{document.sign(certificate_id: 3)}.to raise_error(Mifiel::NoSignatureError)
29
+ expect do
30
+ document.sign(certificate_id: certificate_id)
31
+ end.to raise_error(Mifiel::NoSignatureError)
29
32
  end
30
33
  end
31
34
 
32
35
  context 'with build_signature called before' do
33
36
  it do
34
37
  document.build_signature(private_key, private_key_pass)
35
- expect{document.sign(certificate_id: 3)}.not_to raise_error
38
+ expect do
39
+ document.sign(certificate_id: certificate_id)
40
+ end.not_to raise_error
36
41
  end
37
42
  end
38
43
 
39
44
  context 'with signature' do
40
45
  it do
41
46
  document.signature = signature
42
- expect{document.sign(certificate_id: 3)}.not_to raise_error
47
+ expect do
48
+ document.sign(certificate_id: certificate_id)
49
+ end.not_to raise_error
43
50
  end
44
51
  end
45
52
 
46
53
  context 'with certificate' do
47
54
  it do
48
55
  document.build_signature(private_key, private_key_pass)
49
- expect{document.sign(certificate: certificate)}.not_to raise_error
56
+ expect do
57
+ document.sign(certificate: certificate)
58
+ end.not_to raise_error
59
+ end
60
+ end
61
+
62
+ context 'with certificate in hex' do
63
+ it do
64
+ document.build_signature(private_key, private_key_pass)
65
+ expect do
66
+ document.sign(certificate: certificate.unpack('H*')[0])
67
+ end.not_to raise_error
50
68
  end
51
69
  end
52
70
  end
@@ -56,20 +74,26 @@ describe Mifiel::Document do
56
74
 
57
75
  context 'with a wrong private_key' do
58
76
  it do
59
- expect{document.build_signature('asd', private_key_pass)}.to raise_error(Mifiel::PrivateKeyError)
77
+ expect do
78
+ document.build_signature('asd', private_key_pass)
79
+ end.to raise_error(Mifiel::PrivateKeyError)
60
80
  end
61
81
  end
62
82
 
63
83
  context 'with a private_key that is not a private_key' do
64
84
  xit do
65
85
  cer = OpenSSL::X509::Certificate.new(certificate)
66
- expect{document.build_signature(cer.public_key.to_der, private_key_pass)}.to raise_error(Mifiel::NotPrivateKeyError)
86
+ expect do
87
+ document.build_signature(cer.public_key.to_der, private_key_pass)
88
+ end.to raise_error(Mifiel::NotPrivateKeyError)
67
89
  end
68
90
  end
69
91
 
70
92
  context 'with a wrong password' do
71
93
  it do
72
- expect{document.build_signature('asd', private_key_pass)}.to raise_error(Mifiel::PrivateKeyError)
94
+ expect do
95
+ document.build_signature('asd', private_key_pass)
96
+ end.to raise_error(Mifiel::PrivateKeyError)
73
97
  end
74
98
  end
75
99
  end
@@ -2,14 +2,20 @@ require 'pry'
2
2
  require 'byebug'
3
3
  require 'pry-byebug'
4
4
  require 'mifiel'
5
- # require 'webmock/rspec'
5
+ require 'webmock/rspec'
6
6
 
7
7
  Dir['./spec/support/**/*.rb'].each { |f| require f }
8
8
 
9
- # WebMock.disable_net_connect!
9
+ RSpec.configure do |config|
10
10
 
11
- Mifiel.config do |config|
12
- config.app_id = '138b94faa5a43d35f60208449f0e0916de29575e'
13
- config.app_secret = 'mJM3igFcJp8Iu/euEtr0ZTAGixmE+4K5R9o2TEOQEQfAiEuB7MpVwgMULoDxhJvbIUCDZ5l2nnxgL93I5JBXEw=='
14
- config.base_url = 'http://localhost:3000/api/v1'
11
+ config.before(:suite) do
12
+ Mifiel.config do |conf|
13
+ conf.app_id = 'APP_ID'
14
+ conf.app_secret = 'APP_SECRET'
15
+ end
16
+ end
17
+
18
+ config.before(:each) do
19
+ stub_request(:any, /mifiel.com/).to_rack(FakeMifiel)
20
+ end
15
21
  end
@@ -0,0 +1,68 @@
1
+ require 'sinatra/base'
2
+
3
+ class FakeMifiel < Sinatra::Base
4
+ get '/api/v1/documents' do
5
+ content_type :json
6
+ status 200
7
+ [
8
+ document,
9
+ document
10
+ ].to_json
11
+ end
12
+
13
+ post '/api/v1/documents' do
14
+ content_type :json
15
+ status 200
16
+ document(
17
+ id: params[:id],
18
+ original_hash: Digest::SHA256.hexdigest(params[:file][:tempfile].read),
19
+ file_file_name: params[:file][:filename],
20
+ signed: false,
21
+ signed_at: nil
22
+ ).to_json
23
+ end
24
+
25
+ post '/api/v1/documents/:id/sign' do
26
+ content_type :json
27
+ status 200
28
+ document(
29
+ id: params[:id]
30
+ ).to_json
31
+ end
32
+
33
+ private
34
+
35
+ def document(args={})
36
+ id = args[:id] || SecureRandom.uuid
37
+ {
38
+ id: id,
39
+ original_hash: Digest::SHA256.hexdigest(id),
40
+ file_file_name: 'test-pdf.pdf',
41
+ signed_by_all: true,
42
+ signed: true,
43
+ signed_at: Time.now.utc.iso8601,
44
+ status: [1, 'Firmado'],
45
+ owner: {
46
+ email: 'signer1@email.com',
47
+ name: 'Jorge Morales'
48
+ },
49
+ file: "/api/v1/documents/#{id}/file",
50
+ file_download: "/api/v1/documents/#{id}/file?download=true",
51
+ file_signed: "/api/v1/documents/#{id}/file_signed",
52
+ file_signed_download: "/api/v1/documents/#{id}/file_signed?download=true",
53
+ file_zipped: "/api/v1/documents/#{id}/zip",
54
+ signatures: [{
55
+ email: 'signer1@email.com',
56
+ signed: true,
57
+ signed_at: (Time.now.utc - 10_000).iso8601,
58
+ certificate_number: '20001000000200001410',
59
+ tax_id: 'AAA010101AAA',
60
+ signature: '77cd5156779c..4e276ef1056c1de11b7f70bed28',
61
+ user: {
62
+ email: 'signer1@email.com',
63
+ name: 'Jorge Morales'
64
+ }
65
+ }]
66
+ }
67
+ end
68
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mifiel
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genaro Madrid
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-09 00:00:00.000000000 Z
11
+ date: 2016-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -194,6 +194,40 @@ dependencies:
194
194
  - - ">="
195
195
  - !ruby/object:Gem::Version
196
196
  version: 1.22.2
197
+ - !ruby/object:Gem::Dependency
198
+ name: sinatra
199
+ requirement: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - "~>"
202
+ - !ruby/object:Gem::Version
203
+ version: '1.4'
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: 1.4.7
207
+ type: :development
208
+ prerelease: false
209
+ version_requirements: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - "~>"
212
+ - !ruby/object:Gem::Version
213
+ version: '1.4'
214
+ - - ">="
215
+ - !ruby/object:Gem::Version
216
+ version: 1.4.7
217
+ - !ruby/object:Gem::Dependency
218
+ name: rubocop
219
+ requirement: !ruby/object:Gem::Requirement
220
+ requirements:
221
+ - - "~>"
222
+ - !ruby/object:Gem::Version
223
+ version: '0.36'
224
+ type: :development
225
+ prerelease: false
226
+ version_requirements: !ruby/object:Gem::Requirement
227
+ requirements:
228
+ - - "~>"
229
+ - !ruby/object:Gem::Version
230
+ version: '0.36'
197
231
  description:
198
232
  email:
199
233
  - genmadrid@gmail.com
@@ -207,6 +241,7 @@ files:
207
241
  - ".rubocop.yml"
208
242
  - ".ruby-gemset"
209
243
  - ".ruby-version"
244
+ - ".travis.yml"
210
245
  - Gemfile
211
246
  - LICENSE.txt
212
247
  - README.md
@@ -225,6 +260,7 @@ files:
225
260
  - spec/fixtures/example.pdf
226
261
  - spec/mifiel/document_spec.rb
227
262
  - spec/spec_helper.rb
263
+ - spec/support/fake_mifiel.rb
228
264
  homepage: https://github.com/Mifiel/ruby-api-client
229
265
  licenses:
230
266
  - MIT
@@ -255,3 +291,4 @@ test_files:
255
291
  - spec/fixtures/example.pdf
256
292
  - spec/mifiel/document_spec.rb
257
293
  - spec/spec_helper.rb
294
+ - spec/support/fake_mifiel.rb