Class: Coursera::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/coursera/base.rb

Direct Known Subclasses

Client, Course

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
# File 'lib/coursera/base.rb', line 9

def initialize(json)
  @parsed = json.is_a?(Hash) ? json : JSON.parse(json)

  @parsed.each do |k, v|
    define_singleton_method(k) { v }
  end
end

Class Method Details

.batch_request(route, attrs = {}) ⇒ Object



24
25
26
27
28
29
# File 'lib/coursera/base.rb', line 24

def self.batch_request(route, attrs = {})
  request = self.get_elements(route, {query: attrs})
  results = []
  (request ||= []).each { |obj| results << new(obj) }
  results
end

.get_elements(route, attrs = {}) ⇒ Object



31
32
33
34
35
# File 'lib/coursera/base.rb', line 31

def self.get_elements(route, attrs = {})
  self.get(route, attrs)["elements"]
rescue
  nil
end

.prepare_attributes(attrs) ⇒ Object



17
18
19
20
21
22
# File 'lib/coursera/base.rb', line 17

def self.prepare_attributes(attrs)
  attrs.each do |key, value|
    attrs[key] = attrs[key].map(&:to_s).join(",") if attrs[key].kind_of?(Array)
  end
  attrs
end