lib/universign.rb in universign-0.2.0 vs lib/universign.rb in universign-0.2.1
- old
+ new
@@ -1,21 +1,22 @@
# encoding: utf-8
require 'universign/version'
+require 'universign/hash'
require 'base64'
require 'xmlrpc/client'
module Universign
-
class << self
attr_accessor :configuration
end
def self.configure
self.configuration ||= Configuration.new
yield(configuration) if block_given?
end
+ # Configuration class
class Configuration
attr_accessor :language, :production, :user, :password, :debug,
:profile
def initialize
@@ -25,81 +26,84 @@
@profile = 'default'
end
end
module Sign
-
class << self
+ SignerDefaultOptions = { firstname: nil,
+ lastname: nil,
+ organization: nil,
+ profile: 'default',
+ emailAddress: nil,
+ phoneNum: nil,
+ role: 'signer',
+ signatureField: nil,
+ successURL: nil,
+ cancelURL: nil,
+ failURL: nil,
+ certificateType: 'simple',
+ idDocuments: nil
+ }.freeze
+ SANDBOX_URL = 'sign.test.cryptolog.com'.freeze
+ PROD_URL = 'sign.cryptolog.com'.freeze
def client
- raise 'You need to set config options' if Universign.configuration.nil?
- host = Universign.configuration.production ? 'sign.cryptolog.com' : 'sign.test.cryptolog.com'
+ fail 'You need to set config options' if Universign.configuration.nil?
+ host = Universign.configuration.production ? PROD_URL : SANDBOX_URL
path = '/sign/rpc'
client = Universign::Sign::Client.new(
- host, path, nil, nil, nil, Universign.configuration.user, Universign.configuration.password, true
+ host, path, nil, nil, nil,
+ Universign.configuration.user,
+ Universign.configuration.password, true
)
client.set_debug if Universign.configuration.debug
client
end
- end
+ def transaction_signer(options = {})
+ options = options.reverse_merge(SignerDefaultOptions)
+ validate_transaction_signer_argument options
+ options.reject! { |_, v| v.nil? }
+ end
- Document = Struct.new(:content, :name)
- Signer = Struct.new(
- :phoneNum,
- :emailAddress,
- :firstname,
- :lastname,
- :successURL,
- :failURL,
- :cancelURL
- )
+ def transaction_document(content, name)
+ { content: XMLRPC::Base64.new(content), name: name }
+ end
- def self.transactionSigner(phoneNum, emailAddress, firstname, lastname, successURL, failURL, cancelURL)
- Signer.new(phoneNum, emailAddress, firstname, lastname, successURL, failURL, cancelURL)
- end
+ private
- def self.transactionDocument(content, name)
- Document.new(XMLRPC::Base64.new(content), name)
+ def validate_transaction_signer_argument(options)
+ fail(ArgumentError, 'You have to provide a firstname') if
+ options[:firstname].empty?
+ fail(ArgumentError, 'You have to provide a lastname') if
+ options[:lastname].empty?
+ fail(ArgumentError, 'You have to provide an email') if
+ options[:emailAddress].empty?
+ end
end
class Client < XMLRPC::Client
-
- RequestTransaction = Struct.new(
- :documents,
- :signers,
- :handwrittenSignatureMode,
- :profile,
- :certificateType,
- :language
- )
-
# Request signature (Client side)
- def requestTransaction(transactionSigners, transactionDocuments)
- transactionSigners = [transactionSigners] unless transactionSigners.is_a? Array
- transactionDocuments = [transactionDocuments] unless transactionDocuments.is_a? Array
- request = RequestTransaction.new(
- transactionDocuments,
- transactionSigners,
- 0,
- Universign.configuration.profile,
- 'simple',
- Universign.configuration.language
- )
+ def request_transaction(signers, docs)
+ signers = [signers] unless signers.is_a? Array
+ docs = [docs] unless docs.is_a? Array
+ request = { documents: docs, signers: signers,
+ handwrittenSignatureMode: 0,
+ profile: Universign.configuration.profile,
+ language: Universign.configuration.language }
call('requester.requestTransaction', request)
end
- def getTransactionInfo(transactionId)
- call('requester.getTransactionInfo', transactionId)
+ def get_transaction_info(transaction_id)
+ call('requester.getTransactionInfo', transaction_id)
end
- def getDocuments(transactionId)
- call('requester.getDocuments', transactionId)
+ def get_documents(transaction_id)
+ call('requester.getDocuments', transaction_id)
end
def set_debug
- @http.set_debug_output($stderr);
+ @http.set_debug_output($stderr)
end
-
end
end
end