Class: TIES::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ties/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

options should contain

:api_key
:secret_key
:district_number


10
11
12
13
14
# File 'lib/ties/base.rb', line 10

def initialize(options)
  self.api_key, self.secret_key = options["api_key"], options["secret_key"]
  self.district_number = options["district_number"]
  self.endpoint = options["endpoint"] || TIES::ENDPOINT
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



3
4
5
# File 'lib/ties/base.rb', line 3

def api_key
  @api_key
end

#district_numberObject

Returns the value of attribute district_number.



3
4
5
# File 'lib/ties/base.rb', line 3

def district_number
  @district_number
end

#endpointObject

Returns the value of attribute endpoint.



3
4
5
# File 'lib/ties/base.rb', line 3

def endpoint
  @endpoint
end

#secret_keyObject

Returns the value of attribute secret_key.



3
4
5
# File 'lib/ties/base.rb', line 3

def secret_key
  @secret_key
end

#total_countObject

Returns the value of attribute total_count.



4
5
6
# File 'lib/ties/base.rb', line 4

def total_count
  @total_count
end

#total_pagesObject

Returns the value of attribute total_pages.



4
5
6
# File 'lib/ties/base.rb', line 4

def total_pages
  @total_pages
end

Instance Method Details

#all(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/ties/base.rb', line 26

def all(options = {})
  @results = []
  page = 1
  begin
    @results.concat self.get(page, options)
    page += 1
  end until self.total_pages.nil? || self.total_pages <= page
  @results
end

#authentication(http_method, time, request_uri) ⇒ Object



54
55
56
57
# File 'lib/ties/base.rb', line 54

def authentication(http_method, time, request_uri)
  data = [http_method, time, request_uri].join('\n')
  'TIES ' + [api_key, Base64.encode64(OpenSSL::HMAC.digest('sha1', self.secret_key, data))].join(':')
end

#classesObject



62
# File 'lib/ties/base.rb', line 62

def classes(); @classes ||= TIES::Schedule::Classes.new(self); end

#each(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/ties/base.rb', line 16

def each(options = {})
  page = 1
  begin
    self.get(page, options).each do |element|
      yield(element)
    end
    page += 1
  end until self.total_pages.nil? || self.total_pages <= page
end

#reimbursementsObject



64
# File 'lib/ties/base.rb', line 64

def reimbursements(); @reimbursements ||= TIES::MyView::Reimbursements.new(self); end

#requested_classesObject



63
# File 'lib/ties/base.rb', line 63

def requested_classes(); @requested_classes ||= TIES::Schedule::RequestedClasses.new(self); end

#schoolsObject



59
# File 'lib/ties/base.rb', line 59

def schools(); @schools ||= TIES::Schools.new(self); end

#send_request(uri, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ties/base.rb', line 36

def send_request(uri, options = {})
  if self.api_key.nil? || self.secret_key.nil? || self.district_number.nil?
    raise 'not enough data to send request'
  end
  result = request_over_http(options, endpoint, uri)
  if !result
    raise 'Request to TIES api "%s" failed' % uri
  end
  case result.code.to_i
  when 400
    return false
  when 200
    return JSON.parse(result.body)
  else
    raise 'unknown error status code %s for uri "%s"' % [result.code, uri]
  end
end

#studentsObject



60
# File 'lib/ties/base.rb', line 60

def students(); @students ||= TIES::Students.new(self); end

#teachersObject



61
# File 'lib/ties/base.rb', line 61

def teachers(); @teachers ||= TIES::Teachers.new(self); end