signnow-ruby 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,45 @@
1
+ module Signnow
2
+ class Base
3
+ include Signnow::Operations::All
4
+ include Signnow::Operations::Create
5
+ include Signnow::Operations::Find
6
+
7
+ attr_accessor :created
8
+
9
+ # Initializes the object using the given attributes
10
+ #
11
+ # @param [Hash] attributes The attributes to use for initialization
12
+ def initialize(attributes = {})
13
+ set_attributes(attributes)
14
+ parse_timestamps
15
+ end
16
+
17
+ # Model validations
18
+ #
19
+ # @return [Boolean]
20
+ def valid?
21
+ self.errors.empty?
22
+ end
23
+
24
+ # Accesor for the errors
25
+ #
26
+ def errors
27
+ @errors || []
28
+ end
29
+
30
+ # Sets the attributes
31
+ #
32
+ # @param [Hash] attributes The attributes to initialize
33
+ def set_attributes(attributes)
34
+ attributes.each_pair do |key, value|
35
+ instance_variable_set("@#{key}", value)
36
+ end
37
+ end
38
+
39
+ # Parses UNIX timestamps and creates Time objects.
40
+ def parse_timestamps
41
+ @created = created.to_i if created.is_a? String
42
+ @created = Time.at(created) if created
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,29 @@
1
+ module Signnow
2
+ class Client
3
+ attr_reader :access_token
4
+
5
+ # Creates an account object using the given Signnow user.
6
+ # existing account.
7
+ #
8
+ # @param [String] User access token to use the signnow api.
9
+ def initialize(access_token=nil)
10
+ @access_token = access_token
11
+ self
12
+ end
13
+
14
+ # Executes block with the access token
15
+ #
16
+ # @param [block] block to execute
17
+ #
18
+ # @example [description]
19
+ # client = Signnow::Client.new('_user_auth_')
20
+ # client.perform! do |access_token|
21
+ # Signnow::User.show(access_token: access_token)
22
+ # end
23
+ def perform!(&block)
24
+ raise Signnow::AuthenticationError unless access_token
25
+ block.call(access_token)
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ module Signnow
2
+ class Document < Base
3
+ include Signnow::Operations::Show
4
+ include Signnow::Operations::All
5
+ include Signnow::Operations::DownloadLink
6
+
7
+ attr_accessor :id, :user_id, :document_name, :page_count, :created,
8
+ :updated, :original_filename, :thumbnail, :signatures, :seals, :texts,
9
+ :inserts, :tags, :fields, :requests, :notary_invites, :version_time, :pages
10
+
11
+ # Parses UNIX timestamps and creates Time objects.
12
+ def parse_timestamps
13
+ super
14
+ @updated = updated.to_i if updated.is_a? String
15
+ @updated = Time.at(updated) if updated
16
+ end
17
+
18
+ class << self
19
+ # Redefining the documents api endpoint
20
+ #
21
+ def api_all_url
22
+ "user/documentsv2"
23
+ end
24
+ protected :api_all_url
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,51 @@
1
+ module Signnow
2
+ module Operations
3
+ module All
4
+ module ClassMethods
5
+ # Retrieves all available objects from the Signnow API
6
+ #
7
+ # @param [Hash] options Options to pass to the API
8
+ # @return [Array] The available objects
9
+ def all(attributes = {})
10
+ response = Signnow.request(:get, nil, api_all_url , attributes, options_for_all(attributes))
11
+ results_from response
12
+ end
13
+
14
+ # URl for the all endpoint
15
+ # overwrite this in the model if the api is not well named
16
+ #
17
+ def api_all_url
18
+ "#{self.name.split("::").last.downcase}"
19
+ end
20
+ protected :api_all_url
21
+
22
+ # Options for all
23
+ # overwrite this in the model to set security
24
+ #
25
+ # @return [Hash]
26
+ def options_for_all(attributes)
27
+ access_token = attributes.delete(:access_token)
28
+ raise AuthenticationError unless access_token
29
+ {
30
+ auth_type: :user_token,
31
+ auth_token: access_token
32
+ }
33
+ end
34
+
35
+ private
36
+ def results_from(response)
37
+ results = []
38
+ response.each do |obj|
39
+ results << self.new(obj)
40
+ end
41
+ results
42
+ end
43
+ end
44
+
45
+ def self.included(base)
46
+ base.extend(ClassMethods)
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,35 @@
1
+ module Signnow
2
+ module Operations
3
+ module Create
4
+ module ClassMethods
5
+ # Creates a new object
6
+ #
7
+ # @param [Hash] attributes The attributes of the created object
8
+ def create(attributes)
9
+ response = Signnow.request(:post, nil, api_create_url, attributes, options_for_create)
10
+ self.new(response)
11
+ end
12
+
13
+ # URl for the create endpoint
14
+ # overwrite this in the model if the api is not well named
15
+ #
16
+ def api_create_url
17
+ "#{self.name.split("::").last.downcase}"
18
+ end
19
+ protected :api_create_url
20
+
21
+ # Options for create
22
+ # overwrite this in the model if the api is not well named
23
+ #
24
+ # @return [Hash]
25
+ def options_for_create
26
+ { auth_type: :basic }
27
+ end
28
+ end
29
+
30
+ def self.included(base)
31
+ base.extend(ClassMethods)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,42 @@
1
+ module Signnow
2
+ module Operations
3
+ module Delete
4
+ module ClassMethods
5
+ # Deletes the given object
6
+ #
7
+ # @param [Integer] id The id of the object that gets deleted
8
+ def delete(attributes={})
9
+ id = attributes.delete(:id)
10
+ response = Signnow.request(:delete, nil, api_delete_url(id) , {}, options_for_delete(attributes))
11
+ true
12
+ end
13
+
14
+ # URl for the delete endpoint
15
+ # overwrite this in the model if the api is not well named
16
+ #
17
+ def api_delete_url(id)
18
+ "#{self.name.split("::").last.downcase}s/#{id}"
19
+ end
20
+ protected :api_delete_url
21
+
22
+ # Options for delete
23
+ # overwrite this in the model to set security
24
+ #
25
+ # @return [Hash]
26
+ def options_for_delete(attributes)
27
+ access_token = attributes.delete(:access_token)
28
+ raise AuthenticationError unless access_token
29
+ {
30
+ auth_type: :user_token,
31
+ auth_token: access_token
32
+ }
33
+ end
34
+ protected :options_for_delete
35
+ end
36
+
37
+ def self.included(base)
38
+ base.extend(ClassMethods)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,41 @@
1
+ module Signnow
2
+ module Operations
3
+ module DownloadLink
4
+ module ClassMethods
5
+ # Request a one time download
6
+ #
7
+ def download_link(attributes={})
8
+ response = Signnow.request(:post, nil, api_download_link_url(attributes[:id]), {}, options_for_download_link(attributes))
9
+ response['link']
10
+ end
11
+
12
+ # URl for the show endpoint
13
+ # overwrite this in the model if the api is not well named
14
+ #
15
+ def api_download_link_url(id=nil)
16
+ url = "#{self.name.split("::").last.downcase}"
17
+ url += "/#{id}" if id
18
+ url += "/download/link"
19
+ url
20
+ end
21
+ protected :api_download_link_url
22
+
23
+ # Options for show
24
+ # overwrite this in the model to set security
25
+ #
26
+ # @return [Hash]
27
+ def options_for_download_link(attributes)
28
+ raise AuthenticationError unless attributes[:access_token]
29
+ {
30
+ auth_type: :user_token,
31
+ auth_token: attributes[:access_token]
32
+ }
33
+ end
34
+ end
35
+
36
+ def self.included(base)
37
+ base.extend(ClassMethods)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,43 @@
1
+ module Signnow
2
+ module Operations
3
+ module Find
4
+ module ClassMethods
5
+ # Finds a given object
6
+ #
7
+ # @param [Integer] id The id of the object that should be found
8
+ # @return [Signnow::Base] The found object
9
+ def find(attibutes)
10
+ id = attibutes.delete(:id)
11
+ response = Signnow.request(:get, nil, api_find_url(id), {}, options_for_find(attributes))
12
+ self.new(response["data"])
13
+ end
14
+
15
+ # URl for the find endpoint
16
+ # overwrite this in the model if the api is not well named
17
+ #
18
+ def api_find_url(id)
19
+ "#{self.name.split("::").last.downcase}s/#{id}"
20
+ end
21
+ protected :api_find_url
22
+
23
+ # Options for find
24
+ # overwrite this in the model to set security
25
+ #
26
+ # @return [Hash]
27
+ def options_for_find(attributes)
28
+ access_token = attributes.delete(:access_token)
29
+ raise AuthenticationError unless access_token
30
+ {
31
+ auth_type: :user_token,
32
+ auth_token: access_token
33
+ }
34
+ end
35
+ protected :options_for_find
36
+ end
37
+
38
+ def self.included(base)
39
+ base.extend(ClassMethods)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,42 @@
1
+ module Signnow
2
+ module Operations
3
+ module Show
4
+ module ClassMethods
5
+ # Shows a given object
6
+ #
7
+ # @param [Integer] id The id of the object that should be shown
8
+ # @return [Signnow::Base] The found object
9
+ def show(attributes={})
10
+ response = Signnow.request(:get, nil, api_show_url(attributes[:id]), {}, options_for_show(attributes))
11
+ self.new(response)
12
+ end
13
+
14
+ # URl for the show endpoint
15
+ # overwrite this in the model if the api is not well named
16
+ #
17
+ def api_show_url(id=nil)
18
+ url = "#{self.name.split("::").last.downcase}"
19
+ url += "/#{id}" if id
20
+ url
21
+ end
22
+ protected :api_show_url
23
+
24
+ # Options for show
25
+ # overwrite this in the model to set security
26
+ #
27
+ # @return [Hash]
28
+ def options_for_show(attributes)
29
+ raise AuthenticationError unless attributes[:access_token]
30
+ {
31
+ auth_type: :user_token,
32
+ auth_token: attributes[:access_token]
33
+ }
34
+ end
35
+ end
36
+
37
+ def self.included(base)
38
+ base.extend(ClassMethods)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,52 @@
1
+ module Signnow
2
+ module Operations
3
+ module Update
4
+
5
+ module ClassMethods
6
+ # Updates a object
7
+ # @param [Integer] id The id of the object that should be updated
8
+ # @param [Hash] attributes The attributes that should be updated
9
+ def update_attributes(attributes={})
10
+ id = attributes.delete(:id)
11
+ response = Signnow.request(:put, nil, api_update_url(id), attributes, options_for_update(attributes))
12
+ self.new(response["data"])
13
+ end
14
+
15
+ # URl for the update endpoint
16
+ # overwrite this in the model if the api is not well named
17
+ #
18
+ def api_update_url(id=nil)
19
+ url = "#{self.name.split("::").last.downcase}"
20
+ url += "/#{id}" if id
21
+ url
22
+ end
23
+ protected :api_update_url
24
+
25
+ # Options for update
26
+ # overwrite this in the model to set security
27
+ #
28
+ # @return [Hash]
29
+ def options_for_update(attributes)
30
+ access_token = attributes.delete(:access_token)
31
+ raise AuthenticationError unless access_token
32
+ {
33
+ auth_type: :user_token,
34
+ auth_token: access_token
35
+ }
36
+ end
37
+ end
38
+
39
+ def self.included(base)
40
+ base.extend(ClassMethods)
41
+ end
42
+
43
+ # Updates a object
44
+ #
45
+ # @param [Hash] attributes The attributes that should be updated
46
+ def update_attributes(attributes)
47
+ response = Signnow.request(:put, nil, "#{self.class.name.split("::").last.downcase}/#{id}", attributes)
48
+ set_attributes(response["data"])
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,34 @@
1
+ module Signnow
2
+ module Request
3
+ class Base
4
+ attr_reader :info
5
+ attr_accessor :response
6
+
7
+ def initialize(info)
8
+ @info = info
9
+ end
10
+
11
+ def perform
12
+ raise AuthenticationError if Signnow.encoded_app_credentials.nil?
13
+ connection.setup_https
14
+ send_request
15
+
16
+ validator.validated_data_for(response)
17
+ end
18
+
19
+ protected
20
+
21
+ def send_request
22
+ self.response = connection.request
23
+ end
24
+
25
+ def connection
26
+ @connection ||= Connection.new(info)
27
+ end
28
+
29
+ def validator
30
+ @validator ||= Validator.new(info)
31
+ end
32
+ end
33
+ end
34
+ end