class RailsSso::Client

Attributes

connection[R]
token[R]

Public Class Methods

build(url, &block) click to toggle source
# File lib/rails_sso/client.rb, line 6
def self.build(url, &block)
  adapter = Faraday.new(url, &block)
  new(adapter)
end
build_fake(url) click to toggle source
# File lib/rails_sso/client.rb, line 25
def self.build_fake(url)
  build(url) do |conn|
    conn.adapter :test do |stub|
      RailsSso.config.profile_mocks.each do |token, profile|
        headers = {
          "Content-Type" => "application/json",
          "Authorization" => "Bearer #{token}"
        }

        stub.get(RailsSso.config.provider_profile_path, headers) do |env|
          if profile.nil?
            [401, { "Content-Type" => "application/json" }, ""]
          else
            [200, { "Content-Type" => "application/json" }, profile.to_json]
          end
        end
      end
    end
  end
end
build_real(url) click to toggle source
# File lib/rails_sso/client.rb, line 11
def self.build_real(url)
  build(url) do |conn|
    if RailsSso.config.use_cache
      conn.use :http_cache,
        store: Rails.cache,
        logger: Rails.logger,
        serializer: Marshal,
        shared_cache: false
    end

    conn.adapter Faraday.default_adapter
  end
end
new(adapter) click to toggle source
# File lib/rails_sso/client.rb, line 46
def initialize(adapter)
  @connection = adapter
end

Public Instance Methods

delete(url, params = {}) click to toggle source
# File lib/rails_sso/client.rb, line 68
def delete(url, params = {})
  request(:delete, url, params)
end
get(url, params = {}) click to toggle source
# File lib/rails_sso/client.rb, line 56
def get(url, params = {})
  request(:get, url, params)
end
patch(url, params = {}) click to toggle source
# File lib/rails_sso/client.rb, line 72
def patch(url, params = {})
  request(:patch, url, params)
end
post(url, params = {}) click to toggle source
# File lib/rails_sso/client.rb, line 60
def post(url, params = {})
  request(:post, url, params)
end
put(url, params = {}) click to toggle source
# File lib/rails_sso/client.rb, line 64
def put(url, params = {})
  request(:put, url, params)
end
token!(token) click to toggle source
# File lib/rails_sso/client.rb, line 50
def token!(token)
  @token = token

  self
end

Private Instance Methods

request(verb, url, params = {}) click to toggle source
# File lib/rails_sso/client.rb, line 80
def request(verb, url, params = {})
  connection.send(verb) do |req|
    req.headers["Authorization"] = "Bearer #{token}"
    req.headers["Content-Type"] = "application/json"

    req.url(url)
    req.body = params.to_json
  end
end