lifen 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f5c85c29e8ab9cc7f36987098d50f12a5beb96d
4
- data.tar.gz: 74e319a77102bbc00630fa6f24acdec72d4ee789
3
+ metadata.gz: 48d8b72c4e7250d82b16d02e3f58f1f160e06289
4
+ data.tar.gz: ea331938beaafdf10c457464a141b77af6c71713
5
5
  SHA512:
6
- metadata.gz: b6c8d52af33bb08e5c3926d8bbee08edbd50e6da9ded2303fe62f5b917e6c85c4e955d9233414f2ea79ab702624ef68589f16fe0c168f7e51aba58d68027ee32
7
- data.tar.gz: 436d77f9de97847cf494d0336646a98a958ec12333bf9df5eaf7930d965e8b538fbba24da4d3338b4980fa2ba5986ec331756670f88a37b428c806cc96dc19d6
6
+ metadata.gz: bf51797de230eb1f6abc04cf09e858d8d72461eaed547919b4d3e7f04164209e8fc5fb272e3194e30ad583bee3d746988efecb169ba02b093db304c286b8ddf0
7
+ data.tar.gz: 2d855c4608cd0c93bda9b4d3fe47079d959f9986531e9e08f66fa095cd23cdb5ed35f7d6fbab214c280c50b3b64f43f5eec08af20b5205abb4ff0ce9cf79350d
@@ -1,3 +1,9 @@
1
+ 1.1.0
2
+ -----
3
+
4
+ - Added the `expiration_margin` configuration to force refresh soon-to-be-expired tokens
5
+ - Check the validity of the provided URL [#1]
6
+
1
7
  1.0.2
2
8
  -----
3
9
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lifen (1.0.2)
4
+ lifen (1.1.0)
5
5
  faraday (>= 0.9)
6
6
  inflecto
7
7
  virtus (>= 1.0)
data/README.md CHANGED
@@ -6,7 +6,9 @@ Lifen is a ruby client for [Lifen](https://www.lifen.fr/) JSON API.
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'lifen'
9
+ ```ruby
10
+ gem 'lifen'
11
+ ```
10
12
 
11
13
  And then execute:
12
14
 
@@ -22,38 +24,53 @@ Or install it yourself as:
22
24
 
23
25
  Lifen can be configured (ideally inside an initializer) like so:
24
26
 
25
- Lifen.configure do |config|
26
- config.site = "https://develop.lifen.fr/"
27
- config.application_access_token = "application_access_token"
28
- config.proxy_url = "http://my.proxy.fr/"
29
- end
27
+ ```ruby
28
+ Lifen.configure do |config|
29
+ config.site = "https://develop.lifen.fr/"
30
+ config.application_access_token = "application_access_token"
31
+
32
+ # optionnal
33
+ config.proxy_url = "http://my.proxy.fr/"
34
+ config.expiration_margin = 60 # in seconds, default: 0
35
+ end
36
+ ```
30
37
 
31
- `site` and `application_access_token` are mandatory attributes.
38
+ Optionnal configuration:
39
+
40
+ - `proxy_url`: enables you to route all your requests via a proxy
41
+ - `expiration_margin`: by default a token is considered valid until the expiration time but you can add a security expiration margin
32
42
 
33
43
  ### Managing users
34
44
 
35
- user = Lifen::User.new(email: "email@domain.tld", first_name: "Jean", last_name: "Dupont")
36
- user.create
37
- user.token.refresh
38
- user.status.refresh
45
+ ```ruby
46
+ user = Lifen::User.new(email: "email@domain.tld", first_name: "Jean", last_name: "Dupont")
47
+ user.create
48
+ user.token.refresh
49
+ user.status.refresh
50
+ ```
39
51
 
40
52
  ### Managing flows
41
53
 
42
- user = Lifen::User.new(uuid: "valid_uuid")
43
- user.flows
54
+ ```ruby
55
+ user = Lifen::User.new(uuid: "valid_uuid")
56
+ user.flows
44
57
 
45
- flow = Lifen::Flow.new(user: user, title: "honestica Rocks !")
46
- flow.create
47
- flow.attach_users(user)
48
- flow.detach_users(user)
58
+ flow = Lifen::Flow.new(user: user, title: "honestica Rocks !")
59
+ flow.create
60
+ flow.attach_users(user)
61
+ flow.detach_users(user)
49
62
 
50
- flow = Lifen::Flow.new(user: user, title: "honestica Rocks !", users: [user_1, user_2])
51
- flow.create
63
+ # alternate strategy
64
+ flow = Lifen::Flow.new(user: user, title: "honestica Rocks !", users: [user_1, user_2])
65
+ flow.create
66
+ ```
52
67
 
53
68
  ### Managing messages
54
69
 
55
- message = Lifen::Message.new(flow: flow, content: "Hello World !")
56
- message.create
70
+ ```ruby
71
+ message = Lifen::Message.new(flow: flow, content: "Hello World !")
72
+ message.create
73
+ ```
57
74
 
58
75
  ## Contributing
59
76
 
@@ -1,10 +1,24 @@
1
1
  module Lifen
2
2
  class Configuration
3
- attr_accessor :site, :application_access_token, :proxy_url
3
+ attr_accessor :site, :application_access_token, :proxy_url, :expiration_margin
4
+
5
+ def initialize(args)
6
+ args.each do |k,v|
7
+ instance_variable_set("@#{k}", v) unless v.nil?
8
+ end
9
+ end
10
+
11
+ def site=(url)
12
+ if !/(.*)\/$/.match(url)
13
+ raise Lifen::Error, "Invalid 'site' provided in configuration: '#{url}', a trailing slash is missing"
14
+ end
15
+
16
+ @site = url
17
+ end
4
18
  end
5
19
 
6
20
  def self.configuration
7
- @configuration ||= Configuration.new
21
+ @configuration ||= Configuration.new(expiration_margin: 0)
8
22
  end
9
23
 
10
24
  def self.configure
@@ -12,13 +12,19 @@ module Lifen
12
12
  end
13
13
 
14
14
  def active?
15
- valid? and !has_expired?
15
+ valid? and !needs_to_be_refreshed?
16
16
  end
17
17
 
18
18
  def valid?
19
19
  !value.nil? and value.length > 0 and !expires_at.nil?
20
20
  end
21
21
 
22
+ def needs_to_be_refreshed?
23
+ return true if has_expired?
24
+
25
+ return (expires_at - expiration_margin) < Time.now.to_i
26
+ end
27
+
22
28
  def has_expired?
23
29
  return true if expires_at.nil?
24
30
 
@@ -46,5 +52,9 @@ module Lifen
46
52
  @client ||= AppAuthenticatedClient.new
47
53
  end
48
54
 
55
+ def expiration_margin
56
+ Lifen.configuration.expiration_margin
57
+ end
58
+
49
59
  end
50
60
  end
@@ -1,3 +1,3 @@
1
1
  module Lifen
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Lifen::Configuration do
4
+
5
+ describe ':site' do
6
+
7
+ context 'invalid site' do
8
+
9
+ let(:invalid_site) { "http://website.fr" }
10
+
11
+ it 'raises an error' do
12
+ expect{
13
+ Lifen.configure do |config|
14
+ config.site = invalid_site
15
+ end
16
+ }.to raise_error(Lifen::Error, "Invalid 'site' provided in configuration: 'http://website.fr', a trailing slash is missing")
17
+ end
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -2,16 +2,66 @@ require 'spec_helper'
2
2
 
3
3
  describe Lifen::Token do
4
4
 
5
+ describe ':needs_to_be_refreshed?' do
6
+
7
+ context 'default configuration' do
8
+ it 'knows when a token needs to be refreshed' do
9
+ token = Lifen::Token.new(value: "valid_token", expires_at: Time.now.to_i + 120)
10
+
11
+ expect(token.needs_to_be_refreshed?).to be_falsy
12
+ end
13
+
14
+ it 'knows when a token needs to be refreshed' do
15
+ token = Lifen::Token.new(value: "valid_token", expires_at: Time.now.to_i + 40)
16
+
17
+ expect(token.needs_to_be_refreshed?).to be_falsy
18
+ end
19
+ end
20
+
21
+ context 'custom configuration' do
22
+ before do
23
+ Lifen.configure do |config|
24
+ config.expiration_margin = 60
25
+ end
26
+ end
27
+
28
+ it 'knows when a token needs to be refreshed' do
29
+ token = Lifen::Token.new(value: "valid_token", expires_at: Time.now.to_i + 120)
30
+
31
+ expect(token.needs_to_be_refreshed?).to be_falsy
32
+ end
33
+
34
+ it 'knows when a token needs to be refreshed' do
35
+ token = Lifen::Token.new(value: "valid_token", expires_at: Time.now.to_i + 40)
36
+
37
+ expect(token.needs_to_be_refreshed?).to be_truthy
38
+ end
39
+
40
+ it 'refreshs the token' do
41
+ token = Lifen::Token.new(value: "valid_token", expires_at: Time.now.to_i + 40)
42
+
43
+ expect(token).to receive(:refresh) do
44
+ token.expires_at = Time.now.to_i + 120
45
+ end
46
+
47
+ token.refresh_once_if_needed
48
+ end
49
+ end
50
+
51
+ end
52
+
5
53
  describe 'expired token' do
6
54
 
7
55
  let(:expired_token) { Lifen::Token.new(value: "valid_token", expires_at: Time.now.to_i - 60) }
8
56
 
9
- it 'refreshs the token' do
10
- expect(expired_token).to receive(:refresh) do
11
- expired_token.expires_at = Time.now.to_i + 60
12
- end
57
+ context ':refresh_once_if_needed' do
58
+ it 'refreshs the token' do
59
+ expect(expired_token).to receive(:refresh) do
60
+ expired_token.expires_at = Time.now.to_i + 60
61
+ end
13
62
 
14
- expired_token.refresh_once_if_needed
63
+ expired_token.refresh_once_if_needed
64
+ end
15
65
  end
16
66
 
17
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lifen
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Etienne Depaulis
@@ -186,6 +186,7 @@ files:
186
186
  - spec/cassettes/users/status/refresh/valid_token.yml
187
187
  - spec/cassettes/users/token/refresh/invalid_user_uuid.yml
188
188
  - spec/cassettes/users/token/refresh/valid_user_uuid.yml
189
+ - spec/configuration_spec.rb
189
190
  - spec/flows_spec.rb
190
191
  - spec/messages_spec.rb
191
192
  - spec/spec_helper.rb
@@ -235,6 +236,7 @@ test_files:
235
236
  - spec/cassettes/users/status/refresh/valid_token.yml
236
237
  - spec/cassettes/users/token/refresh/invalid_user_uuid.yml
237
238
  - spec/cassettes/users/token/refresh/valid_user_uuid.yml
239
+ - spec/configuration_spec.rb
238
240
  - spec/flows_spec.rb
239
241
  - spec/messages_spec.rb
240
242
  - spec/spec_helper.rb