class Roqua::RomApi::Sessions::BasicAuthSession

Attributes

password[R]
rom_host[R]
username[R]

Public Class Methods

new(rom_host: ENV['ROM_SITE'], username: ENV['ROM_BASICAUTH_ID'], password: ENV['ROM_BASICAUTH_SECRET']) click to toggle source
# File lib/roqua/rom_api/sessions/basic_auth_session.rb, line 9
def initialize(rom_host: ENV['ROM_SITE'],
               username: ENV['ROM_BASICAUTH_ID'],
               password: ENV['ROM_BASICAUTH_SECRET'])
  @rom_host = rom_host
  @username = username
  @password = password
end

Public Instance Methods

delete(path, params = {}) click to toggle source
# File lib/roqua/rom_api/sessions/basic_auth_session.rb, line 43
def delete(path, params = {})
  perform_request_or_fail do
    HTTParty.delete(full_url_for(path),
                    query: params,
                    basic_auth: basic_auth)
  end
end
get(path, params = {}) click to toggle source
# File lib/roqua/rom_api/sessions/basic_auth_session.rb, line 17
def get(path, params = {})
  perform_request_or_fail do
    HTTParty.get(full_url_for(path),
                 query: params,
                 basic_auth: basic_auth)
  end
end
patch(path, params = {}) click to toggle source
# File lib/roqua/rom_api/sessions/basic_auth_session.rb, line 34
def patch(path, params = {})
  perform_request_or_fail do
    HTTParty.patch(full_url_for(path),
                   headers: {'Content-Type' => 'application/json'},
                   body: params.to_json,
                   basic_auth: basic_auth)
  end
end
post(path, params = {}) click to toggle source
# File lib/roqua/rom_api/sessions/basic_auth_session.rb, line 25
def post(path, params = {})
  perform_request_or_fail do
    HTTParty.post(full_url_for(path),
                  headers: {'Content-Type' => 'application/json'},
                  body: params.to_json,
                  basic_auth: basic_auth)
  end
end

Private Instance Methods

access_denied(response) click to toggle source
# File lib/roqua/rom_api/sessions/basic_auth_session.rb, line 69
def access_denied(response)
  if response.headers['WWW-Authenticate']
    fail NoSession
  else
    fail Unauthorized
  end
end
api_base() click to toggle source
# File lib/roqua/rom_api/sessions/basic_auth_session.rb, line 81
def api_base
  '/api/v1'
end
basic_auth() click to toggle source
# File lib/roqua/rom_api/sessions/basic_auth_session.rb, line 85
def basic_auth
  {username: username, password: password}
end
full_url_for(path) click to toggle source
# File lib/roqua/rom_api/sessions/basic_auth_session.rb, line 77
def full_url_for(path)
  rom_host + api_base + path + '.json'
end
perform_request_or_fail() { || ... } click to toggle source
# File lib/roqua/rom_api/sessions/basic_auth_session.rb, line 53
def perform_request_or_fail(&block)
  response = yield
  case response.code
  when 200..299, 422
    response
  when 401
    access_denied(response)
  else
    msg = response.parsed_response
    if msg.is_a? Hash
      msg = msg.fetch('error', nil)
    end
    fail msg || "Received HTTP response code #{response.code}!"
  end
end