class Fizzy::Api::Sessions::BasicAuthSession

Attributes

fizzy_url[R]
password[R]
username[R]

Public Class Methods

new(fizzy_url: ENV['FIZZY_URL'], username: ENV['FIZZY_BASICAUTH_ID'], password: ENV['FIZZY_BASICAUTH_SECRET']) click to toggle source
# File lib/fizzy/api/sessions/basic_auth_session.rb, line 11
def initialize(fizzy_url: ENV['FIZZY_URL'],
               username: ENV['FIZZY_BASICAUTH_ID'],
               password: ENV['FIZZY_BASICAUTH_SECRET'])
  @fizzy_url = fizzy_url
  @username = username
  @password = password
end

Public Instance Methods

delete(path, params = {}) click to toggle source
# File lib/fizzy/api/sessions/basic_auth_session.rb, line 36
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/fizzy/api/sessions/basic_auth_session.rb, line 19
def get(path, params = {})
  perform_request_or_fail do
    HTTParty.get(full_url_for(path),
                 query: params,
                 basic_auth: basic_auth)
  end
end
post(path, params = {}) click to toggle source
# File lib/fizzy/api/sessions/basic_auth_session.rb, line 27
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/fizzy/api/sessions/basic_auth_session.rb, line 58
def access_denied(response)
  raise NoSession if response.headers['WWW-Authenticate']
  raise Unauthorized
end
api_base() click to toggle source
# File lib/fizzy/api/sessions/basic_auth_session.rb, line 67
def api_base
  '/api/v1'
end
basic_auth() click to toggle source
# File lib/fizzy/api/sessions/basic_auth_session.rb, line 71
def basic_auth
  { username: username, password: password }
end
full_url_for(path) click to toggle source
# File lib/fizzy/api/sessions/basic_auth_session.rb, line 63
def full_url_for(path)
  fizzy_url + api_base + path
end
perform_request_or_fail() { || ... } click to toggle source
# File lib/fizzy/api/sessions/basic_auth_session.rb, line 46
def perform_request_or_fail(&_block)
  response = yield
  case response.code
  when 401
    access_denied(response)
  when 500
    raise((response.parsed_response || "Received HTTP response code #{response.code}!").to_s)
  else
    response
  end
end