Class: Assembly::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/assembly/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Assembly.config) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
# File 'lib/assembly/client.rb', line 5

def initialize(config=Assembly.config)
  @config           = config
  @on_token_refresh = nil
  build_api_adapter
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/assembly/client.rb', line 3

def config
  @config
end

Instance Method Details

#academic_yearsObject



43
44
45
# File 'lib/assembly/client.rb', line 43

def academic_years
  Assembly::AcademicYearResource.new(self)
end

#aspectsObject



55
56
57
# File 'lib/assembly/client.rb', line 55

def aspects
  Assembly::AspectResource.new(self)
end

#assessment_pointsObject



51
52
53
# File 'lib/assembly/client.rb', line 51

def assessment_points
  Assembly::AssessmentPointResource.new(self)
end

#assessmentsObject



47
48
49
# File 'lib/assembly/client.rb', line 47

def assessments
  Assembly::AssessmentResource.new(self)
end

#attendancesObject



103
104
105
# File 'lib/assembly/client.rb', line 103

def attendances
  Assembly::AttendanceResource.new(self)
end

#calendar_eventsObject



59
60
61
# File 'lib/assembly/client.rb', line 59

def calendar_events
  Assembly::CalendarEventResource.new(self)
end

#contactsObject



63
64
65
# File 'lib/assembly/client.rb', line 63

def contacts
  Assembly::ContactResource.new(self)
end

#delete(url) ⇒ Object



37
38
39
40
41
# File 'lib/assembly/client.rb', line 37

def delete(url)
  response = @api.delete url
  check_errors(response)
  response.status == 200
end

#exclusionsObject



107
108
109
# File 'lib/assembly/client.rb', line 107

def exclusions
  Assembly::ExclusionResource.new(self)
end

#facetsObject



83
84
85
# File 'lib/assembly/client.rb', line 83

def facets
  Assembly::FacetResource.new(self)
end

#get(url, params = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# 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_setsObject



67
68
69
# File 'lib/assembly/client.rb', line 67

def grade_sets
  Assembly::GradeSetResource.new(self)
end

#on_token_refresh(&blk) ⇒ Object



115
116
117
# File 'lib/assembly/client.rb', line 115

def on_token_refresh(&blk)
  @on_token_refresh = blk
end

#post(url, params = {}) ⇒ Object



25
26
27
28
29
# 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 = {}) ⇒ Object



31
32
33
34
35
# 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!Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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_groupsObject



71
72
73
# File 'lib/assembly/client.rb', line 71

def registration_groups
  Assembly::RegistrationGroupResource.new(self)
end

#resultsObject



75
76
77
# File 'lib/assembly/client.rb', line 75

def results
  Assembly::ResultResource.new(self)
end

#school_detailsObject



79
80
81
# File 'lib/assembly/client.rb', line 79

def school_details
  Assembly::SchoolDetailResource.new(self)
end

#staff_membersObject



87
88
89
# File 'lib/assembly/client.rb', line 87

def staff_members
  Assembly::StaffMemberResource.new(self)
end

#studentsObject



91
92
93
# File 'lib/assembly/client.rb', line 91

def students
  Assembly::StudentResource.new(self)
end

#subjectsObject



95
96
97
# File 'lib/assembly/client.rb', line 95

def subjects
  Assembly::SubjectResource.new(self)
end

#teaching_groupsObject



99
100
101
# File 'lib/assembly/client.rb', line 99

def teaching_groups
  Assembly::TeachingGroupResource.new(self)
end

#year_groupsObject



111
112
113
# File 'lib/assembly/client.rb', line 111

def year_groups
  Assembly::YearGroupResource.new(self)
end