diff options
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/api/v1/api_key.rb | 22 | ||||
| -rw-r--r-- | app/models/organisation.rb | 2 | ||||
| -rw-r--r-- | app/models/user.rb | 2 |
3 files changed, 16 insertions, 10 deletions
diff --git a/app/models/api/v1/api_key.rb b/app/models/api/v1/api_key.rb index 7390db232..e1a7ab5a4 100644 --- a/app/models/api/v1/api_key.rb +++ b/app/models/api/v1/api_key.rb @@ -3,9 +3,20 @@ module Api class ApiKey < ::ActiveRecord::Base before_create :generate_access_token belongs_to :referential, :class_name => '::Referential' + validates_presence_of :referential - def self.model_name - ActiveModel::Name.new self, Api::V1, self.name.demodulize + class << self + def from(referential, name:) + find_or_create_by!(name: name, referential: referential) + end + def model_name + ActiveModel::Name.new Api::V1, self.name.demodulize + end + def referential_from_token(token) + array = token.split('-') + return nil unless array.size==2 + ::Referential.find( array.first) + end end def eql?(other) @@ -13,16 +24,11 @@ module Api other.token == self.token end - def self.referential_from_token(token) - array = token.split('-') - return nil unless array.size==2 - ::Referential.find( array.first) - end private def generate_access_token begin - self.token = "#{referential.id}-#{SecureRandom.hex}" + self.token = "#{referential_id}-#{SecureRandom.hex}" end while self.class.exists?(:token => self.token) end end diff --git a/app/models/organisation.rb b/app/models/organisation.rb index a3c5da1af..7e571e78d 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -27,7 +27,7 @@ class Organisation < ActiveRecord::Base conf = Rails.application.config.try(:stif_portail_api) raise 'Rails.application.config.stif_portail_api configuration is not defined' unless conf - AF83::HTTPFetcher.get_resource( + HTTPService.get_resource( host: conf[:url], path: '/api/v1/organizations', parse_json: true, diff --git a/app/models/user.rb b/app/models/user.rb index fbffd96be..1dc5975e1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -66,7 +66,7 @@ class User < ActiveRecord::Base conf = Rails.application.config.try(:stif_portail_api) raise 'Rails.application.config.stif_portail_api settings is not defined' unless conf - AF83::HTTPFetcher.get_resource( + HTTPService.get_resource( host: conf[:url], path: '/api/v1/users', parse_json: true, |
