lifen 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +3 -0
  6. data/Gemfile.lock +70 -0
  7. data/License.txt +22 -0
  8. data/README.md +76 -0
  9. data/Rakefile +7 -0
  10. data/lib/lifen.rb +19 -0
  11. data/lib/lifen/app_authenticated_client.rb +32 -0
  12. data/lib/lifen/authentication.rb +29 -0
  13. data/lib/lifen/base.rb +7 -0
  14. data/lib/lifen/client.rb +18 -0
  15. data/lib/lifen/configuration.rb +13 -0
  16. data/lib/lifen/error.rb +26 -0
  17. data/lib/lifen/flow.rb +67 -0
  18. data/lib/lifen/flows.rb +31 -0
  19. data/lib/lifen/user.rb +39 -0
  20. data/lib/lifen/user_authenticated_client.rb +38 -0
  21. data/lib/lifen/version.rb +3 -0
  22. data/lifen.gemspec +29 -0
  23. data/spec/cassettes/flows/attach_users/invalid_flow_uuid.yml +57 -0
  24. data/spec/cassettes/flows/attach_users/invalid_user_uuid.yml +57 -0
  25. data/spec/cassettes/flows/attach_users/valid.yml +57 -0
  26. data/spec/cassettes/flows/create.yml +57 -0
  27. data/spec/cassettes/flows/detach_users/valid.yml +57 -0
  28. data/spec/cassettes/flows/detach_users/valid_again.yml +57 -0
  29. data/spec/cassettes/flows/invalid_token.yml +56 -0
  30. data/spec/cassettes/flows/valid_token.yml +58 -0
  31. data/spec/cassettes/users/create/existing_user.yml +54 -0
  32. data/spec/cassettes/users/create/invalid_token.yml +54 -0
  33. data/spec/cassettes/users/create/missing_fields.yml +55 -0
  34. data/spec/cassettes/users/create/valid_attributes.yml +54 -0
  35. data/spec/cassettes/users/refresh/invalid_user_uuid.yml +55 -0
  36. data/spec/cassettes/users/refresh/valid_user_uuid.yml +54 -0
  37. data/spec/flows_spec.rb +126 -0
  38. data/spec/spec_helper.rb +24 -0
  39. data/spec/users_spec.rb +101 -0
  40. metadata +211 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 76362152176d1e5b5d7b724d62ac5d49359a5408
4
+ data.tar.gz: aae6dccb3e749871ef53af7eb48620ab35aed836
5
+ SHA512:
6
+ metadata.gz: d36a781ccc9075a58023154244365466f611c7d0623f4f43302073ea1bb4e83b5ce4ba6f0f432e0850bf5d79a820b2a279b49c9b76643dc609beb2823377f863
7
+ data.tar.gz: 6ae5c31f6ef79e11dcb952a033b0ac9766361bb5a5b0470841baeecdeb2f69ad80012f7421954fb1ef6e0ebea6711dc29f45f16e9a9fecf02f66e809d8386ec7
@@ -0,0 +1,17 @@
1
+ .DS_Store
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1 @@
1
+ 2.3.1
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,70 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lifen (0.1.0)
5
+ faraday (~> 0.9)
6
+ virtus (~> 1.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.4.0)
12
+ awesome_print (1.7.0)
13
+ axiom-types (0.1.1)
14
+ descendants_tracker (~> 0.0.4)
15
+ ice_nine (~> 0.11.0)
16
+ thread_safe (~> 0.3, >= 0.3.1)
17
+ coercible (1.0.0)
18
+ descendants_tracker (~> 0.0.1)
19
+ crack (0.4.3)
20
+ safe_yaml (~> 1.0.0)
21
+ descendants_tracker (0.0.4)
22
+ thread_safe (~> 0.3, >= 0.3.1)
23
+ diff-lcs (1.2.5)
24
+ equalizer (0.0.11)
25
+ faraday (0.9.2)
26
+ multipart-post (>= 1.2, < 3)
27
+ hashdiff (0.3.0)
28
+ ice_nine (0.11.2)
29
+ multipart-post (2.0.0)
30
+ rake (10.5.0)
31
+ rspec (3.5.0)
32
+ rspec-core (~> 3.5.0)
33
+ rspec-expectations (~> 3.5.0)
34
+ rspec-mocks (~> 3.5.0)
35
+ rspec-core (3.5.2)
36
+ rspec-support (~> 3.5.0)
37
+ rspec-expectations (3.5.0)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.5.0)
40
+ rspec-mocks (3.5.0)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.5.0)
43
+ rspec-support (3.5.0)
44
+ safe_yaml (1.0.4)
45
+ thread_safe (0.3.5)
46
+ vcr (3.0.3)
47
+ virtus (1.0.5)
48
+ axiom-types (~> 0.1)
49
+ coercible (~> 1.0)
50
+ descendants_tracker (~> 0.0, >= 0.0.3)
51
+ equalizer (~> 0.0, >= 0.0.9)
52
+ webmock (1.24.6)
53
+ addressable (>= 2.3.6)
54
+ crack (>= 0.3.2)
55
+ hashdiff
56
+
57
+ PLATFORMS
58
+ ruby
59
+
60
+ DEPENDENCIES
61
+ awesome_print
62
+ bundler (~> 1.12)
63
+ lifen!
64
+ rake (~> 10.5)
65
+ rspec (~> 3.5)
66
+ vcr (~> 3.0)
67
+ webmock (~> 1.24)
68
+
69
+ BUNDLED WITH
70
+ 1.12.5
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Honestica
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,76 @@
1
+ # Lifen
2
+
3
+ Lifen is a ruby client for [Lifen](https://www.lifen.fr/) JSON API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'lifen'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install lifen
18
+
19
+ ## Usage
20
+
21
+ ### Configuration
22
+
23
+ Lifen can be configured (ideally inside an initializer) like so:
24
+
25
+ Lifen.configure do |config|
26
+ config.site = "http://develop.lifen.fr/"
27
+ config.secret_key = "secret_key"
28
+ end
29
+
30
+ ### Managing users
31
+
32
+ user = Lifen::User.new(email: "email@domain.tld", first_name: "Jean", last_name: "Dupont")
33
+ user.create!
34
+ user.refresh_token
35
+
36
+ ### Managing flows
37
+
38
+ user = Lifen::User.new(token: "valid_token", uuid: "valid_uuid")
39
+ user.flows
40
+
41
+ flow = Lifen::Flow.new(user: user, title: "honestica Rocks !")
42
+ flow.create
43
+ flow.attach_users!(user)
44
+ flow.detach_users!(user)
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
53
+
54
+ ## License
55
+
56
+ The MIT License (MIT)
57
+
58
+ Copyright (c) 2016 Honestica
59
+
60
+ Permission is hereby granted, free of charge, to any person obtaining a copy
61
+ of this software and associated documentation files (the "Software"), to deal
62
+ in the Software without restriction, including without limitation the rights
63
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
64
+ copies of the Software, and to permit persons to whom the Software is
65
+ furnished to do so, subject to the following conditions:
66
+
67
+ The above copyright notice and this permission notice shall be included in
68
+ all copies or substantial portions of the Software.
69
+
70
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
71
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
72
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
73
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
74
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
75
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
76
+ THE SOFTWARE.
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new('spec')
6
+
7
+ task default: :spec
@@ -0,0 +1,19 @@
1
+ module Lifen
2
+
3
+ require 'virtus'
4
+ require 'faraday'
5
+
6
+ require "lifen/version"
7
+
8
+ require "lifen/error"
9
+ require 'lifen/client'
10
+ require 'lifen/user_authenticated_client'
11
+ require 'lifen/app_authenticated_client'
12
+ require 'lifen/configuration'
13
+ require "lifen/base"
14
+ require 'lifen/user'
15
+ require 'lifen/flow'
16
+ require 'lifen/flows'
17
+ require 'lifen/authentication'
18
+
19
+ end
@@ -0,0 +1,32 @@
1
+ module Lifen
2
+ class AppAuthenticatedClient < Client
3
+
4
+ def post(url, params = {})
5
+
6
+ response = faraday_client.post do |req|
7
+ req.url url
8
+
9
+ req.headers['secret_key'] = secret_key
10
+ req.headers['Content-Type'] = "application/json"
11
+
12
+ req.body = JSON.generate(params)
13
+ end
14
+
15
+ raise Error if response.status == 500
16
+ raise Error if response.status == 400
17
+ raise InvalidSecretTokenError if response.status == 401
18
+ raise UserAlreadyExistingError if response.status == 403
19
+
20
+ json = JSON.parse response.body
21
+
22
+ json
23
+ end
24
+
25
+ private
26
+
27
+ def secret_key
28
+ Lifen.configuration.secret_key
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,29 @@
1
+ module Lifen
2
+ class Authentication < Base
3
+
4
+ attribute :user, Lifen::User
5
+
6
+ attribute :uuid, String
7
+ attribute :token, String
8
+
9
+ def register!
10
+ json = client.post("authentication/api/register/third_party", {emailAddress: user.email, lastName: user.last_name, firstName: user.first_name})
11
+
12
+ self.token = json["token"]
13
+ self.uuid = json["accountUuid"]
14
+ end
15
+
16
+ def refresh_token
17
+ json = client.post("authentication/api/authenticate/third_party", {accountUuid: user.uuid})
18
+
19
+ self.token = json.first["token"]
20
+ end
21
+
22
+ private
23
+
24
+ def client
25
+ @client ||= AppAuthenticatedClient.new
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,7 @@
1
+ module Lifen
2
+ class Base
3
+
4
+ include Virtus.model
5
+
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ module Lifen
2
+ class Client
3
+
4
+ private
5
+
6
+ def faraday_client
7
+ @faraday_client ||= Faraday.new(url: site) do |faraday|
8
+ faraday.request :url_encoded # form-encode POST params
9
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
10
+ end
11
+ end
12
+
13
+ def site
14
+ Lifen.configuration.site
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ module Lifen
2
+ class Configuration
3
+ attr_accessor :site, :secret_key
4
+ end
5
+
6
+ def self.configuration
7
+ @configuration ||= Configuration.new
8
+ end
9
+
10
+ def self.configure
11
+ yield(configuration) if block_given?
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ module Lifen
2
+ class Error < StandardError
3
+ end
4
+
5
+ class UnauthorizedError < Error
6
+ end
7
+
8
+ class InvalidCredentialsError < Error
9
+ end
10
+
11
+ class InvalidParamsError < Error
12
+ end
13
+
14
+ class InvalidSecretTokenError < Error
15
+ end
16
+
17
+ class InvalidTokenError < Error
18
+ end
19
+
20
+ class UserAlreadyExistingError < Error
21
+ end
22
+
23
+ class CantRefreshTokenError < Error
24
+ end
25
+
26
+ end
@@ -0,0 +1,67 @@
1
+ module Lifen
2
+ class Flow < Base
3
+
4
+ attribute :uuid, String
5
+ attribute :title, String
6
+ attribute :user, Lifen::User
7
+ attribute :active_users, Array[Lifen::User]
8
+
9
+ def create
10
+ json = client.post("central/api/chats", {title: title})
11
+
12
+ flow = self.class.new(json.first)
13
+
14
+ self.user = user
15
+ self.uuid = flow.uuid
16
+ self.active_users = []
17
+ self.title = flow.title
18
+
19
+ self
20
+ end
21
+
22
+ def attach_users!(users)
23
+ users = Array(users)
24
+
25
+ params = users.map(&:uuid).compact.uniq
26
+
27
+ json = client.post("central/api/chats/#{uuid}/attach_users?rel=activeUsers", params)
28
+
29
+ json["activeUsers"].each do |element|
30
+ self.active_users << Lifen::User.new(element)
31
+ end
32
+
33
+ users.each do |user|
34
+ raise Lifen::Error, "User #{user.uuid} was not attached to this flow" if !active_users.map(&:uuid).include? user.uuid
35
+ end
36
+
37
+ self
38
+ end
39
+
40
+ def detach_users!(users)
41
+ users = Array(users)
42
+
43
+ params = users.map(&:uuid).compact.uniq
44
+
45
+ json = client.post("central/api/chats/#{uuid}/detach_users?rel=activeUsers", params)
46
+
47
+ self.active_users = []
48
+
49
+ json["activeUsers"].each do |element|
50
+ self.active_users << Lifen::User.new(element)
51
+ end
52
+
53
+ users.each do |user|
54
+ raise Lifen::Error, "User #{user.uuid} was not detached to this flow" if active_users.map(&:uuid).include? user.uuid
55
+ end
56
+
57
+ self
58
+ end
59
+
60
+ private
61
+
62
+ def client
63
+ @client ||= user.client
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,31 @@
1
+ module Lifen
2
+ class Flows < Base
3
+
4
+ def initialize(user)
5
+ @user = user
6
+ @collection = []
7
+ end
8
+
9
+ attr_reader :collection, :user
10
+
11
+ def all
12
+ json = client.get("central/api/chats")
13
+
14
+ json["_embedded"]["flows"].each do |element|
15
+ flow = Flow.new(element)
16
+ flow.user = user
17
+
18
+ @collection << flow
19
+ end
20
+
21
+ collection
22
+ end
23
+
24
+ private
25
+
26
+ def client
27
+ @client ||= user.client
28
+ end
29
+
30
+ end
31
+ end