diff --git a/lib/mifiel.rb b/lib/mifiel.rb index 202ed47..fd24ef0 100644 --- a/lib/mifiel.rb +++ b/lib/mifiel.rb @@ -5,6 +5,7 @@ module Mifiel autoload :Base, 'mifiel/base' autoload :Document, 'mifiel/document' autoload :Certificate, 'mifiel/certificate' + autoload :Template, 'mifiel/template' autoload :Config, 'mifiel/config' BASE_URL = 'https://www.mifiel.com/api/v1'.freeze diff --git a/lib/mifiel/document.rb b/lib/mifiel/document.rb index 22b5bdd..12385f3 100644 --- a/lib/mifiel/document.rb +++ b/lib/mifiel/document.rb @@ -7,6 +7,8 @@ class Document < Mifiel::Base get :find, '/documents/:id' put :save, '/documents/:id' delete :delete, '/documents/:id' + post :create_from_template, '/templates/:template_id/generate_document', timeout: 60 + post :create_many_from_template, '/templates/:template_id/generate_documents', timeout: 60 # rubocop:disable Metrics/MethodLength, Metrics/AbcSize def self.create(args) diff --git a/lib/mifiel/template.rb b/lib/mifiel/template.rb new file mode 100644 index 0000000..cee6fff --- /dev/null +++ b/lib/mifiel/template.rb @@ -0,0 +1,22 @@ +module Mifiel + class Template < Mifiel::Base + get :all, '/templates' + get :find, '/templates/:id' + put :save, '/templates/:id' + post :create, '/templates' + delete :delete, '/templates/:id' + + def generate_document(args = {}) + Mifiel::Document.create_from_template(args.merge(template_id: id)) + end + + def generate_documents(callback_url:, identifier: nil, documents:) + Mifiel::Document.create_many_from_template( + template_id: template_id, + identifier: identifier, + documents: documents, + callback_url: callback_url + ) + end + end +end diff --git a/lib/mifiel/version.rb b/lib/mifiel/version.rb index c343e15..fd466ee 100644 --- a/lib/mifiel/version.rb +++ b/lib/mifiel/version.rb @@ -1,3 +1,3 @@ module Mifiel - VERSION = '1.1.5'.freeze + VERSION = '1.2.0'.freeze end diff --git a/mifiel.gemspec b/mifiel.gemspec index 5e277c8..48c8536 100644 --- a/mifiel.gemspec +++ b/mifiel.gemspec @@ -19,8 +19,8 @@ Gem::Specification.new do |spec| spec.require_paths = ['lib'] spec.required_ruby_version = '~> 2.1' - spec.add_runtime_dependency 'rest-client', '>= 1.7' - spec.add_runtime_dependency 'json', '> 0' + spec.add_runtime_dependency 'rest-client', '>= 1.8' + spec.add_runtime_dependency 'json', '~> 1.8' spec.add_runtime_dependency 'api-auth', '~> 1.4' # Use Gem::Version to parse the Ruby version for reliable comparison # ActiveSupport 5+ requires Ruby 2.2.2 @@ -34,11 +34,11 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'bundler', '~> 1.6' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rspec', '~> 3.1', '>= 3.1.7' - spec.add_development_dependency 'pry', '~> 0.10', '>= 0.10.1' - spec.add_development_dependency 'pry-byebug', '~> 3.3', '>= 3.3.0' + spec.add_development_dependency 'byebug', '~> 9.0', '< 9.0.6' + spec.add_development_dependency 'pry-byebug', '~> 3.4', '>= 3.3.0' spec.add_development_dependency 'bump', '~> 0.5', '>= 0.5.3' spec.add_development_dependency 'webmock', '~> 1.22', '>= 1.22.2' spec.add_development_dependency 'sinatra', '~> 1.4', '>= 1.4.7' spec.add_development_dependency 'rubocop', '0.47.1' - spec.add_development_dependency 'simplecov', '~> 0.11' + spec.add_development_dependency 'simplecov', '~> 0.15' end diff --git a/spec/mifiel/document_spec.rb b/spec/mifiel/document_spec.rb index 6b36919..2ebb44a 100644 --- a/spec/mifiel/document_spec.rb +++ b/spec/mifiel/document_spec.rb @@ -17,6 +17,52 @@ it { expect(document).to be_a(Mifiel::Document) } end + + context 'from template' do + let!(:template_id) { 'c6c29866-7fd6-4f77-9ecd-eae8bc3a772a' } + let!(:document) do + Mifiel::Document.create_from_template( + template_id: template_id, + fields: { + name: 'some' + }, + signatories: [{ + name: 'Signer', + email: 'signer@email.com' + }, { + name: 'Signer', + email: 'signer@email.com' + }] + ) + end + + it { expect(document).to be_a Mifiel::Document } + end + + context 'many from template' do + let!(:template_id) { 'c6c29866-7fd6-4f77-9ecd-eae8bc3a772a' } + let!(:documents) do + Mifiel::Document.create_many_from_template( + template_id: template_id, + callback_url: 'http://some-callback.url/mifiel', + identifier: 'name', + documents: [{ + fields: { + name: 'Some Name' + }, + signatories: [{ + name: 'Signer', + email: 'signer@email.com' + }, { + name: 'Signer', + email: 'signer@email.com' + }] + }] + ) + end + + it { expect(documents.status).to eq 'success' } + end end describe 'working with a document' do diff --git a/spec/mifiel/template_spec.rb b/spec/mifiel/template_spec.rb new file mode 100644 index 0000000..3ae5c0e --- /dev/null +++ b/spec/mifiel/template_spec.rb @@ -0,0 +1,36 @@ +describe Mifiel::Template do + let!(:template_id) { 'bb7d65b4-47f1-470e-85e4-22ddae86f9ea' } + + shared_examples 'a valid template' do + it { expect(template).to be_a(Mifiel::Template) } + it { expect(template).to respond_to(:id) } + it { expect(template).to respond_to(:name) } + end + + describe '#create' do + let!(:template) do + Mifiel::Template.create( + name: 'Some Template', + content: '
Some NAME
', + header: 'Genaros Header', + footer: 'Genaros Footer' + ) + end + it_behaves_like 'a valid template' + end + + describe '#find' do + let!(:template) { Mifiel::Template.find(template_id) } + + it_behaves_like 'a valid template' + it { expect(template.id).to eq(template_id) } + end + + describe '#all' do + it 'should respond with many templates' do + templates = Mifiel::Template.all + expect(templates.count).to be > 0 + expect(templates.first).to be_a(Mifiel::Template) + end + end +end diff --git a/spec/support/fake_mifiel.rb b/spec/support/fake_mifiel.rb index 118c32e..7e71f35 100644 --- a/spec/support/fake_mifiel.rb +++ b/spec/support/fake_mifiel.rb @@ -10,6 +10,39 @@ class FakeMifiel < Sinatra::Base ].to_json end + get '/api/v1/templates' do + content_type :json + status 200 + [ + template, + template + ].to_json + end + + get '/api/v1/templates/:id' do + content_type :json + status 200 + template(id: params[:id]).to_json + end + + post '/api/v1/templates' do + content_type :json + status 200 + template.to_json + end + + post '/api/v1/templates/:id/generate_documents' do + content_type :json + status 200 + { status: :success }.to_json + end + + post '/api/v1/templates/:id/generate_document' do + content_type :json + status 200 + document(id: params[:id]).to_json + end + post '/api/v1/keys' do content_type :json status 200 @@ -68,6 +101,14 @@ class FakeMifiel < Sinatra::Base private + def template(args = {}) + { + id: args[:id] || SecureRandom.uuid, + name: 'some-template', + content: '
NAME
' + } + end + def key(args={}) id = args[:id] || SecureRandom.uuid {