class Bridge::API::Wrapper

Attributes

auth_token[R]
domain[R]

Public Class Methods

new(auth_token, domain) click to toggle source
# File lib/bridge/api/wrapper.rb, line 14
def initialize(auth_token, domain)
  @auth_token = auth_token
  @domain = domain
end

Public Instance Methods

checkpoints(id = nil) click to toggle source
# File lib/bridge/api/wrapper.rb, line 71
def checkpoints(id = nil)
  path = if id
           "/api/author/tasks/#{id}"
         else
           '/api/author/tasks'
  end

  request(
    http_method: :get,
    endpoint: path
  )
end
courses(id = nil) click to toggle source
# File lib/bridge/api/wrapper.rb, line 32
def courses(id = nil)
  path = if id
           "/api/author/course_templates/#{id}"
         else
           '/api/author/course_templates'
  end

  request(
    http_method: :get,
    endpoint: path
  )
end
live_trainings(id = nil) click to toggle source
# File lib/bridge/api/wrapper.rb, line 45
def live_trainings(id = nil)
  path = if id
           "/api/author/live_courses/#{id}"
         else
           '/api/author/live_courses'
  end

  request(
    http_method: :get,
    endpoint: path
  )
end
programs(id = nil) click to toggle source
# File lib/bridge/api/wrapper.rb, line 58
def programs(id = nil)
  path = if id
           "/api/author/programs/#{id}"
         else
           '/api/author/programs'
  end

  request(
    http_method: :get,
    endpoint: path
  )
end
users(id = nil) click to toggle source
# File lib/bridge/api/wrapper.rb, line 19
def users(id = nil)
  path = if id
           "/api/author/users/#{id}"
         else
           '/api/author/users'
  end

  request(
    http_method: :get,
    endpoint: path
  )
end

Private Instance Methods

client() click to toggle source
# File lib/bridge/api/wrapper.rb, line 86
def client
  @_client ||= Faraday.new("https://#{domain}.bridgeapp.com") do |client|
    client.request :url_encoded
    client.adapter Faraday.default_adapter
    client.headers['Authorization'] = auth_token
  end
end
error_class(response) click to toggle source
# File lib/bridge/api/wrapper.rb, line 105
def error_class(response)
  case response.status
  when HTTP_BAD_REQUEST_CODE
    error = { error: :HTTP_BAD_REQUEST_CODE }
    p error
  when HTTP_UNAUTHORIZED_CODE
    error = { error: :HTTP_UNAUTHORIZED_CODE }
    p error
  when HTTP_FORBIDDEN_CODE
    error = { error: :HTTP_FORBIDDEN_CODE }
    p error
  when HTTP_NOT_FOUND_CODE
    error = { error: :HTTP_NOT_FOUND_CODE }
    p error
  when HTTP_UNPROCESSABLE_ENTITY_CODE
    error = { error: :HTTP_UNPROCESSABLE_ENTITY_CODE }
    p error
  else
    error = { error: :UNKNOWN_ERROR }
    p error
  end
end
request(http_method:, endpoint:, params: {}) click to toggle source
# File lib/bridge/api/wrapper.rb, line 94
def request(http_method:, endpoint:, params: {})
  response = client.public_send(http_method, endpoint, params)
  parsed_response = Oj.load(response.body)

  if response_successful?(response)
    puts parsed_response
  else
    raise error_class(response), "code #{response.status}, response: #{response.body}"
  end
end
response_successful?(response) click to toggle source
# File lib/bridge/api/wrapper.rb, line 128
def response_successful?(response)
  response.status == HTTP_OK_CODE
end