Class: UR::Product
- Inherits:
-
Object
- Object
- UR::Product
- Defined in:
- lib/ur/product.rb,
lib/ur/product/storage.rb,
lib/ur/product/distribution_event.rb,
lib/ur/product/related_product_id.rb
Overview
A product can be a tv show, a radio program or even a website
Defined Under Namespace
Classes: DistributionEvent, RelatedProductId, Storage
Constant Summary collapse
- ASSETS_URL =
'http://assets.ur.se'
Instance Attribute Summary collapse
-
#aspect_ratio ⇒ Object
readonly
Returns the value of attribute aspect_ratio.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#difficulty ⇒ Object
readonly
Returns the value of attribute difficulty.
-
#distribution_events ⇒ Object
readonly
Returns the value of attribute distribution_events.
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#easy_to_read_description ⇒ Object
readonly
Returns the value of attribute easy_to_read_description.
-
#fao ⇒ Object
readonly
Returns the value of attribute fao.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#languages ⇒ Object
readonly
Returns the value of attribute languages.
-
#main_title ⇒ Object
readonly
Returns the value of attribute main_title.
-
#modified_at ⇒ Object
readonly
Returns the value of attribute modified_at.
-
#obsolete_order_id ⇒ Object
readonly
Returns the value of attribute obsolete_order_id.
-
#producing_company ⇒ Object
readonly
Returns the value of attribute producing_company.
-
#product_sub_type ⇒ Object
readonly
Returns the value of attribute product_sub_type.
-
#product_type ⇒ Object
readonly
Returns the value of attribute product_type.
-
#production_year ⇒ Object
readonly
Returns the value of attribute production_year.
-
#publication_date ⇒ Object
readonly
Returns the value of attribute publication_date.
-
#related_product_ids ⇒ Object
readonly
Returns the value of attribute related_product_ids.
-
#related_products ⇒ Object
readonly
Returns the value of attribute related_products.
-
#remainder_of_title ⇒ Object
readonly
Returns the value of attribute remainder_of_title.
-
#sab ⇒ Object
readonly
Returns the value of attribute sab.
-
#sao ⇒ Object
readonly
Returns the value of attribute sao.
-
#sli ⇒ Object
readonly
Returns the value of attribute sli.
-
#sli_sub ⇒ Object
readonly
Returns the value of attribute sli_sub.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#storages ⇒ Object
readonly
Returns the value of attribute storages.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#typical_age_ranges ⇒ Object
readonly
Returns the value of attribute typical_age_ranges.
-
#ur_product_id ⇒ Object
readonly
Returns the value of attribute ur_product_id.
Class Method Summary collapse
- .define_boolean_methods(names) ⇒ Object
- .define_relation_accessor(name) ⇒ Object
- .define_relation_boolean_methods(names) ⇒ Object
- .find(id) ⇒ Object
- .search(solr_params) ⇒ Object
Instance Method Summary collapse
- #active_distribution_events ⇒ Object
- #available_on_avc? ⇒ Boolean
- #broadcasts ⇒ Object
- #documents ⇒ Object
- #full_type ⇒ Object
- #get_storage_location(format) ⇒ Object
- #has_documents? ⇒ Boolean
- #has_image?(number = 1, size = '') ⇒ Boolean
- #has_media_id? ⇒ Boolean
- #has_relations? ⇒ Boolean
- #has_url? ⇒ Boolean
- #humanized_duration ⇒ Object
- #image_url(number = 1, size = '') ⇒ Object
-
#initialize(data) ⇒ Product
constructor
A new instance of Product.
- #media_id ⇒ Object
-
#populate(product_data, relations) ⇒ Object
Populate the object.
- #short_title ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(data) ⇒ Product
Returns a new instance of Product.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ur/product.rb', line 25 def initialize(data) unless !data['status'].nil? && data['status'] == 404 product_data = data.include?('product') ? data['product'] : data relations = data.include?('relations') ? data['relations'] : [] populate(product_data, relations) self.class.define_boolean_methods([ 'distribution_events', 'storages', 'typical_age_ranges', 'languages', 'duration', 'difficulty', 'producing_company', 'production_year', 'obsolete_order_id', 'sli', 'sli_sub', 'sab', 'sao', 'fao' ]) self.class.define_relation_boolean_methods([ 'siblings', 'packageseries', 'packageusageseries', 'website', 'packagedvd', 'packagecd', 'programtv', 'programradio', 'trailertrailer' ]) def ok?; true; end else def ok?; false; end end end |
Instance Attribute Details
#aspect_ratio ⇒ Object (readonly)
Returns the value of attribute aspect_ratio.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def aspect_ratio @aspect_ratio end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def created_at @created_at end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def description @description end |
#difficulty ⇒ Object (readonly)
Returns the value of attribute difficulty.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def difficulty @difficulty end |
#distribution_events ⇒ Object (readonly)
Returns the value of attribute distribution_events.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def distribution_events @distribution_events end |
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def duration @duration end |
#easy_to_read_description ⇒ Object (readonly)
Returns the value of attribute easy_to_read_description.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def easy_to_read_description @easy_to_read_description end |
#fao ⇒ Object (readonly)
Returns the value of attribute fao.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def fao @fao end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def format @format end |
#languages ⇒ Object (readonly)
Returns the value of attribute languages.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def languages @languages end |
#main_title ⇒ Object (readonly)
Returns the value of attribute main_title.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def main_title @main_title end |
#modified_at ⇒ Object (readonly)
Returns the value of attribute modified_at.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def modified_at @modified_at end |
#obsolete_order_id ⇒ Object (readonly)
Returns the value of attribute obsolete_order_id.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def obsolete_order_id @obsolete_order_id end |
#producing_company ⇒ Object (readonly)
Returns the value of attribute producing_company.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def producing_company @producing_company end |
#product_sub_type ⇒ Object (readonly)
Returns the value of attribute product_sub_type.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def product_sub_type @product_sub_type end |
#product_type ⇒ Object (readonly)
Returns the value of attribute product_type.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def product_type @product_type end |
#production_year ⇒ Object (readonly)
Returns the value of attribute production_year.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def production_year @production_year end |
#publication_date ⇒ Object (readonly)
Returns the value of attribute publication_date.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def publication_date @publication_date end |
#related_product_ids ⇒ Object (readonly)
Returns the value of attribute related_product_ids.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def @related_product_ids end |
#related_products ⇒ Object (readonly)
Returns the value of attribute related_products.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def @related_products end |
#remainder_of_title ⇒ Object (readonly)
Returns the value of attribute remainder_of_title.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def remainder_of_title @remainder_of_title end |
#sab ⇒ Object (readonly)
Returns the value of attribute sab.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def sab @sab end |
#sao ⇒ Object (readonly)
Returns the value of attribute sao.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def sao @sao end |
#sli ⇒ Object (readonly)
Returns the value of attribute sli.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def sli @sli end |
#sli_sub ⇒ Object (readonly)
Returns the value of attribute sli_sub.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def sli_sub @sli_sub end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def status @status end |
#storages ⇒ Object (readonly)
Returns the value of attribute storages.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def storages @storages end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def title @title end |
#typical_age_ranges ⇒ Object (readonly)
Returns the value of attribute typical_age_ranges.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def typical_age_ranges @typical_age_ranges end |
#ur_product_id ⇒ Object (readonly)
Returns the value of attribute ur_product_id.
15 16 17 |
# File 'lib/ur/product.rb', line 15 def ur_product_id @ur_product_id end |
Class Method Details
.define_boolean_methods(names) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/ur/product.rb', line 58 def self.define_boolean_methods(names) names.each do |name| define_method("has_#{name}?") do instance_variable = instance_variable_get("@#{name}") !(instance_variable.nil? || instance_variable.to_s.empty?) end end end |
.define_relation_accessor(name) ⇒ Object
78 79 80 81 82 |
# File 'lib/ur/product.rb', line 78 def self.define_relation_accessor(name) define_method(name) do instance_variable_get("@related_products")[name] end end |
.define_relation_boolean_methods(names) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ur/product.rb', line 67 def self.define_relation_boolean_methods(names) names.each do |name| define_method("has_#{name}?") do = instance_variable_get("@related_products") (!.nil? && (![name].nil? && ![name].to_s.empty?)) end end end |
Instance Method Details
#active_distribution_events ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/ur/product.rb', line 160 def active_distribution_events return @active_distribution_events unless @active_distribution_events.nil? @active_distribution_events = {} distribution_events.each do |event| @active_distribution_events[event.receiving_agent_group] = event if event.active? end @active_distribution_events end |
#available_on_avc? ⇒ Boolean
172 173 174 |
# File 'lib/ur/product.rb', line 172 def available_on_avc? !active_distribution_events['avc'].nil? end |
#broadcasts ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/ur/product.rb', line 176 def broadcasts return @broadcasts unless @broadcasts.nil? @broadcasts = [] distribution_events.each do |event| @broadcasts << event if event.event_type == 'broadcast' end @broadcasts = @broadcasts.sort_by { |broadcast| broadcast.start_date } @broadcasts end |
#documents ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/ur/product.rb', line 188 def documents return @docs unless @docs.nil? @docs = [] [ 'textteacherguide', 'textstudyguide', 'textscript', 'textworksheet', 'texttasks', 'texttext' ].each do |name| @docs << @related_products[name] if !@related_products[name].nil? end @docs.flatten! end |
#full_type ⇒ Object
225 226 227 228 |
# File 'lib/ur/product.rb', line 225 def full_type broadcast_format = (product_type == 'package') ? "-#{format}" : '' (product_sub_type.nil?) ? product_type : "#{product_type}#{product_sub_type}#{broadcast_format}" end |
#get_storage_location(format) ⇒ Object
251 252 253 254 |
# File 'lib/ur/product.rb', line 251 def get_storage_location(format) storage = storages.map { |s| s if s.storage_format == format }.compact storage.empty? ? false : storage.first.location end |
#has_documents? ⇒ Boolean
206 207 208 |
# File 'lib/ur/product.rb', line 206 def has_documents? (!documents.nil? && documents.size > 0) end |
#has_image?(number = 1, size = '') ⇒ Boolean
234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/ur/product.rb', line 234 def has_image?(number = 1, size = '') return @has_image if !@has_image.nil? begin url = "#{ASSETS_URL}/id/#{ur_product_id}/images/#{number}#{size}.jpg" @has_image = (RestClient.head(url).headers[:x_ur_http_status] == "200") rescue RestClient::ResourceNotFound @has_image = false end @has_image end |
#has_media_id? ⇒ Boolean
262 263 264 |
# File 'lib/ur/product.rb', line 262 def has_media_id? (media_id) end |
#has_relations? ⇒ Boolean
247 248 249 |
# File 'lib/ur/product.rb', line 247 def has_relations? (!@related_products.nil? && @related_products.size > 0) end |
#has_url? ⇒ Boolean
284 285 286 |
# File 'lib/ur/product.rb', line 284 def has_url? (url) end |
#humanized_duration ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/ur/product.rb', line 210 def humanized_duration if matched = duration.match(/^(\d\d):(\d\d):(\d\d)/) (full,h,m,s) = matched.to_a if h == '00' && m != '00' "#{m.to_i} minuter" elsif h == '00' 'Under en minut' else "#{h.to_i}:#{m}" end else duration end end |
#image_url(number = 1, size = '') ⇒ Object
230 231 232 |
# File 'lib/ur/product.rb', line 230 def image_url(number = 1, size = '') "#{ASSETS_URL}/id/#{ur_product_id}/images/#{number}#{size}.jpg" end |
#media_id ⇒ Object
256 257 258 259 260 |
# File 'lib/ur/product.rb', line 256 def media_id @media_id ||= (get_storage_location('genericvideo') || get_storage_location('genericaudio')) @media_id end |
#populate(product_data, relations) ⇒ Object
Populate the object
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/ur/product.rb', line 85 def populate(product_data, relations) # Handle the fields standard_fields = [ 'id', 'obsoleteorderid', 'status', 'language', 'title.sv', 'maintitle.sv', 'remainderoftitle.sv', 'description.sv', 'easytoreaddescription.sv', 'productionyear', 'producingcompany', 'created', 'modified', 'pubdate', 'format', 'duration', 'aspect_ratio', 'product_type', 'product_sub_type', 'typicalagerange', 'difficulty', 'sli', 'sli_sub', 'sab', 'sao', 'fao' ] field_names = lambda do |name| renamed = { 'id' => 'ur_product_id', 'maintitle' => 'main_title', 'remainderoftitle' => 'remainder_of_title', 'easytoreaddescription' => 'easy_to_read_description', 'producingcompany' => 'producing_company', 'created' => 'created_at', 'modified' => 'modified_at', 'pubdate' => 'publication_date', 'obsoleteorderid' => 'obsolete_order_id', 'typicalagerange' => 'typical_age_ranges', 'productionyear' => 'production_year', 'language' => 'languages' } (renamed.keys.include?(name)) ? renamed[name] : name end standard_fields.each do |field| field,lang = field.split('.') if lang.nil? value = product_data[field] elsif !product_data[field].nil? value = product_data[field][lang] end if ['pubdate', 'created', 'modified'].include? field value = Time.parse(value) end instance_variable_set("@#{field_names.call(field)}", value) end # Handle the data structures [ ['distributionevent', 'distribution_events', UR::Product::DistributionEvent], ['storage', 'storages', UR::Product::Storage], ['relation_haspart', 'related_product_ids', UR::Product::RelatedProductId], ].each do |name, variable, structure_class| data = product_data[name] instance_variable_set("@#{variable}", (!data.nil? && data.size > 0) ? data.map { |d| structure_class.new d } : []) end # Handle the relations @related_products = {} if relations.size > 0 @has_relations = true relations.each do |name, products| @related_products[name] = [] products.each do || @related_products[name] << Product.new() end self.class.define_relation_accessor(name) end end end |
#short_title ⇒ Object
288 289 290 291 292 293 294 |
# File 'lib/ur/product.rb', line 288 def short_title if !remainder_of_title.nil? remainder_of_title else title end end |
#url ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/ur/product.rb', line 266 def url return @url unless @url.nil? if product_type == 'website' @url = get_storage_location('url') elsif respond_to?(:website) && website.respond_to?(:first) if website.first.nil? @url = false else @url = website.first.get_storage_location('url') end else @url = false end @url end |