class Assembly::Client

Attributes

config[R]

Public Class Methods

new(config=Assembly.config) click to toggle source
# File lib/assembly/client.rb, line 5
def initialize(config=Assembly.config)
  @config           = config
  @on_token_refresh = nil
  build_api_adapter
end

Public Instance Methods

academic_years() click to toggle source
# File lib/assembly/client.rb, line 43
def academic_years
  Assembly::AcademicYearResource.new(self)
end
aspects() click to toggle source
# File lib/assembly/client.rb, line 55
def aspects
  Assembly::AspectResource.new(self)
end
assessment_points() click to toggle source
# File lib/assembly/client.rb, line 51
def assessment_points
  Assembly::AssessmentPointResource.new(self)
end
assessments() click to toggle source
# File lib/assembly/client.rb, line 47
def assessments
  Assembly::AssessmentResource.new(self)
end
attendances() click to toggle source
# File lib/assembly/client.rb, line 103
def attendances
  Assembly::AttendanceResource.new(self)
end
calendar_events() click to toggle source
# File lib/assembly/client.rb, line 59
def calendar_events
  Assembly::CalendarEventResource.new(self)
end
contacts() click to toggle source
# File lib/assembly/client.rb, line 63
def contacts
  Assembly::ContactResource.new(self)
end
delete(url) click to toggle source
# File lib/assembly/client.rb, line 37
def delete(url)
  response = @api.delete url
  check_errors(response)
  response.status == 200
end
exclusions() click to toggle source
# File lib/assembly/client.rb, line 107
def exclusions
  Assembly::ExclusionResource.new(self)
end
facets() click to toggle source
# File lib/assembly/client.rb, line 83
def facets
  Assembly::FacetResource.new(self)
end
get(url, params={}) click to toggle source
# File lib/assembly/client.rb, line 11
def get(url, params={})
  if_modified_since = params.delete(:since)
  headers = {}
  headers.merge!({ 'IF_MODIFIED_SINCE': if_modified_since }) if if_modified_since

  response = @api.get(url, params, headers)
  ok       = check_errors(response)
  if ok
    response.body
  else
    get(url, params)
  end
end
grade_sets() click to toggle source
# File lib/assembly/client.rb, line 67
def grade_sets
  Assembly::GradeSetResource.new(self)
end
on_token_refresh(&blk) click to toggle source
# File lib/assembly/client.rb, line 115
def on_token_refresh(&blk)
  @on_token_refresh = blk
end
post(url, params={}) click to toggle source
# File lib/assembly/client.rb, line 25
def post(url, params={})
  response = @api.post url, params.to_json
  check_errors(response)
  response.body
end
put(url, params={}) click to toggle source
# File lib/assembly/client.rb, line 31
def put(url, params={})
  response = @api.put url, params.to_json
  check_errors(response)
  response.body
end
refresh_token!() click to toggle source
# File lib/assembly/client.rb, line 119
def refresh_token!
  return false unless config.client_id && config.client_secret && config.refresh_token
  refresh_api = Faraday.new(:url => config.auth_host) do |faraday|
    faraday.request :url_encoded
    faraday.response :json
    faraday.adapter Faraday.default_adapter
  end
  refresh_api.headers[:accept]        = "application/vnd.assembly+json; version=#{config.api_version}"
  refresh_api.headers[:authorization] = refresh_api.basic_auth(config.client_id, config.client_secret)

  response = refresh_api.post('/oauth/token', {
    grant_type:    'refresh_token',
    refresh_token: config.refresh_token
  })
  return false unless check_errors(response)
  config.token = response.body[:access_token]
  build_api_adapter
  @on_token_refresh.call(response.body) if @on_token_refresh
  true
end
registration_groups() click to toggle source
# File lib/assembly/client.rb, line 71
def registration_groups
  Assembly::RegistrationGroupResource.new(self)
end
results() click to toggle source
# File lib/assembly/client.rb, line 75
def results
  Assembly::ResultResource.new(self)
end
school_details() click to toggle source
# File lib/assembly/client.rb, line 79
def school_details
  Assembly::SchoolDetailResource.new(self)
end
staff_members() click to toggle source
# File lib/assembly/client.rb, line 87
def staff_members
  Assembly::StaffMemberResource.new(self)
end
students() click to toggle source
# File lib/assembly/client.rb, line 91
def students
  Assembly::StudentResource.new(self)
end
subjects() click to toggle source
# File lib/assembly/client.rb, line 95
def subjects
  Assembly::SubjectResource.new(self)
end
teaching_groups() click to toggle source
# File lib/assembly/client.rb, line 99
def teaching_groups
  Assembly::TeachingGroupResource.new(self)
end
year_groups() click to toggle source
# File lib/assembly/client.rb, line 111
def year_groups
  Assembly::YearGroupResource.new(self)
end

Private Instance Methods

build_api_adapter() click to toggle source
# File lib/assembly/client.rb, line 167
def build_api_adapter
  @api = Faraday.new(:url => config.host) do |faraday|
    faraday.request :json
    faraday.request :assembly_oauth2, config.token
    faraday.response :json
    faraday.adapter Faraday.default_adapter
  end
  @api.headers[:accept] = "application/vnd.assembly+json; version=#{config.api_version}"
end
check_errors(response) click to toggle source
# File lib/assembly/client.rb, line 142
def check_errors(response)
  status = response.status.to_i
  case status
    when 401
      handle_unauthorized_response(response)
      return false
    when 404
      raise Assembly::NotFoundError.new(response)
    when 422
      raise Assembly::ValidationError.new(response)
    when 429
      raise Assembly::TooManyRequestsError.new(response)
    when 500
      raise Assembly::ServerError.new(response)
  end
  true
end
handle_unauthorized_response(response) click to toggle source
# File lib/assembly/client.rb, line 160
def handle_unauthorized_response(response)
  if response.body[:error] != 'invalid_token'
    raise Assembly::UnauthorizedError.new(response)
  end
  raise Assembly::UnauthorizedError.new(response) unless refresh_token!
end