lib/signaturit_client.rb in signaturit-sdk-1.0.1 vs lib/signaturit_client.rb in signaturit-sdk-1.1.0
- old
+ new
@@ -9,11 +9,11 @@
# Initialize the object with the token and environment
def initialize(token, production = false)
base = production ? 'https://api.signaturit.com' : 'https://api.sandbox.signaturit.com'
- @client = RestClient::Resource.new base, :headers => { :Authorization => "Bearer #{token}", :user_agent => 'signaturit-ruby-sdk 1.0.0' }, :ssl_version => :TLSv1_2
+ @client = RestClient::Resource.new base, :headers => { :Authorization => "Bearer #{token}", :user_agent => 'signaturit-ruby-sdk 1.1.0' }, :ssl_version => :TLSv1_2
end
# Get a concrete signature object
#
# Params:
@@ -75,10 +75,18 @@
# +params+:: An array of parameters for the signature object
def create_signature(filepath, recipients, params = {})
params[:recipients] = {}
[recipients].flatten.each_with_index do |recipient, index|
+ # if only email is given, transform it to a object
+ recipient = { email: recipient, name: recipient } if recipient.is_a? String
+
+ # workaround for a bug in rest_client not dealing with objects inside an array
+ if recipient[:require_signature_in_coordinates]
+ recipient[:require_signature_in_coordinates] = array2hash recipient[:require_signature_in_coordinates]
+ end
+
params[:recipients][index] = recipient
end
params[:files] = [filepath].flatten.map do |filepath|
File.new(filepath, 'rb')
@@ -124,11 +132,11 @@
# +params+:: An array of parameters for the branding object
def create_branding(params)
request :post, "/v3/brandings.json", params
end
- # Update a existing branding
+ # Update an existing branding
#
# Params:
# +branding_id+:: Id of the branding to update
# +params+:: Same params as method create_branding, see above
def update_branding(branding_id, params)
@@ -205,17 +213,387 @@
end
# Get the audit trail of concrete certificate
#
# Params:
- # +email_id++:: The id of the signature object
- # +certificate_id++:: The id of the document object
+ # +email_id++:: The id of the email object
+ # +certificate_id++:: The id of the certificate object
def download_email_audit_trail(email_id, certificate_id)
request :get, "/v3/emails/#{email_id}/certificates/#{certificate_id}/download/audit_trail", {}, false
end
+ # Get all SMS
+ #
+ # Params:
+ # +limit+:: Maximum number of results to return
+ # +offset+:: Offset of results to skip
+ # +conditions+:: Query conditions
+ def get_sms(limit = 100, offset = 0, conditions = {})
+ params = extract_query_params conditions
+
+ params['limit'] = limit
+ params['offset'] = offset
+
+ request :get, "/v3/sms.json", params
+ end
+
+ # Count all SMS
+ #
+ # Params:
+ # +conditions+:: Query conditions
+ def count_sms(conditions = {})
+ params = extract_query_params conditions
+
+ request :get, "/v3/sms/count.json", params
+ end
+
+ # Get a single SMS
+ #
+ # Params:
+ # +sms_id+:: Id of SMS
+ def get_single_sms(sms_id)
+ request :get, "/v3/sms/#{sms_id}.json"
+ end
+
+ # Create a new SMS
+ #
+ # Params:
+ # +files+:: File or files to send with the SMS
+ # +recipients+:: Recipients for the SMS
+ # +body+:: SMS body
+ # +params+:: Extra params
+ def create_sms(files, recipients, body, params = {})
+ params[:recipients] = {}
+
+ [recipients].flatten.each_with_index do |recipient, index|
+ params[:recipients][index] = recipient
+ end
+
+ if files
+ params[:attachments] = [files].flatten.map do |filepath|
+ File.new(filepath, 'rb')
+ end
+ end
+
+ params[:body] = body
+
+ request :post, "/v3/sms.json", params
+ end
+
+ # Get the audit trail of concrete certificate
+ #
+ # Params:
+ # +sms_id++:: The id of the SMS object
+ # +certificate_id++:: The id of the certificate object
+ def download_sms_audit_trail(sms_id, certificate_id)
+ request :get, "/v3/sms/#{sms_id}/certificates/#{certificate_id}/download/audit_trail", {}, false
+ end
+
+ # Get all subscription
+ #
+ # Params:
+ # +limit+:: Maximum number of results to return
+ # +offset+:: Offset of results to skip
+ # +conditions+:: Query conditions
+ def get_subscriptions(limit = 100, offset = 0, conditions = {})
+ params = extract_query_params conditions
+
+ params['limit'] = limit
+ params['offset'] = offset
+
+ request :get, "/v3/subscriptions.json", params
+ end
+
+ # Count all subscription
+ #
+ # Params:
+ # +conditions+:: Query conditions
+ def count_subscriptions(conditions = {})
+ params = extract_query_params conditions
+
+ request :get, "/v3/subscriptions/count.json", params
+ end
+
+ # Get a single subscription
+ #
+ # Params:
+ # +subscription_id+:: Id of subscription
+ def get_subscription(subscription_id)
+ request :get, "/v3/subscriptions/#{subscription_id}.json"
+ end
+
+ # Create a new subscription
+ #
+ # Params:
+ # +url+:: A url where to send the events.
+ # +events+:: The list of events to subscribe.
+ def create_subscription(url, events)
+ params = { url: url, events: events }
+
+ request :post, "/v3/subscriptions.json", params
+ end
+
+ # Update an existing subscription
+ #
+ # Params:
+ # +subscription_id+:: Id of the subscription to update
+ # +params+:: Same params as method create_subscription, see above
+ def update_subscription(subscription_id, params)
+ request :patch, "/v3/subscriptions/#{subscription_id}.json", params
+ end
+
+ # Delete an existing subscription
+ #
+ # Params:
+ # +subscription_id+:: Id of the subscription to update
+ def delete_subscription(subscription_id)
+ request :delete, "/v3/subscriptions/#{subscription_id}.json"
+ end
+
+ # Get all contacts
+ #
+ # Params:
+ # +limit+:: Maximum number of results to return
+ # +offset+:: Offset of results to skip
+ # +conditions+:: Query conditions
+ def get_contacts(limit = 100, offset = 0, conditions = {})
+ params = extract_query_params conditions
+
+ params['limit'] = limit
+ params['offset'] = offset
+
+ request :get, "/v3/contacts.json", params
+ end
+
+ # Count all contacts
+ #
+ # Params:
+ # +conditions+:: Query conditions
+ def count_contacts(conditions = {})
+ params = extract_query_params conditions
+
+ request :get, "/v3/contacts/count.json", params
+ end
+
+ # Get a single contact
+ #
+ # Params:
+ # +contact_id+:: Id of contact
+ def get_contact(contact_id)
+ request :get, "/v3/contacts/#{contact_id}.json"
+ end
+
+ # Create a new contact
+ #
+ # Params:
+ # +email+:: Contact email
+ # +name+:: Contact name
+ def create_contact(email, name)
+ params = { email: email, name: name }
+
+ request :post, "/v3/contacts.json", params
+ end
+
+ # Update an existing contact
+ #
+ # Params:
+ # +contact_id+:: Id of the contact to update
+ # +params+:: Same params as method create_contact, see above
+ def update_contact(contact_id, params)
+ request :patch, "/v3/contacts/#{contact_id}.json", params
+ end
+
+ # Delete an existing contact
+ #
+ # Params:
+ # +contact_id+:: Id of the contact to update
+ def delete_contact(contact_id)
+ request :delete, "/v3/contacts/#{contact_id}.json"
+ end
+
+ # Get all users
+ #
+ # Params:
+ # +limit+:: Maximum number of results to return
+ # +offset+:: Offset of results to skip
+ # +conditions+:: Query conditions
+ def get_users(limit = 100, offset = 0, conditions = {})
+ params = extract_query_params conditions
+
+ params['limit'] = limit
+ params['offset'] = offset
+
+ request :get, "/v3/team/users.json", params
+ end
+
+ # Get a single user
+ #
+ # Params:
+ # +user_id+:: Id of user
+ def get_user(user_id)
+ request :get, "/v3/team/users/#{user_id}.json"
+ end
+
+ # Invites a user to the team
+ #
+ # Params:
+ # +email+:: User email
+ # +role+:: User role
+ def invite_user(email, role)
+ params = { email: email, role: role }
+
+ request :post, "/v3/team/users.json", params
+ end
+
+ # Update an existing user role
+ #
+ # Params:
+ # +user_id+:: Id of the user to update
+ # +role+:: The new role
+ def change_user_role(user_id, role)
+ params = { role: role }
+
+ request :patch, "/v3/team/users/#{user_id}.json", params
+ end
+
+ # Delete an existing user from the team
+ #
+ # Params:
+ # +user_id+:: Id of the user to remove
+ def remove_user(user_id)
+ request :delete, "/v3/team/users/#{user_id}.json"
+ end
+
+ # Get all groups
+ #
+ # Params:
+ # +limit+:: Maximum number of results to return
+ # +offset+:: Offset of results to skip
+ # +conditions+:: Query conditions
+ def get_groups(limit = 100, offset = 0, conditions = {})
+ params = extract_query_params conditions
+
+ params['limit'] = limit
+ params['offset'] = offset
+
+ request :get, "/v3/team/groups.json", params
+ end
+
+ # Get a single group
+ #
+ # Params:
+ # +group_id+:: Id of group
+ def get_group(group_id)
+ request :get, "/v3/team/groups/#{group_id}.json"
+ end
+
+ # Create a new group
+ #
+ # Params:
+ # +name+:: Group name
+ def create_group(name)
+ params = { name: name }
+
+ request :post, "/v3/team/groups.json", params
+ end
+
+ # Update an existing group
+ #
+ # Params:
+ # +group_id+:: Id of the group to update
+ # +params+:: Same params as method create_group, see above
+ def update_group(group_id, name)
+ params = { name: name }
+
+ request :patch, "/v3/team/groups/#{group_id}.json", params
+ end
+
+ # Delete an existing group
+ #
+ # Params:
+ # +group_id+:: Id of the group to delete
+ def delete_group(group_id)
+ request :delete, "/v3/team/groups/#{group_id}.json"
+ end
+
+ # Add a new manager to the group
+ #
+ # Params:
+ # +group_id+:: Id of the group where to add the user
+ # +user_id+:: Id of the user to add
+ def add_manager_to_group(group_id, user_id)
+ request :post, "/v3/team/groups/#{group_id}/managers/#{user_id}.json"
+ end
+
+ # Remove a manager from the group
+ #
+ # Params:
+ # +group_id+:: Id of the group where to add the user
+ # +user_id+:: Id of the user to add
+ def remove_manager_from_group(group_id, user_id)
+ request :delete, "/v3/team/groups/#{group_id}/managers/#{user_id}.json"
+ end
+
+ # Add a new member to the group
+ #
+ # Params:
+ # +group_id+:: Id of the group where to add the user
+ # +user_id+:: Id of the user to add
+ def add_member_to_group(group_id, user_id)
+ request :post, "/v3/team/groups/#{group_id}/members/#{user_id}.json"
+ end
+
+ # Remove a member from the group
+ #
+ # Params:
+ # +group_id+:: Id of the group where to add the user
+ # +user_id+:: Id of the user to add
+ def remove_member_from_group(group_id, user_id)
+ request :delete, "/v3/team/groups/#{group_id}/members/#{user_id}.json"
+ end
+
+ # Get all seats
+ #
+ # Params:
+ # +limit+:: Maximum number of results to return
+ # +offset+:: Offset of results to skip
+ # +conditions+:: Query conditions
+ def get_seats(limit = 100, offset = 0, conditions = {})
+ params = extract_query_params conditions
+
+ params['limit'] = limit
+ params['offset'] = offset
+
+ request :get, "/v3/team/seats.json", params
+ end
+
+ # Remove a seat
+ #
+ # Params:
+ # +seat_id+:: Id of the seat to remove
+ def remove_seat(seat_id)
+ request :delete, "/v3/team/seats/#{seat_id}.json"
+ end
+
# PRIVATE METHODS FROM HERE
private
+
+ # convert array to hash recursively
+ def array2hash(array)
+ unless array.is_a?(Array)
+ return array
+ end
+
+ Hash[
+ array.map.with_index do |x, i|
+ if x.is_a?(Array)
+ x = array2hash(x)
+ end
+
+ [i, x]
+ end
+ ]
+ end
# Parse query parameters
def extract_query_params(conditions)
params = {}