class Docusigner::Base

Public Class Methods

act_as_user(user) { || ... } click to toggle source
# File lib/docusigner/base.rb, line 39
def act_as_user(user, &block)
  old_header = headers.delete('X-DocuSign-Act-As-User')
  headers['X-DocuSign-Act-As-User'] = user
  yield
  if old_header.nil?
    headers.delete('X-DocuSign-Act-As-User')
  else
    headers['X-DocuSign-Act-As-User'] = old_header
  end
end
authorization=(options = {}) click to toggle source
# File lib/docusigner/base.rb, line 35
def authorization=(options = {})
  headers['X-DocuSign-Authentication'] = "<DocuSignCredentials><Username>%{username}</Username><Password>%{password}</Password><IntegratorKey>%{integrator_key}</IntegratorKey></DocuSignCredentials>" % options
end
connection(refresh = false) click to toggle source
# File lib/docusigner/base.rb, line 20
def connection(refresh = false)
  if defined?(@connection) || self == Docusigner::Base
    @connection = Docusigner::Connection.new(site, format) if refresh || @connection.nil? || !@connection.is_a?(Docusigner::Connection)
    @connection.timeout = timeout if timeout
    @connection.ssl_options = ssl_options if ssl_options
    @connection
  else
    superclass.connection
  end
end
headers() click to toggle source

we want to inherit headers for authentication

# File lib/docusigner/base.rb, line 14
def headers
  @headers ||= begin
    superclass.respond_to?(:headers) ? superclass.headers.dup : {}
  end
end
token=(token) click to toggle source
# File lib/docusigner/base.rb, line 31
def token=(token)
  headers['Authorization'] = "Bearer #{token}"
end

Private Class Methods

instantiate_collection(data, prefix_options = {}) click to toggle source

some of DocuSign’s responses contain metadata about the response (e.g. number of records returned)

Calls superclass method
# File lib/docusigner/base.rb, line 66
def instantiate_collection(data, prefix_options = {})
  if data.is_a?(Hash)
    # if the data has the collection name as a root element, use that to build the records
    if data.has_key?(collection_name)
      super(data[collection_name], prefix_options) 
    else
      instantiate_flattened_collection(data, prefix_options)
    end
  else
    super(data, prefix_options)
  end
end
instantiate_flattened_collection(data, prefix_options) click to toggle source
# File lib/docusigner/base.rb, line 79
def instantiate_flattened_collection(data, prefix_options)
  flattened = []
  data.each do |type, array|
    array.each do |obj|
      flattened << instantiate_record(obj.merge(:type => type), prefix_options)
    end if array.is_a?(Array)
  end
  flattened
end
instantiate_record(record, prefix_options = {}) click to toggle source

we want to automatically set the foreign keys if they are provided

Calls superclass method
# File lib/docusigner/base.rb, line 53
def instantiate_record(record, prefix_options = {})
  super(record, prefix_options).tap do |r|
    prefix_options.each do |k, v|
      if r.respond_to?("#{k}=")
        r.send("#{k}=", v)
      end
    end
    # set the id field from [element_name]Id (e.g. userId)
    r.id = r.send("#{element_name}Id") if r.respond_to?("#{element_name}Id")
  end
end

Public Instance Methods

to_json(opts = {}) click to toggle source

the json should skip the root element

# File lib/docusigner/base.rb, line 91
def to_json(opts = {})
  as_json.to_json
end

Protected Instance Methods

create_resource_for(resource_name) click to toggle source

we want any generated resources for generated models to extend from this base class

mainly because we don't want the include the root in the json representation
# File lib/docusigner/base.rb, line 99
def create_resource_for(resource_name)
  resource = self.class.const_set(resource_name, Class.new(Docusigner::Base))
  resource.prefix = self.class.prefix
  resource.site = self.class.site
  resource
end