Class: EdoolsSdk::Course
- Inherits:
-
EdoolsBase
- Object
- EdoolsBase
- EdoolsSdk::Course
- Defined in:
- lib/edools_sdk/course.rb
Overview
Course class
Instance Attribute Summary collapse
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#description ⇒ Object
Returns the value of attribute description.
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#forum_section_ids ⇒ Object
Returns the value of attribute forum_section_ids.
-
#id ⇒ Object
Returns the value of attribute id.
-
#image_url ⇒ Object
Returns the value of attribute image_url.
-
#library_resource ⇒ Object
Returns the value of attribute library_resource.
-
#linear_requirements ⇒ Object
Returns the value of attribute linear_requirements.
-
#name ⇒ Object
Returns the value of attribute name.
-
#path_ids ⇒ Object
Returns the value of attribute path_ids.
-
#ready ⇒ Object
Returns the value of attribute ready.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Class Method Summary collapse
-
.all ⇒ Object
Get all courses.
-
.all! ⇒ Object
Get all courses, if anything goes wrong an exception will be raised.
-
.create(props) ⇒ Object
Create a course.
-
.create!(props) ⇒ Object
Create a course, if anything goes wrong an exception will be raised.
-
.parse_json(props) ⇒ Object
Parse json to Course object.
Instance Method Summary collapse
-
#initialize ⇒ Course
constructor
A new instance of Course.
-
#save ⇒ Object
Save a course.
-
#save! ⇒ Object
Save a course, if anything goes wrong an exception will be raised.
Methods inherited from EdoolsBase
Constructor Details
#initialize ⇒ Course
Returns a new instance of Course.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/edools_sdk/course.rb', line 22 def initialize @id = nil @name = nil @description = nil @image_url = nil @duration = nil @ready = nil @path_ids = nil @forum_section_ids = nil @linear_requirements = nil @library_resource = nil @created_at = nil @updated_at = nil end |
Instance Attribute Details
#created_at ⇒ Object
Returns the value of attribute created_at.
4 5 6 |
# File 'lib/edools_sdk/course.rb', line 4 def created_at @created_at end |
#description ⇒ Object
Returns the value of attribute description.
4 5 6 |
# File 'lib/edools_sdk/course.rb', line 4 def description @description end |
#duration ⇒ Object
Returns the value of attribute duration.
4 5 6 |
# File 'lib/edools_sdk/course.rb', line 4 def duration @duration end |
#forum_section_ids ⇒ Object
Returns the value of attribute forum_section_ids.
4 5 6 |
# File 'lib/edools_sdk/course.rb', line 4 def forum_section_ids @forum_section_ids end |
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/edools_sdk/course.rb', line 4 def id @id end |
#image_url ⇒ Object
Returns the value of attribute image_url.
4 5 6 |
# File 'lib/edools_sdk/course.rb', line 4 def image_url @image_url end |
#library_resource ⇒ Object
Returns the value of attribute library_resource.
4 5 6 |
# File 'lib/edools_sdk/course.rb', line 4 def library_resource @library_resource end |
#linear_requirements ⇒ Object
Returns the value of attribute linear_requirements.
4 5 6 |
# File 'lib/edools_sdk/course.rb', line 4 def linear_requirements @linear_requirements end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/edools_sdk/course.rb', line 4 def name @name end |
#path_ids ⇒ Object
Returns the value of attribute path_ids.
4 5 6 |
# File 'lib/edools_sdk/course.rb', line 4 def path_ids @path_ids end |
#ready ⇒ Object
Returns the value of attribute ready.
4 5 6 |
# File 'lib/edools_sdk/course.rb', line 4 def ready @ready end |
#updated_at ⇒ Object
Returns the value of attribute updated_at.
4 5 6 |
# File 'lib/edools_sdk/course.rb', line 4 def updated_at @updated_at end |
Class Method Details
.all ⇒ Object
Get all courses
Example:
>> Course.all!
=> [#<EdoolsSdk::Course:0x00000001aef0c8 @id=21530, @name="test 4", @description=nil, @image_url=nil, @duration=nil, @ready=nil, @path_ids=[], @forum_section_ids=[], @linear_requirements=nil, @library_resource={"id"=>1152227, "library"=>{"id"=>572}, "library_tags"=>[], "school_products"=>[]}, @created_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>, @updated_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>>]
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/edools_sdk/course.rb', line 83 def self.all response = HTTP .headers('Authorization' => "Token token=\"#{ENV['edools_token']}\"") .get(COURSE_URL) return response if response.status >= 400 && response.status < 600 body = response.parse(:json) body['courses'].map { |c| Course.parse_json(c) } end |
.all! ⇒ Object
Get all courses, if anything goes wrong an exception will be raised
Example:
>> Course.all!
=> [#<EdoolsSdk::Course:0x00000001aef0c8 @id=21530, @name="test 4", @description=nil, @image_url=nil, @duration=nil, @ready=nil, @path_ids=[], @forum_section_ids=[], @linear_requirements=nil, @library_resource={"id"=>1152227, "library"=>{"id"=>572}, "library_tags"=>[], "school_products"=>[]}, @created_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>, @updated_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>>]
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/edools_sdk/course.rb', line 66 def self.all! response = HTTP .headers('Authorization' => "Token token=\"#{ENV['edools_token']}\"") .get(COURSE_URL) raise "invalid status code #{response.status}" if response.status >= 400 && response.status < 600 body = response.parse(:json) body['courses'].map { |c| Course.parse_json(c) } end |
.create(props) ⇒ Object
Create a course
Example:
>> Course.create("name" => "test")
=> #<EdoolsSdk::Course:0x00000001aef0c8 @id=21530, @name="test", @description=nil, @image_url=nil, @duration=nil, @ready=nil, @path_ids=[], @forum_section_ids=[], @linear_requirements=nil, @library_resource={"id"=>1152227, "library"=>{"id"=>572}, "library_tags"=>[], "school_products"=>[]}, @created_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>, @updated_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>>
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/edools_sdk/course.rb', line 159 def self.create(props) response = HTTP .headers('Authorization' => "Token token=\"#{ENV['edools_token']}\"") .post(COURSE_URL, :json => props) return response if response.status != 201 body = response.parse(:json) Course.parse_json(body) end |
.create!(props) ⇒ Object
Create a course, if anything goes wrong an exception will be raised
Example:
>> Course.create!("name" => "test")
=> #<EdoolsSdk::Course:0x00000001aef0c8 @id=21530, @name="test", @description=nil, @image_url=nil, @duration=nil, @ready=nil, @path_ids=[], @forum_section_ids=[], @linear_requirements=nil, @library_resource={"id"=>1152227, "library"=>{"id"=>572}, "library_tags"=>[], "school_products"=>[]}, @created_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>, @updated_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>>
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/edools_sdk/course.rb', line 142 def self.create!(props) response = HTTP .headers('Authorization' => "Token token=\"#{ENV['edools_token']}\"") .post(COURSE_URL, :json => props) raise "invalid status code #{response.status}" if response.status != 201 body = response.parse(:json) Course.parse_json(body) end |
.parse_json(props) ⇒ Object
Parse json to Course object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/edools_sdk/course.rb', line 38 def self.parse_json(props) course = Course.new course.id = props['id'] course.name = props['name'] course.description = props['description'] course.image_url = props['image_url'] course.duration = props['duration'] course.ready = props['ready'] course.path_ids = props['path_ids'] course.forum_section_ids = props['forum_section_ids'] course.linear_requirements = props['linear_requirements'] course.library_resource = props['library_resource'] unless props['created_at'].nil? course.created_at = Date.parse(props['created_at']) end unless props['updated_at'].nil? course.updated_at = Date.parse(props['updated_at']) end course end |
Instance Method Details
#save ⇒ Object
Save a course
Example:
>> course_instance.save
=> #<EdoolsSdk::Course:0x00000001aef0c8 @id=21530, @name="test 4", @description=nil, @image_url=nil, @duration=nil, @ready=nil, @path_ids=[], @forum_section_ids=[], @linear_requirements=nil, @library_resource={"id"=>1152227, "library"=>{"id"=>572}, "library_tags"=>[], "school_products"=>[]}, @created_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>, @updated_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>>
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/edools_sdk/course.rb', line 121 def save response = HTTP .headers('Authorization' => "Token token=\"#{ENV['edools_token']}\"") .post(COURSE_URL, :json => self.to_h) return response if response.status != 201 body = response.parse(:json) course_saved = Course.parse_json(body) change_values(course_saved) course_saved end |
#save! ⇒ Object
Save a course, if anything goes wrong an exception will be raised
Example:
>> course_instance.save!
=> #<EdoolsSdk::Course:0x00000001aef0c8 @id=21530, @name="test 4", @description=nil, @image_url=nil, @duration=nil, @ready=nil, @path_ids=[], @forum_section_ids=[], @linear_requirements=nil, @library_resource={"id"=>1152227, "library"=>{"id"=>572}, "library_tags"=>[], "school_products"=>[]}, @created_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>, @updated_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>>
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/edools_sdk/course.rb', line 100 def save! response = HTTP .headers('Authorization' => "Token token=\"#{ENV['edools_token']}\"") .post(COURSE_URL, :json => self.to_h) raise "invalid status code #{response.status}" if response.status != 201 body = response.parse(:json) course_saved = Course.parse_json(body) change_values(course_saved) course_saved end |