ur-product 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,18 +10,18 @@ Feature: UR Product
10
10
  And it should have relations
11
11
  And it should have siblings
12
12
  And one title in siblings should be "Antarktis : Ett vittne om framtiden"
13
-
13
+
14
14
  Scenario: Invalid ID
15
15
  Given I want the product 99991
16
16
  When I get the product it should throw "UR::InvalidProductID"
17
-
17
+
18
18
  Scenario: Multiple products
19
19
  Given I want the products 100001 and 150423
20
20
  When I get the products
21
21
  Then the result should contain 2 products
22
- And the second product should have the title "Vetenskapslandet"
23
-
22
+ And the second product should have the title "Vetenskapslandet : Säsong 6 prg. 13"
23
+
24
24
  Scenario: Check if a product has an image
25
25
  Given I want the product 100001
26
26
  When I get the product
27
- Then it should have image
27
+ Then it should have image
@@ -4,36 +4,36 @@ require 'restclient'
4
4
  # Module for Utbildningsradion AB (http://ur.se/)
5
5
  module UR
6
6
  # A product can be a tv show, a radio program or even a website
7
- class Product
7
+ class Product
8
8
  # Setup
9
9
  if !defined?(ASSETS_URL)
10
10
  ASSETS_URL = 'http://assets.ur.se'
11
11
  end
12
-
12
+
13
13
  attr_reader :related_products,
14
- :ur_product_id, :title, :languages,
14
+ :ur_product_id, :title, :languages,
15
15
  :description, :easy_to_read_description,
16
16
  :obsolete_order_id, :status, :production_year,
17
17
  :main_title, :remainder_of_title, :producing_company,
18
18
  :created_at, :modified_at, :format, :duration, :aspect_ratio,
19
- :product_type, :product_sub_type, :typical_age_ranges,
20
- :publication_date, :storages, :distribution_events, :difficulty,
19
+ :product_type, :product_sub_type, :typical_age_ranges,
20
+ :publication_date, :storages, :distribution_events, :difficulty,
21
21
  :sli, :sli_sub, :sab, :sao, :fao, :related_product_ids
22
-
22
+
23
23
  def initialize(data)
24
24
  unless !data['status'].nil? && data['status'] == 404
25
25
  product_data = data.include?('product') ? data['product'] : data
26
26
  relations = data.include?('relations') ? data['relations'] : []
27
27
  populate(product_data, relations)
28
-
28
+
29
29
  self.class.define_boolean_methods([
30
30
  'distribution_events', 'storages', 'typical_age_ranges', 'languages',
31
- 'duration', 'difficulty', 'producing_company', 'production_year',
31
+ 'duration', 'difficulty', 'producing_company', 'production_year',
32
32
  'obsolete_order_id', 'sli', 'sli_sub', 'sab', 'sao', 'fao'
33
33
  ])
34
34
  self.class.define_relation_boolean_methods([
35
- 'siblings', 'packageseries', 'packageusageseries', 'website',
36
- 'packagedvd', 'packagecd', 'programtv', 'programradio',
35
+ 'siblings', 'packageseries', 'packageusageseries', 'website',
36
+ 'packagedvd', 'packagecd', 'programtv', 'programradio',
37
37
  'trailertrailer'
38
38
  ])
39
39
  def ok?; true; end
@@ -41,61 +41,61 @@ module UR
41
41
  def ok?; false; end
42
42
  end
43
43
  end
44
-
44
+
45
45
  def self.find(id)
46
46
  data = MetadataCache.find(id)
47
-
48
- (data.instance_of?(Array)) ?
47
+
48
+ (data.instance_of?(Array)) ?
49
49
  data.map { |d| Product.new(d) } : Product.new(data)
50
50
  end
51
-
51
+
52
52
  def self.search(solr_params)
53
53
  UR::Search.new(solr_params)
54
54
  end
55
-
55
+
56
56
  def self.define_boolean_methods(names)
57
57
  names.each do |name|
58
58
  define_method("has_#{name}?") do
59
- instance_variable = instance_variable_get("@#{name}")
59
+ instance_variable = instance_variable_get("@#{name}")
60
60
  !(instance_variable.nil? || instance_variable.to_s.empty?)
61
61
  end
62
62
  end
63
63
  end
64
-
64
+
65
65
  def self.define_relation_boolean_methods(names)
66
66
  names.each do |name|
67
67
  define_method("has_#{name}?") do
68
68
  related_products = instance_variable_get("@related_products")
69
- (!related_products.nil? &&
70
- (!related_products[name].nil? &&
69
+ (!related_products.nil? &&
70
+ (!related_products[name].nil? &&
71
71
  !related_products[name].to_s.empty?))
72
72
  end
73
73
  end
74
74
  end
75
-
75
+
76
76
  def self.define_relation_accessor(name)
77
77
  define_method(name) do
78
78
  instance_variable_get("@related_products")[name]
79
79
  end
80
80
  end
81
-
81
+
82
82
  # Populate the object
83
83
  def populate(product_data, relations)
84
84
  # Handle the fields
85
85
  standard_fields = [
86
- 'id', 'obsoleteorderid', 'status', 'language',
86
+ 'id', 'obsoleteorderid', 'status', 'language',
87
87
  'title.sv', 'maintitle.sv', 'remainderoftitle.sv',
88
88
  'description.sv', 'easytoreaddescription.sv',
89
89
  'productionyear', 'producingcompany',
90
- 'created', 'modified', 'pubdate',
90
+ 'created', 'modified', 'pubdate',
91
91
  'format', 'duration', 'aspect_ratio',
92
- 'product_type', 'product_sub_type',
93
- 'typicalagerange', 'difficulty',
92
+ 'product_type', 'product_sub_type',
93
+ 'typicalagerange', 'difficulty',
94
94
  'sli', 'sli_sub', 'sab', 'sao', 'fao'
95
95
  ]
96
-
96
+
97
97
  field_names = lambda do |name|
98
- renamed = {
98
+ renamed = {
99
99
  'id' => 'ur_product_id',
100
100
  'maintitle' => 'main_title',
101
101
  'remainderoftitle' => 'remainder_of_title',
@@ -111,23 +111,23 @@ module UR
111
111
  }
112
112
  (renamed.keys.include?(name)) ? renamed[name] : name
113
113
  end
114
-
114
+
115
115
  standard_fields.each do |field|
116
116
  field,lang = field.split('.')
117
-
117
+
118
118
  if lang.nil?
119
119
  value = product_data[field]
120
120
  elsif !product_data[field].nil?
121
121
  value = product_data[field][lang]
122
122
  end
123
-
123
+
124
124
  if ['pubdate', 'created', 'modified'].include? field
125
125
  value = Time.parse(value)
126
126
  end
127
-
127
+
128
128
  instance_variable_set("@#{field_names.call(field)}", value)
129
129
  end
130
-
130
+
131
131
  # Handle the data structures
132
132
  [
133
133
  ['distributionevent', 'distribution_events', UR::Product::DistributionEvent],
@@ -135,74 +135,76 @@ module UR
135
135
  ['relation_haspart', 'related_product_ids', UR::Product::RelatedProductId],
136
136
  ].each do |name, variable, structure_class|
137
137
  data = product_data[name]
138
- instance_variable_set("@#{variable}", (!data.nil? && data.size > 0) ?
138
+ instance_variable_set("@#{variable}", (!data.nil? && data.size > 0) ?
139
139
  data.map { |d| structure_class.new d } : [])
140
140
  end
141
-
141
+
142
142
  # Handle the relations
143
143
  @related_products = {}
144
144
  if relations.size > 0
145
145
  @has_relations = true
146
146
  relations.each do |name, products|
147
147
  @related_products[name] = []
148
-
148
+
149
149
  products.each do |related_product_data|
150
150
  @related_products[name] << Product.new(related_product_data)
151
151
  end
152
-
152
+
153
153
  self.class.define_relation_accessor(name)
154
154
  end
155
155
  end
156
156
  end
157
-
158
- def available_on_avc?
159
- return @available_on_avc unless @available_on_avc.nil?
160
- @available_on_avc = false
161
-
157
+
158
+ def active_distribution_events
159
+ return @active_distribution_events unless @active_distribution_events.nil?
160
+
161
+ @active_distribution_events = {}
162
+
162
163
  distribution_events.each do |event|
163
- if event.receiving_agent_group == 'avc'
164
- @available_on_avc = event.active?
165
- break
166
- end
164
+ @active_distribution_events[event.receiving_agent_group] = event if event.active?
167
165
  end
168
-
169
- @available_on_avc
166
+
167
+ @active_distribution_events
168
+ end
169
+
170
+ def available_on_avc?
171
+ !active_distribution_events['avc'].nil?
170
172
  end
171
-
173
+
172
174
  def broadcasts
173
175
  return @broadcasts unless @broadcasts.nil?
174
176
  @broadcasts = []
175
-
177
+
176
178
  distribution_events.each do |event|
177
179
  @broadcasts << event if event.event_type == 'broadcast'
178
180
  end
179
-
181
+
180
182
  @broadcasts = @broadcasts.sort_by { |broadcast| broadcast.start_date }
181
183
  @broadcasts
182
184
  end
183
-
185
+
184
186
  def documents
185
187
  return @docs unless @docs.nil?
186
188
  @docs = []
187
-
189
+
188
190
  [
189
- 'textteacherguide',
190
- 'textstudyguide',
191
- 'textscript',
191
+ 'textteacherguide',
192
+ 'textstudyguide',
193
+ 'textscript',
192
194
  'textworksheet',
193
- 'texttasks',
195
+ 'texttasks',
194
196
  'texttext'
195
197
  ].each do |name|
196
198
  @docs << @related_products[name] if !@related_products[name].nil?
197
199
  end
198
-
200
+
199
201
  @docs.flatten!
200
202
  end
201
-
203
+
202
204
  def has_documents?
203
205
  (!documents.nil? && documents.size > 0)
204
206
  end
205
-
207
+
206
208
  def humanized_duration
207
209
  if matched = duration.match(/^(\d\d):(\d\d):(\d\d)/)
208
210
  (full,h,m,s) = matched.to_a
@@ -222,42 +224,46 @@ module UR
222
224
  broadcast_format = (product_type == 'package') ? "-#{format}" : ''
223
225
  (product_sub_type.nil?) ? product_type : "#{product_type}#{product_sub_type}#{broadcast_format}"
224
226
  end
225
-
227
+
226
228
  def image_url(number = 1, size = '')
227
229
  "#{ASSETS_URL}/id/#{ur_product_id}/images/#{number}#{size}.jpg"
228
230
  end
229
-
231
+
230
232
  def has_image?(number = 1, size = '')
231
233
  return @has_image if !@has_image.nil?
232
-
234
+
233
235
  begin
234
236
  url = "#{ASSETS_URL}/id/#{ur_product_id}/images/#{number}#{size}.jpg"
235
237
  @has_image = (RestClient.head(url).headers[:x_ur_http_status] == "200")
236
238
  rescue RestClient::ResourceNotFound
237
239
  @has_image = false
238
240
  end
239
-
241
+
240
242
  @has_image
241
243
  end
242
244
 
243
245
  def has_relations?
244
246
  (!@related_products.nil? && @related_products.size > 0)
245
247
  end
246
-
248
+
247
249
  def get_storage_location(format)
248
250
  storage = storages.map { |s| s if s.storage_format == format }.compact
249
251
  storage.empty? ? false : storage.first.location
250
252
  end
251
-
253
+
252
254
  def media_id
253
- @media_id ||= (get_storage_location('genericvideo') ||
255
+ @media_id ||= (get_storage_location('genericvideo') ||
254
256
  get_storage_location('genericaudio'))
255
257
  @media_id
256
258
  end
257
-
259
+
260
+ def has_media_id?
261
+ (media_id)
262
+ end
263
+
258
264
  def url
259
265
  return @url unless @url.nil?
260
-
266
+
261
267
  if product_type == 'website'
262
268
  @url = get_storage_location('url')
263
269
  elsif respond_to?(:website) && website.respond_to?(:first)
@@ -272,11 +278,11 @@ module UR
272
278
 
273
279
  @url
274
280
  end
275
-
281
+
276
282
  def has_url?
277
283
  (url)
278
284
  end
279
-
285
+
280
286
  def short_title
281
287
  if !remainder_of_title.nil?
282
288
  remainder_of_title
@@ -285,4 +291,4 @@ module UR
285
291
  end
286
292
  end
287
293
  end
288
- end
294
+ end
@@ -2,8 +2,8 @@ module UR
2
2
  module VERSION
3
3
  MAJOR = 1
4
4
  MINOR = 0
5
- TINY = 2
6
-
5
+ TINY = 3
6
+
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
9
- end
9
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ur-product
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 2
10
- version: 1.0.2
9
+ - 3
10
+ version: 1.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Peter Hellberg
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-08 00:00:00 +01:00
18
+ date: 2011-01-07 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency