ur-product 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,8 +7,10 @@ module UR
7
7
  # populating one or more UR::Product objects
8
8
  class MetadataCache
9
9
  # Setup
10
- METADATA_PRODUCT_URL = 'http://metadata.ur.se/products'
11
-
10
+ if !defined?(METADATA_PRODUCT_URL)
11
+ METADATA_PRODUCT_URL = 'http://metadata.ur.se/products'
12
+ end
13
+
12
14
  # Retrieve one or more products
13
15
  def self.find(id)
14
16
  if id.instance_of?(Array)
data/lib/ur/product.rb CHANGED
@@ -2,23 +2,31 @@
2
2
  module UR
3
3
  # A product can be a tv show, a radio program or even a website
4
4
  class Product
5
+
5
6
  # Setup
6
- ASSETS_URL = 'http://assets.ur.se'
7
+ if !defined?(ASSETS_URL)
8
+ ASSETS_URL = 'http://assets.ur.se'
9
+ end
10
+
7
11
  attr_reader :related_products,
8
- :ur_product_id, :title, :language,
12
+ :ur_product_id, :title, :languages,
9
13
  :description, :easy_to_read_description,
10
- :obsoleteorderid, :status, :productionyear,
11
- :maintitle, :remainderoftitle, :producingcompany,
12
- :created, :modified, :format, :duration, :aspect_ratio,
13
- :product_type, :product_sub_type, :typical_age_range,
14
- :pubdate, :storages, :distribution_events
14
+ :obsolete_order_id, :status, :production_year,
15
+ :main_title, :remainder_of_title, :producing_company,
16
+ :created_at, :modified_at, :format, :duration, :aspect_ratio,
17
+ :product_type, :product_sub_type, :typical_age_ranges,
18
+ :publication_date, :storages, :distribution_events, :sab, :sao
15
19
 
16
20
  def initialize(data)
17
21
  product_data = data.include?('product') ? data['product'] : data
18
22
  relations = data.include?('relations') ? data['relations'] : []
19
23
  populate(product_data, relations)
20
24
 
21
- self.class.define_boolean_methods(['distribution_events', 'storages'])
25
+ self.class.define_boolean_methods([
26
+ 'distribution_events', 'storages', 'typical_age_ranges', 'languages',
27
+ 'duration', 'difficulty', 'producing_company', 'obsolete_order_id',
28
+ 'sab', 'sao'
29
+ ])
22
30
  self.class.define_relation_boolean_methods(['siblings'])
23
31
  end
24
32
 
@@ -37,7 +45,7 @@ module UR
37
45
  names.each do |name|
38
46
  define_method("has_#{name}?") do
39
47
  instance_variable = instance_variable_get("@#{name}")
40
- (!instance_variable.nil? || !instance_variable.empty?)
48
+ !(instance_variable.nil? || instance_variable.empty?)
41
49
  end
42
50
  end
43
51
  end
@@ -68,16 +76,25 @@ module UR
68
76
  'created', 'modified', 'pubdate',
69
77
  'format', 'duration', 'aspect_ratio',
70
78
  'product_type', 'product_sub_type',
71
- 'typical_age_range'
79
+ 'typicalagerange', 'sab', 'sao'
72
80
  ]
73
81
 
74
82
  field_names = lambda do |name|
75
83
  renamed = {
76
- 'id' => 'ur_product_id',
84
+ 'id' => 'ur_product_id',
85
+ 'maintitle' => 'main_title',
86
+ 'remainderoftitle' => 'remainder_of_title',
77
87
  'easytoreaddescription' => 'easy_to_read_description',
78
- 'typicalagerange' => 'typical_age_range'
88
+ 'producingcompany' => 'producing_company',
89
+ 'created' => 'created_at',
90
+ 'modified' => 'modified_at',
91
+ 'pubdate' => 'publication_date',
92
+ 'obsoleteorderid' => 'obsolete_order_id',
93
+ 'typicalagerange' => 'typical_age_ranges',
94
+ 'productionyear' => 'production_year',
95
+ 'language' => 'languages'
79
96
  }
80
- (renamed.include?(name)) ? renamed[name] : name
97
+ (renamed.keys.include?(name)) ? renamed[name] : name
81
98
  end
82
99
 
83
100
  standard_fields.each do |field|
@@ -89,6 +106,10 @@ module UR
89
106
  value = product_data[field][lang]
90
107
  end
91
108
 
109
+ if ['pubdate', 'created', 'modified'].include? field
110
+ value = Time.parse(value)
111
+ end
112
+
92
113
  instance_variable_set("@#{field_names.call(field)}", value)
93
114
  end
94
115
 
@@ -117,12 +138,36 @@ module UR
117
138
  end
118
139
  end
119
140
  end
141
+
142
+ def humanized_duration
143
+ if matched = duration.match(/^(\d\d):(\d\d):(\d\d)/)
144
+ (full,h,m,s) = matched.to_a
145
+ if h == '00'
146
+ "#{m} minuter"
147
+ elsif h == '00' && m == '00'
148
+ 'Under en minut'
149
+ else
150
+ "#{h.to_i}:#{m}"
151
+ end
152
+ else
153
+ duration
154
+ end
155
+ end
120
156
 
121
- def has_image?
157
+ def full_type
158
+ broadcast_format = (product_type == 'package') ? "-#{format}" : ''
159
+ (product_sub_type.nil?) ? product_type : "#{product_type}#{product_sub_type}#{broadcast_format}"
160
+ end
161
+
162
+ def image_url(number = 1, size = '')
163
+ "#{ASSETS_URL}/id/#{ur_product_id}/images/#{number}#{size}.jpg"
164
+ end
165
+
166
+ def has_image?(number = 1, size = '')
122
167
  return @has_image if !@has_image.nil?
123
168
 
124
169
  begin
125
- url = "#{ASSETS_URL}/id/#{ur_product_id}/images/1.jpg"
170
+ url = "#{ASSETS_URL}/id/#{ur_product_id}/images/#{number}#{size}.jpg"
126
171
  @has_image = (RestClient.head(url).headers[:x_ur_http_status] == "200")
127
172
  rescue RestClient::ResourceNotFound
128
173
  @has_image = false
data/lib/ur/search.rb CHANGED
@@ -5,7 +5,10 @@ module UR
5
5
  # Search for products and populate from the metadata cache
6
6
  class Search
7
7
  # Setup
8
- SEARCH_SERVICE_URL = 'http://services.ur.se/search'
8
+ if !defined?(SEARCH_SERVICE_URL)
9
+ SEARCH_SERVICE_URL = 'http://services.ur.se/search'
10
+ end
11
+
9
12
  attr_reader :products, :solr, :facets
10
13
 
11
14
  def initialize(solr_params)
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- version: "0.3"
7
+ - 4
8
+ version: "0.4"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Peter Hellberg
@@ -13,7 +13,7 @@ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
 
16
- date: 2010-04-29 00:00:00 +02:00
16
+ date: 2010-05-06 00:00:00 +02:00
17
17
  default_executable:
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency