Lifen

Lifen is a Ruby client for Lifen JSON API.

Installation

Add this line to your application's Gemfile:

gem 'lifen'

And then execute:

$ bundle

Or install it yourself as:

$ gem install lifen

Usage

Configuration

Lifen can be configured (ideally inside an initializer) like so:

Lifen.configure do |config|
  config.site                      = "https://develop.lifen.fr/"
  config.application_access_token  = "application_access_token"

  # optionnal
  config.proxy_url                 = "http://my.proxy.fr/"
  config.expiration_margin         = 60 # in seconds, default: 0
end

Optionnal configuration:

  • proxy_url: enables you to route all your requests via a proxy
  • expiration_margin: by default a token is considered valid until the expiration time but you can add a security expiration margin

Internal

Managing users

user = Lifen::User.new(email: "[email protected]", first_name: "Jean", last_name: "Dupont")
user.create
user.token.refresh
user.status.reload

other_user = Lifen::User.find("user_uuid")
other_user.first_name = "Marc"
other_user.save
other_user.reload

Creating a user in a threadsafe way

Assuming the lifen uuid is stored on the User model as lifen_uuid, you can simply change the create method to became threadsafe:

lifen_user.create(
  persisted_lifen_uuid: ->(internal_user) {
    user.reload

    current_uuid = user.lifen_uuid

    if current_uuid.blank?
      nil
    else
      current_uuid
    end
  },
  save_to_db: ->(lifen_user) {
    user.lifen_uuid = lifen_user.uuid
    user.save!
  }
)

Managing users' settings

user.settings
user.settings.reload
user.settings.push_notifications
user.settings.push_notifications = false
user.settings.save

Refreshing a token in a threadsafe way

Depending on the OAuth provider strategy, the token refresh strategy has to be adapted. In our case, if token are stored in a database, you can ask the client to reload the token from your database value before performing a real refresh:

token.refresh_once_if_needed do |token|
  current_user.reload

  token.value = current_user.lifen_token
  token.expires_at = current_user.lifen_token_expires_at.to_i
end

Managing flows

user = Lifen::User.new(uuid: "valid_uuid")
user.flows

flow = Lifen::Flow.new(user: user, title: "Honestica Rocks !")
flow.create
flow.attach_users(user)
flow.detach_users(user)

# alternate strategy
flow = Lifen::Flow.new(user: user, title: "honestica Rocks !", users: [user_1, user_2])
flow.create

Managing messages

message = Lifen::Message.new(flow: flow, content: "Hello World !")
message.create

Deploying to Rubygems

Once the new version is validated, the deployment follows those steps :

  1. update lib/lifen/version.rb
  2. run bundle install to update Gemfile.lock
  3. update CHANGELOG.md with the additional features of the new version
  4. commit changes to Github
  5. build the gem using gem build lifen.gemspec
  6. publish the gem using gem publish lifen-X.X.X.gem
  7. run bundle update lifen in related projects :)

License

The MIT License (MIT)

Copyright (c) 2016-2017 Honestica

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.