# Lifen Lifen is a ruby client for [Lifen](https://www.lifen.fr/) JSON API. ## Installation Add this line to your application's Gemfile: ```ruby 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: ```ruby 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 ### Managing users ```ruby user = Lifen::User.new(email: "email@domain.tld", 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: ```ruby 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 ```ruby 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: ```ruby 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 ```ruby 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 ```ruby message = Lifen::Message.new(flow: flow, content: "Hello World !") message.create ``` ### Communications (FHIR) ```ruby sender = Lifen::User.find_by_rpps("899900018483") recipient = Lifen::User.find_by_rpps("899900018484") channel = recipient.channels.first category = Lifen::Category.new(code: "MEDICAL_REPORT") # default case, optionnal element attachment = Lifen::Attachment.new(title: "Test document", path: "path/to/file") patient = Lifen::Patient.new(first_name: "Jean", last_name: "Dupond", birthdate: Date.new(2000,1,1)) communication = Lifen::Communication.new(sender: sender, recipient: recipient, channel: channel, attachment: attachment, patient: patient, category: category) communication.send communication = Lifen::Communication.find("valid-communication-uuid") communication.status ``` ### Custom Addresses management ```ruby recipient = Lifen::User.new(uuid: "valid-user-uuid") channel = recipient.create_channel(type: "address", lines: ["39 rue Aboukir"], city: "Paris", postal_code: "75002", country: "France") ``` ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request ## 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.