class Eversign::Client

Attributes

access_key[RW]
base_uri[RW]
business_id[RW]
token[RW]

Public Class Methods

new() click to toggle source
# File lib/eversign/client.rb, line 14
          def initialize()
                  self.base_uri = (Eversign.configuration && Eversign.configuration.api_base) ? Eversign.configuration.api_base : 'https://api.eversign.com'
                  access_key = Eversign.configuration.access_key
                  if access_key.start_with?('Bearer ')
    self.set_oauth_access_token(access_key)
else
                          self.access_key = access_key
                  end
                  self.business_id = Eversign.configuration.business_id
          end

Public Instance Methods

cancel_document(document_hash) click to toggle source
# File lib/eversign/client.rb, line 128
def cancel_document(document_hash)
        path = "/api/document?access_key=#{access_key}&business_id=#{business_id}&document_hash=#{document_hash}&cancel=1"
        delete(path, document_hash)
end
create_document(document) click to toggle source
# File lib/eversign/client.rb, line 103
          def create_document(document)
                  if document.files
                          for file in document.files
          if file.file_url
            file_response = self.upload_file(file.file_url)
            file.file_url = nil
            file.file_id = file_response.file_id
          end
        end
      end
         path = "/api/document?access_key=#{access_key}&business_id=#{business_id}"
data = Eversign::Mappings::Document.representation_for(document)
                  response = execute_request(:post, path, data)
                  extract_response(response.body, Eversign::Mappings::Document)
          end
create_document_from_template(template) click to toggle source
# File lib/eversign/client.rb, line 119
def create_document_from_template(template)
        create_document(template)
end
delete_document(document_hash) click to toggle source
# File lib/eversign/client.rb, line 123
 def delete_document(document_hash)
path = "/api/document?access_key=#{access_key}&business_id=#{business_id}&document_hash=#{document_hash}"
         delete(path, document_hash)
 end
download_final_document_to_path(document_hash, path, audit_trail=1) click to toggle source
# File lib/eversign/client.rb, line 138
def download_final_document_to_path(document_hash, path, audit_trail=1)
        sub_uri = "/api/download_raw_document?access_key=#{access_key}&business_id=#{business_id}&document_hash=#{document_hash}&audit_trail=#{audit_trail}"
        download(sub_uri, path)
end
download_raw_document_to_path(document_hash, path) click to toggle source
# File lib/eversign/client.rb, line 133
def download_raw_document_to_path(document_hash, path)
        sub_uri = "/api/download_raw_document?access_key=#{access_key}&business_id=#{business_id}&document_hash=#{document_hash}"
        download(sub_uri, path)
end
generate_oauth_authorization_url(options) click to toggle source
# File lib/eversign/client.rb, line 34
            def generate_oauth_authorization_url(options)
  check_arguments(['client_id', 'state'], options)
  template = Addressable::Template.new(Eversign.configuration.oauth_base + '/authorize{?clinet_id,state}')
  return template.partial_expand(clinet_id: options['client_id'], state: options['state']).pattern
end
get_action_required_documents() click to toggle source
# File lib/eversign/client.rb, line 77
 def get_action_required_documents
get_documents('my_action_required')
 end
get_all_documents() click to toggle source
# File lib/eversign/client.rb, line 61
 def get_all_documents
get_documents('all')
 end
get_archived_templates() click to toggle source
# File lib/eversign/client.rb, line 89
 def get_archived_templates
get_documents('templates_archived')
 end
get_buisnesses() click to toggle source
# File lib/eversign/client.rb, line 56
 def get_buisnesses
         response = execute_request(:get, "/api/business?access_key=#{access_key}")
extract_response(response.body, Eversign::Mappings::Business)
 end
get_cancelled_documents() click to toggle source
# File lib/eversign/client.rb, line 73
 def get_cancelled_documents
get_documents('cancelled')
 end
get_completed_documents() click to toggle source
# File lib/eversign/client.rb, line 65
 def get_completed_documents
get_documents('completed')
 end
get_document(document_hash) click to toggle source
# File lib/eversign/client.rb, line 97
 def get_document(document_hash)
path = "/api/document?access_key=#{access_key}&business_id=#{business_id}&document_hash=#{document_hash}"
response = execute_request(:get, path)
extract_response(response.body, Eversign::Mappings::Document)
 end
get_draft_documents() click to toggle source
# File lib/eversign/client.rb, line 69
 def get_draft_documents
get_documents('draft')
 end
get_draft_templates() click to toggle source
# File lib/eversign/client.rb, line 93
 def get_draft_templates
get_documents('template_draft')
 end
get_templates() click to toggle source
# File lib/eversign/client.rb, line 85
 def get_templates
get_documents('templates')
 end
get_waiting_for_others_documents() click to toggle source
# File lib/eversign/client.rb, line 81
 def get_waiting_for_others_documents
get_documents('waiting_for_others')
 end
request_oauth_token(options) click to toggle source
# File lib/eversign/client.rb, line 40
def request_oauth_token(options)
  check_arguments(['client_id', 'client_secret', 'code', 'state'], options)

  req = execute_request(:post, '/token', options)
  if req.status == 200
    response_obj = JSON.parse(req.body)

    if response_obj.key?('success')
      raise response_obj['message']
    else
      return response_obj['access_token']
    end
  end
  raise 'no success'
end
send_reminder_for_document(document_hash, signer_id) click to toggle source
# File lib/eversign/client.rb, line 150
def send_reminder_for_document(document_hash, signer_id)
        path = "/api/send_reminder?access_key=#{access_key}&business_id=#{business_id}"
        response = execute_request(:post, path, {document_hash: document_hash, signer_id: signer_id}.to_json)
        eval(response.body)[:success] ? true : extract_response(response.body)
end
set_oauth_access_token(oauthtoken) click to toggle source
# File lib/eversign/client.rb, line 25
          def set_oauth_access_token(oauthtoken)
                  if oauthtoken.startswith('Bearer ')
  self.token = oauthtoken
else
  self.token = 'Bearer ' + oauthtoken
end
self.get_businesses()
          end
upload_file(file_path) click to toggle source
# File lib/eversign/client.rb, line 143
def upload_file(file_path)
        payload = { upload: Faraday::UploadIO.new(file_path, 'text/plain') }
        path = "/api/file?access_key=#{access_key}&business_id=#{business_id}"
        response = execute_request(:post, path, payload, true)
        extract_response(response.body, Eversign::Mappings::File)
end

Private Instance Methods

check_arguments(arguments=[], options={}) click to toggle source
# File lib/eversign/client.rb, line 172
                  def check_arguments(arguments=[], options={})
  arguments.each do |argument|
    raise ('Please specify ' + argument) unless options.has_key?(argument.to_sym) 
  end
end
delete(path, document_hash) click to toggle source
# File lib/eversign/client.rb, line 178
def delete(path, document_hash)
        response = execute_request(:delete, path)
        eval(response.body)[:success] ? true : extract_response(response.body)
end
download(sub_uri, path) click to toggle source
# File lib/eversign/client.rb, line 183
def download(sub_uri, path)
        response = execute_request(:get, sub_uri)
        File.open(path, 'wb') { |fp| fp.write(response.body) }
end
execute_request(method, path, body=nil, multipart=false) click to toggle source
# File lib/eversign/client.rb, line 157
def execute_request(method, path, body=nil, multipart=false)
        @faraday ||= Faraday.new(base_uri) do |conn| 
          conn.headers = { }
          conn.headers['User-Agent'] = 'Eversign_Ruby_SDK'
          conn.headers['Authorization'] = token if token
          conn.request :multipart if multipart
          conn.adapter :net_http
        end

        @faraday.send(method) do |request|
                request.url path
                request.body = body if body
        end
end
extract_response(body, mapping=nil) click to toggle source
# File lib/eversign/client.rb, line 194
def extract_response(body, mapping=nil)
        data = JSON.parse(body)
        if data.kind_of?(Array)
                mapping.extract_collection(body, nil)
        else
                if data.key?('success')
                        Eversign::Mappings::Exception.extract_single(body, nil)
                else
                        mapping.extract_single(body, nil)
                end
        end
end
get_documents(doc_type) click to toggle source
# File lib/eversign/client.rb, line 188
  def get_documents(doc_type)
          path = "/api/document?access_key=#{access_key}&business_id=#{business_id}&type=#{doc_type}"
response = execute_request(:get, path)
extract_response(response.body, Eversign::Mappings::Document)
  end