Sha256: b820aa005773909ab85c069793a102167487044d292ce23dec5ec3789f904662
Contents?: true
Size: 1.49 KB
Versions: 1
Compression:
Stored size: 1.49 KB
Contents
require "ostruct" require "json" require "faraday" require "redis" require 'byebug' require_relative "authz_jurnal_client/version" module AuthzJurnalClient class Error < StandardError; end class UserRoles class << self attr_accessor :redis, :domain, :authorization, :expiry def config(opts = {}) @authorization = opts[:authorization] @headers = {"Authorization" => @authorization} @domain = opts[:domain] || 'http://localhost:3000' redis_path = opts[:redis] || 'redis://localhost:6379' @redis = Redis.new(url: redis_path) @expiry = opts[:expiry] || 3600 end def call(id, cid) if @redis cached = @redis.get(make_key(id, cid)) return get_roles(cached) if cached end response = backend_request(id, cid) return unless response.success? @redis&.setex(make_key(id, cid), @expiry, response.body) get_roles(response.body) rescue Redis::CannotConnectError response = backend_request(id, cid) get_roles(response.body) end def backend_request(id, cid) conn = Faraday.new("#{@domain}/api/v1/users/#{id}") do |f| f.adapter :net_http end conn.get('roles', { company_id: cid }, @headers) end def make_key(id, cid) "manage_roles:#{id}-#{cid}" end def get_roles(j) data = eval(j) JSON.parse( data.to_json, object_class: OpenStruct).data.roles end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
authz_jurnal_client-0.0.5 | lib/authz_jurnal_client.rb |