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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b06055e99370cf62de7dce20b653a01a098bb39b
4
+ data.tar.gz: 9ed58ce770ce2437ecb0f9410914716b7aec9ea9
5
+ SHA512:
6
+ metadata.gz: 7880020a0c9dd24b52d5d22d3a03fed698cf36ddb3c7b7e373b8d9b23313ae42fee3aa45cfbbe37d1b352d7a683b2ea1a4775d54b6dc60b81d363cc18b0fe5d2
7
+ data.tar.gz: be414eb7985a9c1151ef47000e3ea434839ba34fe59c9e8e6af8b812fdb73fe4b998fd7d3312086cd21da4609c657340901548bdd67595adbb3eefd19f8ed907
@@ -0,0 +1,11 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.html
6
+ /.rvmrc
7
+ .project
8
+ .ruby-version
9
+ .ruby-gemset
10
+ /spec/integration/*
11
+ /coverage/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "rspec"
6
+ gem "rake"
7
+ gem "webmock"
8
+ gem "pry"
9
+ gem 'coveralls'
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2012 Andres Bravo
4
+ Copyright (c) 2012 dkd Internet Service GmbH
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,150 @@
1
+ SignNow [![Build Status](https://secure.travis-ci.org/andresbravog/signnow-ruby.png)](https://travis-ci.org/andresbravog/signnow-ruby) [![Code Climate](https://codeclimate.com/github/andresbravog/signnow-ruby.png)](https://codeclimate.com/github/andresbravog/signnow-ruby) [![Coverage Status](https://coveralls.io/repos/andresbravog/signnow-ruby/badge.png)](https://coveralls.io/r/andresbravog/signnow-ruby)
2
+ ======
3
+
4
+ This is a Ruby wrapper for SignNow's API.
5
+
6
+ Documentation
7
+ =====
8
+
9
+ We use RubyDoc for documentation.
10
+
11
+ Usage
12
+ ======
13
+
14
+ First, you've to install the gem
15
+
16
+ gem install signnow
17
+
18
+ and require it
19
+
20
+ require "signnow"
21
+
22
+ and set up your api_key
23
+
24
+ Signnow.configure do |config|
25
+ config[:app_id] = "_your_application_app_id_",
26
+ config[:app_secret] = "_your_application_app_secret_"
27
+ end
28
+
29
+ set the extra ```:use_test_env?``` config option to true if you want to use the [signnow test environment](https://eval.signnow.com)
30
+
31
+ Signnow.configure do |config|
32
+ config[:use_test_env?] = true
33
+ end
34
+
35
+ Oauth
36
+ =====
37
+
38
+ *[SignNow oauth2 API documentation](https://signnow.atlassian.net/wiki/display/SAPI/REST+Endpoints#RESTEndpoints-POST/oauth2)*
39
+
40
+ Redirecting the user to the authorize url, You need to provide a redirect url where you will get a code param.
41
+
42
+ # Redirect the user to the authorize url
43
+ redirect_to Signnow::Authentications::Oauth.authorize_url(redirect_uri: your_redirect_url)
44
+
45
+ Process the code param in order to get user oauth credentials.
46
+
47
+ oauth_credentials = Signnow::Authentications::Oauth.authenticate(code: code)
48
+
49
+ =>
50
+ {
51
+ access_token: '_user_access_token_',
52
+ refresh_token: '_user_refresh_token',
53
+ token_type: 'bearer',
54
+ scope: '*',
55
+ last_login: 2013929273,
56
+ expires_in: 2013929273
57
+ }
58
+
59
+ Store the access token credentials in order to use the Signnow API
60
+
61
+ Signnow::User.show(access_token: oauth_credentials.access_token)
62
+
63
+
64
+ Users
65
+ =====
66
+
67
+ *[SignNow users API documentation](https://signnow.atlassian.net/wiki/display/SAPI/REST+Endpoints#RESTEndpoints-/user)*
68
+
69
+ Creating a user:
70
+
71
+ user = Singnow::User.create(
72
+ email: 'yournewuser@email.com', # required
73
+ password: 'new_password', # required
74
+ first_name: 'john', # optional
75
+ last_name: 'doe', # optional
76
+ )
77
+
78
+ Store the acess_token
79
+
80
+ token = user.access_token
81
+
82
+ Generate a client with the access token
83
+
84
+ client = Signnow::Client.new(token)
85
+
86
+ Showing a user:
87
+
88
+ client.perform! |token|
89
+ Singnow::User.show(access_token: token)
90
+ end
91
+
92
+
93
+ Documents
94
+ =====
95
+
96
+ *[SignNow documents API documentation](https://signnow.atlassian.net/wiki/display/SAPI/REST+Endpoints#RESTEndpoints-/document)*
97
+
98
+ List user documents:
99
+
100
+ client.perform! |token|
101
+ Singnow::Document.all(access_token: token)
102
+ end
103
+
104
+ Show a document:
105
+
106
+ client.perform! |token|
107
+ Singnow::Document.show(id: 'document_id', access_token: token)
108
+ end
109
+
110
+ Get a one time download link for a document:
111
+
112
+ link =
113
+ client.perform! |token|
114
+ Singnow::Document.download_link(id: 'document_id', access_token: token)
115
+ end
116
+
117
+
118
+ Documentation
119
+ =====
120
+
121
+ *[SignNow developers page](https://developers.signnow.com)*
122
+
123
+ *[SignNow API documentation](https://signnow.atlassian.net/wiki/display/SAPI/REST+Endpoints)*
124
+
125
+
126
+ Requirements
127
+ =====
128
+
129
+ This gem requires at least Ruby 1.9 and faces version 1 of SignNow's API.
130
+
131
+ Bugs
132
+ ======
133
+
134
+ Please report bugs at http://github.com/andresbravog/signnow-ruby/issues.
135
+
136
+ Note on Patches/Pull Requests
137
+ ======
138
+
139
+ * Fork the project from http://github.com/andresbravog/signnow-ruby.
140
+ * Make your feature addition or bug fix.
141
+ * Add tests for it. This is important so I don't break it in a
142
+ future version unintentionally.
143
+ * Commit, do not mess with rakefile, version, or history.
144
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
145
+ * Send me a pull request. Bonus points for topic branches.
146
+
147
+ Copyright
148
+ ======
149
+
150
+ Copyright (c) 2012-2013 andresbravog Internet Service GmbH, Andres Bravo. See LICENSE for details.
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new(:spec) do |spec|
5
+ spec.pattern = FileList['spec/**/*_spec.rb']
6
+ end
7
+
8
+ task default: :spec
@@ -0,0 +1,119 @@
1
+ require "net/http"
2
+ require "net/https"
3
+ require "json"
4
+ require "signnow/version"
5
+ require 'base64'
6
+
7
+ module Signnow
8
+ DOMAIN_BASE = 'api'
9
+ TEST_DOMAIN_BASE = 'capi-eval'
10
+ API_BASE = 'signnow.com'
11
+ API_BASE_PATH = nil
12
+ TEST_API_BASE_PATH = 'api'
13
+ API_VERSION = 'v1'
14
+ ROOT_PATH = File.dirname(__FILE__)
15
+
16
+ @@configuration = {}
17
+
18
+ autoload :Base, "signnow/base"
19
+ autoload :User, "signnow/user"
20
+ autoload :Document, "signnow/document"
21
+ autoload :Client, "signnow/client"
22
+
23
+ module Authentications
24
+ autoload :Base, "signnow/authentications/base"
25
+ autoload :Oauth, "signnow/authentications/oauth"
26
+ end
27
+
28
+ module Operations
29
+ autoload :All, "signnow/operations/all"
30
+ autoload :Create, "signnow/operations/create"
31
+ autoload :Delete, "signnow/operations/delete"
32
+ autoload :Find, "signnow/operations/find"
33
+ autoload :Show, "signnow/operations/show"
34
+ autoload :DownloadLink, "signnow/operations/download_link"
35
+ autoload :Update, "signnow/operations/update"
36
+ end
37
+
38
+ module Request
39
+ autoload :Base, "signnow/request/base"
40
+ autoload :Connection, "signnow/request/connection"
41
+ autoload :Helpers, "signnow/request/helpers"
42
+ autoload :Info, "signnow/request/info"
43
+ autoload :Validator, "signnow/request/validator"
44
+ end
45
+
46
+ class SignnowError < StandardError; end
47
+ class AuthenticationError < SignnowError; end
48
+ class APIError < SignnowError; end
49
+ class NotFound < SignnowError; end
50
+ class InvalidToken < APIError; end
51
+ class EmptyDocuments < APIError; end
52
+
53
+ # Returns the set api key
54
+ #
55
+ # @return [String] The api key
56
+ def self.encoded_app_credentials
57
+ return unless configuration[:app_id] && configuration[:app_secret]
58
+ configuration[:encoded_app_credentials] ||=
59
+ Base64.strict_encode64("#{configuration[:app_id]}:#{configuration[:app_secret]}")
60
+ end
61
+
62
+ # Use thisfunciotn with a block to add app credentials configuration
63
+ # Example:
64
+ #
65
+ # Signnow.configure do |config|
66
+ # config[:app_id] = 'your_app_id'
67
+ # config[:app_secret] = 'your_app_secret'
68
+ # end
69
+ def self.configure
70
+ return unless block_given?
71
+ yield(configuration)
72
+ end
73
+
74
+ # Returns configuration hash
75
+ # @return [Hash]
76
+ def self.configuration
77
+ @@configuration
78
+ end
79
+
80
+ # Makes a request against the Signnow API
81
+ #
82
+ # @param [Symbol] http_method The http method to use, must be one of :get, :post, :put and :delete
83
+ # @param [String] domain The API domain to use
84
+ # @param [String] api_url The API url to use
85
+ # @param [Hash] data The data to send, e.g. used when creating new objects.
86
+ # @return [Array] The parsed JSON response.
87
+ def self.request(http_method, domain, api_url, data, options={})
88
+ info = Request::Info.new(http_method, domain, api_url, data, options)
89
+ Request::Base.new(info).perform
90
+ end
91
+
92
+ # Returns the domain base of the api depending on the env
93
+ #
94
+ def self.domain
95
+ if configuration[:use_test_env?]
96
+ TEST_DOMAIN_BASE
97
+ else
98
+ DOMAIN_BASE
99
+ end
100
+ end
101
+
102
+ # Returns the api base path depending on the env
103
+ #
104
+ def self.base_path
105
+ if configuration[:use_test_env?]
106
+ TEST_API_BASE_PATH
107
+ else
108
+ API_BASE_PATH
109
+ end
110
+ end
111
+
112
+ # Returns the api uri
113
+ #
114
+ def self.uri
115
+ uri = "https://#{domain}"
116
+ base_path && uri += "/#{base_path}"
117
+ uri
118
+ end
119
+ end
@@ -0,0 +1,57 @@
1
+ module Signnow
2
+ module Authentications
3
+ class Base
4
+ class << self
5
+ # Retrieves an oauth token from the Signnow API
6
+ #
7
+ # @param [Hash] attributes Attributes to pass to the API
8
+ # @return [Array] The available objects
9
+ def authenticate(attributes = {})
10
+ response = Signnow.request(
11
+ :post,
12
+ nil,
13
+ self.api_authenticate_url,
14
+ attributes_for_authentication(attributes),
15
+ options_for_authentication.merge(
16
+ use_form_data: true
17
+ )
18
+ )
19
+ self.new(response)
20
+ end
21
+
22
+ protected
23
+
24
+ # URl for the authenticate endpoint
25
+ # overwrite this in the model if the api is not well named
26
+ #
27
+ def api_authenticate_url
28
+ "#{self.name.split("::").last.downcase}"
29
+ end
30
+
31
+ # Options for the authentication mehtod
32
+ #
33
+ # @return [Hash]
34
+ def options_for_authentication
35
+ { auth_type: :basic }
36
+ end
37
+
38
+ # Attributes for the authentication mehtod
39
+ # overwrite this in the class to add attributes
40
+ #
41
+ # @param [Hash] attributes to merge with
42
+ # @return [Hash]
43
+ def attributes_for_authentication(attributes={})
44
+ {}.merge(attributes)
45
+ end
46
+
47
+ def domain
48
+ if Signnow.configuration[:use_test_env?]
49
+ 'eval'
50
+ else
51
+ 'www'
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,68 @@
1
+ module Signnow
2
+ module Authentications
3
+ class Oauth < Base
4
+ include Signnow::Operations::Create
5
+
6
+ attr_accessor :created, :access_token, :token_type, :expires_in,
7
+ :refresh_token, :scope, :last_login
8
+
9
+ class << self
10
+ # Returns the redirect url to authorize
11
+ #
12
+ # @param [Hash] options
13
+ # @option options [String] redirect_url () redirect url to receive the code param
14
+ def authorize_url(options = {})
15
+ protocol = "https"
16
+ base_url = "#{protocol}://#{domain}.#{API_BASE}"
17
+ path = "/proxy/index.php/authorize"
18
+ params = {
19
+ client_id: Signnow.configuration[:app_id],
20
+ redirect_uri: options[:redirect_uri],
21
+ response_type: 'code'
22
+ }
23
+
24
+ "#{base_url}#{path}?#{URI.encode_www_form(params)}"
25
+ end
26
+
27
+ protected
28
+
29
+ def api_authenticate_url
30
+ "oauth2/token"
31
+ end
32
+
33
+ # Attributes for the authentication mehtod
34
+ # overwrite this in the class to add attributes
35
+ #
36
+ # @param [Hash] attributes to merge with
37
+ # @return [Hash]
38
+ def attributes_for_authentication(attributes={})
39
+ { grant_type: 'authorization_code' }.merge(attributes)
40
+ end
41
+ end
42
+
43
+ # Initializes the object using the given attributes
44
+ #
45
+ # @param [Hash] attributes The attributes to use for initialization
46
+ def initialize(attributes = {})
47
+ set_attributes(attributes)
48
+ parse_timestamps
49
+ end
50
+
51
+ # Sets the attributes
52
+ #
53
+ # @param [Hash] attributes The attributes to initialize
54
+ def set_attributes(attributes)
55
+ attributes.each_pair do |key, value|
56
+ instance_variable_set("@#{key}", value)
57
+ end
58
+ end
59
+
60
+ # Parses UNIX timestamps and creates Time objects.
61
+ def parse_timestamps
62
+ @created = Time.at(created.to_i) if created
63
+ @expires_in = Time.at(expires_in.to_i) if expires_in
64
+ @last_login = Time.at(last_login.to_i) if last_login
65
+ end
66
+ end
67
+ end
68
+ end