Skip to content

Commit

Permalink
Merge branch 'feature/templates' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
genaromadrid committed Nov 15, 2017
2 parents 233172a + eea4257 commit e25aebd
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/mifiel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/mifiel/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions lib/mifiel/template.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions mifiel.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
46 changes: 46 additions & 0 deletions spec/mifiel/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions spec/mifiel/template_spec.rb
Original file line number Diff line number Diff line change
@@ -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: '<div>Some <field name="name" type="string">NAME</field></div>',
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
41 changes: 41 additions & 0 deletions spec/support/fake_mifiel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,6 +101,14 @@ class FakeMifiel < Sinatra::Base

private

def template(args = {})
{
id: args[:id] || SecureRandom.uuid,
name: 'some-template',
content: '<div><field name="name">NAME</field></div>'
}
end

def key(args={})
id = args[:id] || SecureRandom.uuid
{
Expand Down

0 comments on commit e25aebd

Please sign in to comment.