inkdit 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -61,4 +61,22 @@ module Inkdit
61
61
 
62
62
  # an exception that's raised when a request fails due to an invalid access token
63
63
  class Unauthorized < Exception; end
64
+
65
+ # a generic exception raised when an request fails for any other reason
66
+ class Error < Exception
67
+ attr_reader :response
68
+
69
+ def initialize(response)
70
+ @response = response
71
+ super()
72
+ end
73
+
74
+ def message
75
+ "response.status=#{response.status} response.body=#{response.body.inspect}"
76
+ end
77
+
78
+ def inspect
79
+ "#<#{self.class.inspect} #{self.message}>"
80
+ end
81
+ end
64
82
  end
@@ -74,7 +74,9 @@ module Inkdit
74
74
  # raises {Inkdit::Unauthorized} if the response indicates an invalid access token.
75
75
  # @return [OAuth2::Response]
76
76
  def checked_response(response)
77
- if response.status == 401 and response.headers['www-authenticate'].match /invalid_token/
77
+ if response.status == 401 and
78
+ response.headers['www-authenticate'] and
79
+ response.headers['www-authenticate'].match /invalid_token/
78
80
  # wrong access token or the user rescinded the app's access.
79
81
  # TODO: properly parse the header
80
82
  # Bearer realm="api.inkdit.com", error="invalid_token"
@@ -12,6 +12,8 @@ module Inkdit
12
12
  # @return [Contract] contract the newly created contract
13
13
  def self.create(client, owner, params)
14
14
  response = client.post owner.contracts_link, { :body => params.to_json, :headers => { 'Content-Type' => 'application/json' } }
15
+ raise Inkdit::Error.new(response) unless response.status == 201
16
+
15
17
  self.new(client, response.parsed)
16
18
  end
17
19
 
@@ -55,8 +57,8 @@ module Inkdit
55
57
  end
56
58
  end
57
59
 
58
- # @param [Entity] individual the individual who should be connected to the contract
59
- # @param [Entity] entity the entity that the individual should be connected to the contract through
60
+ # @param [String] individual the url of an individual who should be connected to the contract
61
+ # @param [String] entity the url of an entity that the individual should be connected to the contract through
60
62
  def share_with(individual, entity)
61
63
  params = {
62
64
  :individual => {
@@ -25,6 +25,8 @@ module Inkdit
25
25
  # @return [Array<Contract>] the contracts in this entity's Contract Collection
26
26
  def get_contracts
27
27
  response = @client.get(contracts_link)
28
+ raise Inkdit::Error.new(response) unless response.status == 200
29
+
28
30
  response.parsed['resources'].map do |contract_params|
29
31
  Contract.new @client, contract_params
30
32
  end
@@ -13,7 +13,9 @@ module Inkdit
13
13
 
14
14
  # @return [Entity] the entity this field was signed on behalf of
15
15
  def on_behalf_of
16
- Inkdit::Entity.new(@client, @params['on_behalf_of'])
16
+ if @params['on_behalf_of']
17
+ Inkdit::Entity.new(@client, @params['on_behalf_of'])
18
+ end
17
19
  end
18
20
 
19
21
  # @return [Time] when this field was signed
@@ -12,6 +12,10 @@ module Inkdit
12
12
  @params['url']
13
13
  end
14
14
 
15
+ def designation_url
16
+ @params['links']['designation']
17
+ end
18
+
15
19
  # sign this field as the user and entity associated with the current access token.
16
20
  # @return [Signature] the newly-created signature
17
21
  def sign!
@@ -21,9 +25,41 @@ module Inkdit
21
25
 
22
26
  response = @client.put self.url, { :body => params.to_json, :headers => { 'Content-Type' => 'application/json' } }
23
27
 
28
+ if response.status != 201
29
+ raise Inkdit::Error.new(response)
30
+ end
31
+
24
32
  Inkdit::Signature.new(@client, response.parsed)
25
33
  end
26
34
 
35
+ # +params+ is a Hash specifying who this signature field should be
36
+ # designated to.
37
+ # It must contain the key +individual+.
38
+ # It can contain the key +organization+ if a specific organization
39
+ # is being designated.
40
+ #
41
+ # , and +organization+. At least one
42
+ # should be present. They should be [Entity]s
43
+ def designate(params)
44
+ params = {
45
+ :individual => { :url => params[:individual].url }
46
+ }
47
+
48
+ params[:entity] = if params[:organization]
49
+ { :url => params[:organization].url }
50
+ else
51
+ params[:individual]
52
+ end
53
+
54
+ response = @client.put self.designation_url, { :body => params.to_json, :headers => { 'Content-Type' => 'application/json' } }
55
+
56
+ if response.status != 200
57
+ raise Inkdit::Error.new(response)
58
+ end
59
+
60
+ Inkdit::SignatureField.new(@client, @contract, response.parsed)
61
+ end
62
+
27
63
  def inspect
28
64
  "#<#{self.class.inspect} params=#{@params}>"
29
65
  end
@@ -1,3 +1,3 @@
1
1
  module Inkdit
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -35,6 +35,15 @@ get '/get-auth-code' do
35
35
  end
36
36
 
37
37
  get '/got-auth-code' do
38
+ if params[:error]
39
+ return <<END
40
+ <p>Uh-oh, we ran into a problem!</p>
41
+
42
+ <p>Error: <code>#{params[:error]}</code></p>
43
+ <p>Description: #{params[:error_description]}</p>
44
+ END
45
+ end
46
+
38
47
  auth_code = params[:code]
39
48
 
40
49
  @access_token = Inkdit.get_token(params[:code], url('/got-auth-code'))
@@ -40,11 +40,35 @@ describe Inkdit::Contract do
40
40
  contract.signatures.length.should == 1
41
41
  signature_field = contract.signatures.first
42
42
 
43
+ designated_line = signature_field.designate :individual => entity
44
+
43
45
  signature = signature_field.sign!
44
46
 
45
- signature.signed_by.should == entity
46
- signature.on_behalf_of.should == entity
47
+ signature.signed_by.should == entity
48
+
49
+ if entity.type == 'individual'
50
+ signature.on_behalf_of.should be_nil
51
+ else
52
+ signature.on_behalf_of.should == entity
53
+ end
47
54
  end
48
55
 
49
- pending 'sharing a contract (this is tough to do because it requires an entity URL)'
56
+ pending 'allows you to share a contract (this is tough to do because it requires an entity URL)' do
57
+ entity = @client.get_entity
58
+
59
+ contract_name = "API Test #{Time.now.iso8601}"
60
+
61
+ params = {
62
+ :name => contract_name,
63
+ :content => "I agree etc. etc. etc.\n##signature##",
64
+ :test => true
65
+ }
66
+
67
+ contract = Inkdit::Contract.create @client, entity, params
68
+
69
+ # this shouldn't actually return a response but whatevs.
70
+ response = contract.share_with(SOME_OTHER_USER, SOME_OTHER_USER)
71
+
72
+ response.status.should == 201
73
+ end
50
74
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inkdit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-10 00:00:00.000000000 Z
12
+ date: 2012-05-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oauth2
16
- requirement: &14778920 !ruby/object:Gem::Requirement
16
+ requirement: &21202900 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *14778920
24
+ version_requirements: *21202900
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: yard
27
- requirement: &14778260 !ruby/object:Gem::Requirement
27
+ requirement: &21200800 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *14778260
35
+ version_requirements: *21200800
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &14777780 !ruby/object:Gem::Requirement
38
+ requirement: &20798500 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *14777780
46
+ version_requirements: *20798500
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: sinatra
49
- requirement: &14627380 !ruby/object:Gem::Requirement
49
+ requirement: &20793340 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *14627380
57
+ version_requirements: *20793340
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: haml
60
- requirement: &14625260 !ruby/object:Gem::Requirement
60
+ requirement: &20819320 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *14625260
68
+ version_requirements: *20819320
69
69
  description: ! 'A library for using the Inkdit API to sign, share and store contracts.
70
70
 
71
71