Skip to content

Commit

Permalink
Refactor initializing client to parent class and before each controll…
Browse files Browse the repository at this point in the history
…er action
  • Loading branch information
jyoung488 committed Sep 12, 2017
1 parent ba55354 commit e688e02
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 28 deletions.
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

before_action :client

private

def client
client = ApplicationRecord.initiate_client
end
end
6 changes: 4 additions & 2 deletions app/controllers/apps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ class AppsController < ApplicationController
skip_before_action :verify_authenticity_token

def info
client = App.initiate_client

app = client.get_api_app :client_id => params[:client_id]

render json: app
end

def list
end

end
1 change: 0 additions & 1 deletion app/controllers/embeddeds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def template_draft
end

def edit_unclaimed_draft
client = Embedded.initiate_client
@draft = client.edit_and_resend_unclaimed_draft(
:signature_request_id => '596e1b9288c7c726c603fc3080689e6c89c1a397',
:client_id => ENV['CLIENT_ID'],
Expand Down
13 changes: 3 additions & 10 deletions app/controllers/signs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class SignsController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :client

def index

Expand All @@ -14,26 +15,21 @@ def index


def account
client = Sign.initiate_client
@account = client.get_account
end

def signature_request
client = Sign.initiate_client

request = client.get_signature_request :signature_request_id => params[:request_id]
render json: request
end

def all_requests
client = Sign.initiate_client
@signatures = client.get_signature_requests

render json: @signatures
end

def send_request
client = Sign.initiate_client
response = client.send_signature_request(
:test_mode => 1,
:title => 'Test Contract',
Expand Down Expand Up @@ -80,10 +76,11 @@ def send_request

data = JSON.parse(response.to_json, symbolize_names: true)
id = data[:raw_data][:signatures][0][:signature_id]

render json: response
end

def template_request
client = Sign.initiate_client
client.send_signature_request_with_template(
:test_mode => 1,
:template_id => 'feb796545f869222c6bd67a6eb276a7573700704',
Expand Down Expand Up @@ -111,14 +108,12 @@ def template_request
end

def reminder
client = Sign.initiate_client
client.remind_signature_request :signature_request_id => params[:signature_id], :email_address => params[:email]

redirect_to root_path
end

def update_email
client = Sign.initiate_client
client.update_signature_request(
:signature_request_id => '33e7a1d839e98797d633fb8b47247c4aa9a5936d',
:signature_id => '5064ca698bde9581ad75f6d62450eb4b',
Expand All @@ -127,14 +122,12 @@ def update_email
end

def cancel
client = Sign.initiate_client
client.cancel_signature_request :signature_request_id => params[:request_id]

redirect_to root_path
end

def file
client = Sign.initiate_client
file_bin = client.signature_request_files :signature_request_id => params[:request_id], :get_url => true
File.open("files.zip", "wb") do |file|
file.write(file_bin)
Expand Down
1 change: 0 additions & 1 deletion app/controllers/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class TemplatesController < ApplicationController
skip_before_action :verify_authenticity_token

def update
client = Template.initiate_client
@template = client.update_template_files :template_id =>'74cb63b17da0bc1541dbb6c4f6b2f10004ef0492', :file_url => 'http://che.org.il/wp-content/uploads/2016/12/pdf-sample.pdf', :message => 'updated using new endpoint!'
end
end
3 changes: 0 additions & 3 deletions app/models/app.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
class App < ApplicationRecord
def self.initiate_client
HelloSign::Client.new :api_key => ENV['HS_KEY']
end
end
4 changes: 4 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true

def self.initiate_client
HelloSign::Client.new :api_key => ENV['HS_KEY']
end
end
3 changes: 0 additions & 3 deletions app/models/embedded.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
class Embedded < ApplicationRecord
def self.initiate_client
HelloSign::Client.new :api_key => ENV['HS_KEY']
end
end
5 changes: 0 additions & 5 deletions app/models/sign.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
class Sign < ApplicationRecord

def self.initiate_client
HelloSign::Client.new :api_key => ENV['HS_KEY']
end

end
3 changes: 0 additions & 3 deletions app/models/template.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
class Template < ApplicationRecord
def self.initiate_client
HelloSign::Client.new :api_key => ENV['HS_KEY']
end
end
Empty file added app/views/apps/list.html.erb
Empty file.
3 changes: 3 additions & 0 deletions app/views/signs/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ Hello API event received
<%= submit_tag("Get API App") %>
<% end %>
</li>
<li>
<%= link_to 'List API Apps', apps_list_path %>
</li>
</ul>
</div>
<div align="center">
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@
get '/templates/update' => 'templates#update'

get '/apps/info' => 'apps#info'
get '/apps/list' => 'apps#list'
end

0 comments on commit e688e02

Please sign in to comment.