module Bliss::Client

Constants

ADAPTER
API_VERSION
TOKEN_PATH
VERSION
ValidationError

Public Instance Methods

api_url() click to toggle source
# File lib/bliss/client.rb, line 69
def api_url
  "#{server}/api/#{API_VERSION}"
end
client_id() click to toggle source
# File lib/bliss/client.rb, line 77
def client_id
  ENV['BLISS_CLIENT_ID']
end
client_secret() click to toggle source
# File lib/bliss/client.rb, line 81
def client_secret
  ENV['BLISS_CLIENT_SECRET']
end
connection() click to toggle source
# File lib/bliss/client.rb, line 29
def connection
  @connection = nil if test?

  @connection ||= Faraday.new(url: api_url) do |f|
    f.request :oauth2, token unless skip_auth?
    f.request :json
    f.response :json, content_type: /\bjson$/
    if debug?
      f.response :logger
    end
    f.adapter ADAPTER
  end
end
debug?() click to toggle source
# File lib/bliss/client.rb, line 85
def debug?
  ENV['BLISS_CLIENT_DEBUG']
end
server() click to toggle source
# File lib/bliss/client.rb, line 73
def server
  ENV['BLISS_SERVER']
end
skip_auth?() click to toggle source
# File lib/bliss/client.rb, line 93
def skip_auth?
  ENV['BLISS_CLIENT_SKIP_AUTH'] == 'true'
end
test?() click to toggle source
# File lib/bliss/client.rb, line 89
def test?
  ENV['BLISS_CLIENT_ENV'] == 'test'
end
token() click to toggle source
# File lib/bliss/client.rb, line 43
def token
  # Fetch the token every time when testing, so that VCR can expect this
  # request to happen.
  @token = nil if test?

  @token ||= begin
    connection = Faraday.new(url: server) do |f|
      f.request :json
      f.response :json, content_type: /\bjson$/
      if debug?
        f.response :logger
      end
      f.adapter ADAPTER
    end

    response = connection.post(
      TOKEN_PATH,
      grant_type: 'client_credentials',
      client_id: client_id,
      client_secret: client_secret
    )

    response.body.fetch('access_token')
  end
end