ur-product 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/MIT-LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2010 Peter Hellberg
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,34 @@
1
+ # UR::Product
2
+
3
+ API wrapper for the [Utbildningsradion](http://ur.se) product services.
4
+
5
+ ### Installation
6
+
7
+ gem install ur-product
8
+
9
+ ### Related Resources (in Swedish)
10
+
11
+ * [Metadata Cache](http://metadata.ur.se)
12
+ * [Search](http://services.ur.se/search)
13
+
14
+ ### Example usage
15
+
16
+ #### Find
17
+ require 'ur-product'
18
+ product = UR::Product.find(100001)
19
+
20
+ #### Search
21
+ require 'ur-product'
22
+ UR::Product.search({
23
+ :queries => 'samtid',
24
+ :filter => { :search_product_type => 'programtv' },
25
+ :page => 1,
26
+ :per_page => 5,
27
+ :publicstreaming => 'NOW'
28
+ })
29
+
30
+ ### Dependencies
31
+
32
+ * [RSolr](http://github.com/mwmitchell/rsolr)
33
+ * [RSolr-ext](http://github.com/mwmitchell/rsolr-ext)
34
+ * [RestClient](http://github.com/archiloque/rest-client)
@@ -0,0 +1,41 @@
1
+ require 'json'
2
+ require 'restclient'
3
+
4
+ # Module for Utbildningsradion AB (http://ur.se/)
5
+ module UR
6
+ # Responsible for retrieving metadata and
7
+ # populating one or more UR::Product objects
8
+ class MetadataCache
9
+ # Setup
10
+ METADATA_PRODUCT_URL = 'http://metadata.ur.se/products'
11
+
12
+ # Retrieve one or more products
13
+ def self.find(id)
14
+ if id.instance_of?(Array)
15
+ valid_ids = []
16
+
17
+ id.each do |id|
18
+ if id.to_s.match(/^1\d{5}$/)
19
+ valid_ids << id
20
+ else
21
+ raise UR::InvalidProductID
22
+ end
23
+ end
24
+
25
+ url = METADATA_PRODUCT_URL + ".json?ur_product_ids=#{valid_ids.join(',')}"
26
+ else
27
+ raise UR::InvalidProductID if !id.to_s.match(/^1\d{5}$/)
28
+ url = METADATA_PRODUCT_URL + "/#{id}.json"
29
+ end
30
+
31
+ # Get the JSON response from the Metadata Cache
32
+ response = RestClient.get(url)
33
+
34
+ # Make sure that we got a valid response
35
+ raise UR::InvalidResponse if response.code != 200
36
+
37
+ # Return the response as a parsed JSON object
38
+ JSON.parse(response.body)
39
+ end
40
+ end
41
+ end
data/lib/ur/product.rb ADDED
@@ -0,0 +1,118 @@
1
+ # Module for Utbildningsradion AB (http://ur.se/)
2
+ module UR
3
+ # A product can be a tv show, a radio program or even a website
4
+ class Product
5
+ # Setup
6
+ ASSETS_URL = 'http://assets.ur.se'
7
+ attr_accessor :related_products,
8
+ :ur_product_id, :title, :language,
9
+ :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
15
+
16
+ def initialize(data)
17
+ product_data = data.include?('product') ? data['product'] : data
18
+ relations = data.include?('relations') ? data['relations'] : []
19
+ populate(product_data, relations)
20
+
21
+ self.class.define_boolean_methods(['siblings'])
22
+ end
23
+
24
+ def self.find(id)
25
+ data = MetadataCache.find(id)
26
+
27
+ (data.instance_of?(Array)) ?
28
+ data.map { |d| Product.new(d) } : Product.new(data)
29
+ end
30
+
31
+ def self.search(solr_params)
32
+ UR::Search.new(solr_params)
33
+ end
34
+
35
+ def self.define_boolean_methods(names)
36
+ names.each do |name|
37
+ define_method("has_#{name}?") do
38
+ related_products = instance_variable_get("@related_products")
39
+ (!related_products.nil? && !related_products[name].nil?)
40
+ end
41
+ end
42
+ end
43
+
44
+ def self.define_relation_accessor(name)
45
+ define_method(name) do
46
+ instance_variable_get("@related_products")[name]
47
+ end
48
+ end
49
+
50
+ # Populate the object
51
+ def populate(product_data, relations)
52
+ # Handle the fields
53
+ standard_fields = [
54
+ 'id', 'obsoleteorderid', 'status', 'language',
55
+ 'title.sv', 'maintitle.sv', 'remainderoftitle.sv',
56
+ 'description.sv', 'easytoreaddescription.sv',
57
+ 'productionyear', 'producingcompany',
58
+ 'created', 'modified', 'pubdate',
59
+ 'format', 'duration', 'aspect_ratio',
60
+ 'product_type', 'product_sub_type',
61
+ 'typical_age_range'
62
+ ]
63
+
64
+ field_names = lambda do |name|
65
+ renamed = {
66
+ 'id' => 'ur_product_id',
67
+ 'easytoreaddescription' => 'easy_to_read_description',
68
+ 'typicalagerange' => 'typical_age_range'
69
+ }
70
+ (renamed.include?(name)) ? renamed[name] : name
71
+ end
72
+
73
+ standard_fields.each do |field|
74
+ field,lang = field.split('.')
75
+
76
+ if lang.nil?
77
+ value = product_data[field]
78
+ elsif !product_data[field].nil?
79
+ value = product_data[field][lang]
80
+ end
81
+
82
+ self.send("#{field_names.call(field)}=", value)
83
+ end
84
+
85
+ # Handle the relations
86
+ if relations.size > 0
87
+ @related_products = {}
88
+ @has_relations = true
89
+ relations.each do |name, products|
90
+ @related_products[name] = []
91
+
92
+ products.each do |related_product_data|
93
+ @related_products[name] << Product.new(related_product_data)
94
+ end
95
+
96
+ self.class.define_relation_accessor(name)
97
+ end
98
+ end
99
+ end
100
+
101
+ def has_image?
102
+ return @has_image if !@has_image.nil?
103
+
104
+ begin
105
+ url = "#{ASSETS_URL}/id/#{ur_product_id}/images/1.jpg"
106
+ @has_image = (RestClient.head(url).headers[:x_ur_http_status] == "200")
107
+ rescue RestClient::ResourceNotFound
108
+ @has_image = false
109
+ end
110
+
111
+ @has_image
112
+ end
113
+
114
+ def has_relations?
115
+ (!@related_products.nil? && @related_products.size > 0)
116
+ end
117
+ end
118
+ end
data/lib/ur/search.rb ADDED
@@ -0,0 +1,43 @@
1
+ require 'rsolr-ext'
2
+
3
+ # Module for Utbildningsradion AB (http://ur.se/)
4
+ module UR
5
+ # Search for products and populate from the metadata cache
6
+ class Search
7
+ # Setup
8
+ SEARCH_SERVICE_URL = 'http://services.ur.se/search'
9
+ attr_accessor :products, :solr, :facets
10
+
11
+ def initialize(solr_params)
12
+ solr = RSolr.connect :url => SEARCH_SERVICE_URL
13
+ response = solr.find solr_params
14
+
15
+ # Populate the products from the Metadata Cache
16
+ @products = (response.ok? && response.docs.size > 0) ?
17
+ Product.find(response.docs.map { |d| d.id }) : []
18
+
19
+ # Expose the Solr response
20
+ @solr = response
21
+
22
+ # Prepare the facets
23
+ @facets = {}
24
+ @solr.facets.map { |f| @facets[f.name] = f.items } if @solr.facets.size > 0
25
+ end
26
+
27
+ def num_found
28
+ @solr.response['numFound'].to_i
29
+ end
30
+
31
+ def ok?
32
+ @solr.ok?
33
+ end
34
+
35
+ def previous_page
36
+ @solr.docs.previous_page
37
+ end
38
+
39
+ def next_page
40
+ @solr.docs.next_page
41
+ end
42
+ end
43
+ end
data/lib/ur-product.rb ADDED
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/ur/metadata_cache'
2
+ require File.dirname(__FILE__) + '/ur/product'
3
+ require File.dirname(__FILE__) + '/ur/search'
4
+
5
+ module UR
6
+ class InvalidProductID < Exception
7
+ end
8
+
9
+ class InvalidResponse < Exception
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ur-product
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Peter Hellberg
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-04-22 00:00:00 +02:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: json
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ segments:
27
+ - 1
28
+ - 2
29
+ - 3
30
+ version: 1.2.3
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rsolr
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ - 12
43
+ - 1
44
+ version: 0.12.1
45
+ type: :runtime
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: rsolr-ext
49
+ prerelease: false
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ - 12
57
+ - 0
58
+ version: 0.12.0
59
+ type: :runtime
60
+ version_requirements: *id003
61
+ - !ruby/object:Gem::Dependency
62
+ name: rest-client
63
+ prerelease: false
64
+ requirement: &id004 !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 1
70
+ - 4
71
+ - 2
72
+ version: 1.4.2
73
+ type: :runtime
74
+ version_requirements: *id004
75
+ description: Enables searching and fetching of Utbildningsradion products
76
+ email: peter@c7.se
77
+ executables: []
78
+
79
+ extensions: []
80
+
81
+ extra_rdoc_files: []
82
+
83
+ files:
84
+ - README.markdown
85
+ - MIT-LICENSE
86
+ - lib/ur-product.rb
87
+ - lib/ur/metadata_cache.rb
88
+ - lib/ur/product.rb
89
+ - lib/ur/search.rb
90
+ has_rdoc: true
91
+ homepage: http://github.com/c7/ur-product
92
+ licenses:
93
+ - MIT-LICENSE
94
+ post_install_message:
95
+ rdoc_options: []
96
+
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ segments:
111
+ - 0
112
+ version: "0"
113
+ requirements: []
114
+
115
+ rubyforge_project: ur-product
116
+ rubygems_version: 1.3.6
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: API wrapper for the Utbildningsradion product services
120
+ test_files: []
121
+