Class: EdoolsSdk::Product

Inherits:
EdoolsBase show all
Defined in:
lib/edools_sdk/product.rb

Overview

Product class

Constant Summary collapse

PRODUCT_URL_WITHOUT_SCHOOL =
'https://core.myedools.info/api/school_products'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EdoolsBase

#to_h

Constructor Details

#initializeProduct

Returns a new instance of Product.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/edools_sdk/product.rb', line 41

def initialize
  @id = nil
  @title = nil
  @description = nil
  @subtitle = nil
  @logo = nil
  @video_url = nil
  @video_title = nil
  @video_description = nil
  @published = nil
  @hidden = nil
  @restricted = nil
  @certification = nil
  @classes_auto_generation = nil
  @certification_min_progress = nil
  @meta_title = nil
  @meta_description = nil
  @meta_keys = nil
  @available_time_type = nil
  @available_time_length = nil
  @available_time_unit = nil
  @expire_date = nil
  @library_resource_id = nil
  @max_attendance_type = nil
  @max_attendance_length = nil
  @allowed_emails = nil
  @class_teacher_ids = nil
  @category_ids = nil
  @gallery_media_ids = nil
  @created_at = nil
  @updated_at = nil
end

Instance Attribute Details

#allowed_emailsObject

Returns the value of attribute allowed_emails.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def allowed_emails
  @allowed_emails
end

#available_time_lengthObject

Returns the value of attribute available_time_length.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def available_time_length
  @available_time_length
end

#available_time_typeObject

Returns the value of attribute available_time_type.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def available_time_type
  @available_time_type
end

#available_time_unitObject

Returns the value of attribute available_time_unit.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def available_time_unit
  @available_time_unit
end

#category_idsObject

Returns the value of attribute category_ids.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def category_ids
  @category_ids
end

#certificationObject

Returns the value of attribute certification.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def certification
  @certification
end

#certification_min_progressObject

Returns the value of attribute certification_min_progress.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def certification_min_progress
  @certification_min_progress
end

#class_teacher_idsObject

Returns the value of attribute class_teacher_ids.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def class_teacher_ids
  @class_teacher_ids
end

#classes_auto_generationObject

Returns the value of attribute classes_auto_generation.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def classes_auto_generation
  @classes_auto_generation
end

#created_atObject

Returns the value of attribute created_at.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def created_at
  @created_at
end

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def description
  @description
end

#expire_dateObject

Returns the value of attribute expire_date.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def expire_date
  @expire_date
end

Returns the value of attribute gallery_media_ids.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def gallery_media_ids
  @gallery_media_ids
end

#hiddenObject

Returns the value of attribute hidden.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def hidden
  @hidden
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def id
  @id
end

#library_resource_idObject

Returns the value of attribute library_resource_id.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def library_resource_id
  @library_resource_id
end

#logoObject

Returns the value of attribute logo.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def 
  @logo
end

#max_attendance_lengthObject

Returns the value of attribute max_attendance_length.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def max_attendance_length
  @max_attendance_length
end

#max_attendance_typeObject

Returns the value of attribute max_attendance_type.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def max_attendance_type
  @max_attendance_type
end

#meta_descriptionObject

Returns the value of attribute meta_description.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def meta_description
  @meta_description
end

#meta_keysObject

Returns the value of attribute meta_keys.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def meta_keys
  @meta_keys
end

#meta_titleObject

Returns the value of attribute meta_title.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def meta_title
  @meta_title
end

#publishedObject

Returns the value of attribute published.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def published
  @published
end

#restrictedObject

Returns the value of attribute restricted.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def restricted
  @restricted
end

#subtitleObject

Returns the value of attribute subtitle.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def subtitle
  @subtitle
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def title
  @title
end

#updated_atObject

Returns the value of attribute updated_at.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def updated_at
  @updated_at
end

#video_descriptionObject

Returns the value of attribute video_description.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def video_description
  @video_description
end

#video_titleObject

Returns the value of attribute video_title.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def video_title
  @video_title
end

#video_urlObject

Returns the value of attribute video_url.



4
5
6
# File 'lib/edools_sdk/product.rb', line 4

def video_url
  @video_url
end

Class Method Details

.allObject

Get all products

Example:

>> Product.all!
=> [#<EdoolsSdk::Product:0x00000002bc4a88 @id=22395, @title="test product 1", @description=nil, @subtitle=nil, @logo=nil, @video_url=nil, @video_title=nil, @video_description=nil, @published=false, @hidden=false, @restricted=false, @certification=false, @classes_auto_generation=false, @certification_min_progress=nil, @meta_title=nil, @meta_description=nil, @meta_keys=nil, @available_time_type="indeterminate", @available_time_length=nil, @available_time_unit=nil, @expire_date=nil, @library_resource_id=nil, @max_attendance_type="indeterminate", @max_attendance_length=nil, @allowed_emails=[], @class_teacher_ids=nil, @category_ids=nil, @gallery_media_ids=nil, @created_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>, @updated_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>>]


139
140
141
142
143
144
145
146
147
148
149
# File 'lib/edools_sdk/product.rb', line 139

def self.all
  response = HTTP
    .headers('Authorization' => "Token token=\"#{ENV['edools_token']}\"")
    .get(PRODUCT_URL_WITHOUT_SCHOOL)

  return response if response.status >= 400 && response.status < 600

  body = response.parse(:json)

  body['school_products'].map { |c| Product.parse_json(c) }
end

.all!Object

Get all products, if anything goes wrong an exception will be raised

Example:

>> Product.all!
=> [#<EdoolsSdk::Product:0x00000002bc4a88 @id=22395, @title="test product 1", @description=nil, @subtitle=nil, @logo=nil, @video_url=nil, @video_title=nil, @video_description=nil, @published=false, @hidden=false, @restricted=false, @certification=false, @classes_auto_generation=false, @certification_min_progress=nil, @meta_title=nil, @meta_description=nil, @meta_keys=nil, @available_time_type="indeterminate", @available_time_length=nil, @available_time_unit=nil, @expire_date=nil, @library_resource_id=nil, @max_attendance_type="indeterminate", @max_attendance_length=nil, @allowed_emails=[], @class_teacher_ids=nil, @category_ids=nil, @gallery_media_ids=nil, @created_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>, @updated_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>>]


122
123
124
125
126
127
128
129
130
131
132
# File 'lib/edools_sdk/product.rb', line 122

def self.all!
  response = HTTP
    .headers('Authorization' => "Token token=\"#{ENV['edools_token']}\"")
    .get(PRODUCT_URL_WITHOUT_SCHOOL)

  raise "invalid status code #{response.status}" if response.status >= 400 && response.status < 600

  body = response.parse(:json)

  body['school_products'].map { |c| Product.parse_json(c) }
end

.create(school_id, props) ⇒ Object

Create a product

Example:

>> Product.create(222, "title" => "test")
=> #<EdoolsSdk::Product:0x00000002bc4a88 @id=22395, @title="test product 1", @description=nil, @subtitle=nil, @logo=nil, @video_url=nil, @video_title=nil, @video_description=nil, @published=false, @hidden=false, @restricted=false, @certification=false, @classes_auto_generation=false, @certification_min_progress=nil, @meta_title=nil, @meta_description=nil, @meta_keys=nil, @available_time_type="indeterminate", @available_time_length=nil, @available_time_unit=nil, @expire_date=nil, @library_resource_id=nil, @max_attendance_type="indeterminate", @max_attendance_length=nil, @allowed_emails=[], @class_teacher_ids=nil, @category_ids=nil, @gallery_media_ids=nil, @created_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>, @updated_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>>


215
216
217
218
219
220
221
222
223
224
225
# File 'lib/edools_sdk/product.rb', line 215

def self.create(school_id, props)
  response = HTTP
    .headers('Authorization' => "Token token=\"#{ENV['edools_token']}\"")
    .post("#{PRODUCT_URL}#{school_id}/school_products", :json => props)

  return response if response.status != 201

  body = response.parse(:json)

  Product.parse_json(body)
end

.create!(school_id, props) ⇒ Object

Create a product, if anything goes wrong an exception will be raised

Example:

>> Product.create!(222, "title" => "test")
=> #<EdoolsSdk::Product:0x00000002bc4a88 @id=22395, @title="test product 1", @description=nil, @subtitle=nil, @logo=nil, @video_url=nil, @video_title=nil, @video_description=nil, @published=false, @hidden=false, @restricted=false, @certification=false, @classes_auto_generation=false, @certification_min_progress=nil, @meta_title=nil, @meta_description=nil, @meta_keys=nil, @available_time_type="indeterminate", @available_time_length=nil, @available_time_unit=nil, @expire_date=nil, @library_resource_id=nil, @max_attendance_type="indeterminate", @max_attendance_length=nil, @allowed_emails=[], @class_teacher_ids=nil, @category_ids=nil, @gallery_media_ids=nil, @created_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>, @updated_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>>


198
199
200
201
202
203
204
205
206
207
208
# File 'lib/edools_sdk/product.rb', line 198

def self.create!(school_id, props)
  response = HTTP
    .headers('Authorization' => "Token token=\"#{ENV['edools_token']}\"")
    .post("#{PRODUCT_URL}#{school_id}/school_products", :json => props)

  raise "invalid status code #{response.status}" if response.status != 201

  body = response.parse(:json)

  Product.parse_json(body)
end

.parse_json(props) ⇒ Object

Parse json to Product object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/edools_sdk/product.rb', line 75

def self.parse_json(props)
  product = Product.new

  product.id = props['id']
  product.title = props['title']
  product.description = props['description']
  product.subtitle = props['subtitle']
  product. = props['logo']
  product.video_url = props['video_url']
  product.video_title = props['video_title']
  product.video_description = props['video_description']
  product.published = props['published']
  product.hidden = props['hidden']
  product.restricted = props['restricted']
  product.certification = props['certification']
  product.classes_auto_generation = props['classes_auto_generation']
  product.certification_min_progress = props['certification_min_progress']
  product.meta_title = props['meta_title']
  product.meta_description = props['meta_description']
  product.meta_keys = props['meta_keys']
  product.available_time_type = props['available_time_type']
  product.available_time_length = props['available_time_length']
  product.available_time_unit = props['available_time_unit']
  product.expire_date = props['expire_date']
  product.library_resource_id = props['library_resource_id']
  product.max_attendance_type = props['max_attendance_type']
  product.max_attendance_length = props['max_attendance_length']
  product.allowed_emails = props['allowed_emails']
  product.class_teacher_ids = props['class_teacher_ids']
  product.category_ids = props['category_ids']
  product.gallery_media_ids = props['gallery_media_ids']

  unless props['created_at'].nil?
    product.created_at = Date.parse(props['created_at'])
  end
  unless props['updated_at'].nil?
    product.updated_at = Date.parse(props['updated_at'])
  end

  product
end

Instance Method Details

#save(school_id) ⇒ Object

Save a product

Example:

>> product_instance.save(222)
=> #<EdoolsSdk::Product:0x00000002bc4a88 @id=22395, @title="test product 1", @description=nil, @subtitle=nil, @logo=nil, @video_url=nil, @video_title=nil, @video_description=nil, @published=false, @hidden=false, @restricted=false, @certification=false, @classes_auto_generation=false, @certification_min_progress=nil, @meta_title=nil, @meta_description=nil, @meta_keys=nil, @available_time_type="indeterminate", @available_time_length=nil, @available_time_unit=nil, @expire_date=nil, @library_resource_id=nil, @max_attendance_type="indeterminate", @max_attendance_length=nil, @allowed_emails=[], @class_teacher_ids=nil, @category_ids=nil, @gallery_media_ids=nil, @created_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>, @updated_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>>


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/edools_sdk/product.rb', line 177

def save(school_id)
  response = HTTP
    .headers('Authorization' => "Token token=\"#{ENV['edools_token']}\"")
    .post("#{PRODUCT_URL}#{school_id}/school_products", :json => self.to_h)

  return response if response.status != 201

  body = response.parse(:json)

  product_saved = Product.parse_json(body)

  change_values(product_saved)

  product_saved
end

#save!(school_id) ⇒ Object

Save a product, if anything goes wrong an exception will be raised

Example:

>> product_instance.save(222)!
=> #<EdoolsSdk::Product:0x00000002bc4a88 @id=22395, @title="test product 1", @description=nil, @subtitle=nil, @logo=nil, @video_url=nil, @video_title=nil, @video_description=nil, @published=false, @hidden=false, @restricted=false, @certification=false, @classes_auto_generation=false, @certification_min_progress=nil, @meta_title=nil, @meta_description=nil, @meta_keys=nil, @available_time_type="indeterminate", @available_time_length=nil, @available_time_unit=nil, @expire_date=nil, @library_resource_id=nil, @max_attendance_type="indeterminate", @max_attendance_length=nil, @allowed_emails=[], @class_teacher_ids=nil, @category_ids=nil, @gallery_media_ids=nil, @created_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>, @updated_at=#<Date: 2017-09-25 ((2458022j,0s,0n),+0s,2299161j)>>


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/edools_sdk/product.rb', line 156

def save!(school_id)
  response = HTTP
    .headers('Authorization' => "Token token=\"#{ENV['edools_token']}\"")
    .post("#{PRODUCT_URL}#{school_id}/school_products", :json => self.to_h)

  raise "invalid status code #{response.status}" if response.status != 201

  body = response.parse(:json)

  product_saved = Product.parse_json(body)

  change_values(product_saved)

  product_saved
end