bbc 0.0.1 → 0.0.2

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/lib/bbc.rb CHANGED
@@ -1,12 +1,18 @@
1
1
  require 'rubygems'
2
2
  require 'json'
3
+ require 'json/add/core'
3
4
  require 'restclient'
4
5
 
5
- require 'bbc/radio1'
6
+ require 'bbc/client'
7
+ require 'bbc/music'
6
8
 
7
9
  require 'bbc/extensions/array'
8
10
 
9
11
  module BBC
10
- VERSION = '0.0.1'
12
+ VERSION = '0.0.2'
11
13
  API_HOST = 'http://www.bbc.co.uk'
14
+ CLIENT = BBC::Client.new
15
+
16
+ class NoDataReturnedError < StandardError ; end
17
+ class ApiHostError < StandardError ; end
12
18
  end
@@ -0,0 +1,27 @@
1
+ module BBC
2
+ class Client
3
+
4
+ def artists(gid)
5
+ request(:get, "http://www.bbc.co.uk/music/artists/#{gid}.json")['artist']
6
+ end
7
+
8
+ def now_playing
9
+ Array.wrap(request(:get, "http://www.bbc.co.uk/radio1/nowplaying/latest.json")['nowplaying'])
10
+ end
11
+
12
+ private
13
+
14
+ def parse_json_from(response)
15
+ JSON.parse(response)
16
+ rescue
17
+ raise BBC::ApiHostError
18
+ end
19
+
20
+ def request(method, url)
21
+ parse_json_from RestClient.send(method, url)
22
+ rescue RestClient::ResourceNotFound
23
+ raise BBC::ApiHostError
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,8 @@
1
+ require 'bbc/music/song'
2
+ require 'bbc/music/artist'
3
+ require 'bbc/music/release'
4
+ require 'bbc/music/link'
5
+ require 'bbc/music/review'
6
+
7
+ module Music
8
+ end
@@ -0,0 +1,54 @@
1
+ module BBC
2
+ module Music
3
+ class Artist
4
+
5
+ CLIENT = BBC::Client.new
6
+
7
+ attr_reader :name, :description, :id, :releases, :links, :reviews, :image_url, :related_artists
8
+
9
+ def self.find(id)
10
+ new(CLIENT.artists(id))
11
+ end
12
+
13
+ def initialize(params)
14
+ populate_from params
15
+ end
16
+
17
+ def get_details
18
+ unless @details_retreived
19
+ populate_from CLIENT.artists(id)
20
+ @details_retreived = true
21
+ end
22
+ end
23
+
24
+ def to_h
25
+ {
26
+ :id => id,
27
+ :name => name,
28
+ :description => description,
29
+ :releases => releases,
30
+ :reviews => reviews,
31
+ :links => links,
32
+ :image_url => image_url,
33
+ :related_artists => related_artists
34
+ }
35
+ end
36
+
37
+ def to_json(options={})
38
+ JSON.generate(to_h)
39
+ end
40
+
41
+ private
42
+
43
+ def populate_from(params)
44
+ @name = params['name']
45
+ @id = params['gid']
46
+ @releases = Array.wrap(params['releases']).map { |release| BBC::Music::Release.new(release) }
47
+ @links = Array.wrap(params['links']).map { |link| BBC::Music::Link.new(link) }
48
+ @related_artists = Array.wrap(params['related_artists']).map { |artist| BBC::Music::Artist.new(artist['artist'])}
49
+ @image_url = params['image'] ? params['image']['src'] : nil
50
+ @description = params['wikipedia_article'] ? params['wikipedia_article']['content'] : nil
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,5 +1,5 @@
1
1
  module BBC
2
- module Radio1
2
+ module Music
3
3
  class Link
4
4
 
5
5
  attr_reader :name, :url, :type
@@ -9,6 +9,18 @@ module BBC
9
9
  @url = params['url']
10
10
  @type = params['type']
11
11
  end
12
+
13
+ def to_h
14
+ {
15
+ :name => name,
16
+ :url => url,
17
+ :type => type
18
+ }
19
+ end
20
+
21
+ def to_json(options={})
22
+ JSON.generate(to_h)
23
+ end
12
24
  end
13
25
  end
14
26
  end
@@ -1,5 +1,5 @@
1
1
  module BBC
2
- module Radio1
2
+ module Music
3
3
  class Release
4
4
 
5
5
  attr_reader :name, :year, :type, :status
@@ -10,6 +10,19 @@ module BBC
10
10
  @type = params['release_type']
11
11
  @status = params['official']
12
12
  end
13
+
14
+ def to_h
15
+ {
16
+ :name => name,
17
+ :year => year,
18
+ :type => type,
19
+ :status => status
20
+ }
21
+ end
22
+
23
+ def to_json(options={})
24
+ JSON.generate(to_h)
25
+ end
13
26
  end
14
27
  end
15
28
  end
@@ -1,5 +1,5 @@
1
1
  module BBC
2
- module Radio1
2
+ module Music
3
3
  class Review
4
4
  def initialize(params)
5
5
 
@@ -0,0 +1,45 @@
1
+ module BBC
2
+ module Music
3
+ class Song
4
+
5
+ CLIENT = BBC::Client.new
6
+
7
+ attr_reader :artist_gid, :artist_name, :title
8
+
9
+ def self.playing_now
10
+ new(CLIENT.now_playing.first)
11
+ end
12
+
13
+ def self.played_recently
14
+ Array.wrap(CLIENT.now_playing.slice(1, 6)).map { |song| new(song) }
15
+ end
16
+
17
+ def initialize(params)
18
+ if params
19
+ @artist_gid = params['artist_gid']
20
+ @artist_name = params['artist']
21
+ @title = params['title']
22
+ else
23
+ raise BBC::NoDataReturnedError
24
+ end
25
+ end
26
+
27
+ def artist
28
+ @artist ||= BBC::Music::Artist.find(artist_gid)
29
+ end
30
+
31
+ def to_h
32
+ {
33
+ :artist_gid => @artist_gid,
34
+ :artist_name => @artist_name,
35
+ :title => @title
36
+ }
37
+ end
38
+
39
+ def to_json(options={})
40
+ JSON.generate(to_h)
41
+ end
42
+
43
+ end
44
+ end
45
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Oliver Nightingale
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-11 00:00:00 +01:00
17
+ date: 2010-08-16 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -75,13 +75,14 @@ extensions: []
75
75
  extra_rdoc_files: []
76
76
 
77
77
  files:
78
+ - lib/bbc/client.rb
78
79
  - lib/bbc/extensions/array.rb
79
- - lib/bbc/radio1/artist.rb
80
- - lib/bbc/radio1/link.rb
81
- - lib/bbc/radio1/release.rb
82
- - lib/bbc/radio1/review.rb
83
- - lib/bbc/radio1/song.rb
84
- - lib/bbc/radio1.rb
80
+ - lib/bbc/music/artist.rb
81
+ - lib/bbc/music/link.rb
82
+ - lib/bbc/music/release.rb
83
+ - lib/bbc/music/review.rb
84
+ - lib/bbc/music/song.rb
85
+ - lib/bbc/music.rb
85
86
  - lib/bbc.rb
86
87
  - LICENSE
87
88
  - README.md
@@ -1,8 +0,0 @@
1
- require 'bbc/radio1/song'
2
- require 'bbc/radio1/artist'
3
- require 'bbc/radio1/release'
4
- require 'bbc/radio1/link'
5
- require 'bbc/radio1/review'
6
-
7
- module Radio1
8
- end
@@ -1,40 +0,0 @@
1
- module BBC
2
- module Radio1
3
- class Artist
4
-
5
- attr_reader :name, :id, :releases, :links, :reviews, :image_url, :related_artists
6
-
7
- def self.find(id)
8
- RestClient.get("http://www.bbc.co.uk/music/artists/#{id}.json") do |r|
9
- response = JSON.parse(r)
10
- new(response['artist'])
11
- end
12
- end
13
-
14
- def initialize(params)
15
- populate_from params
16
- end
17
-
18
- def get_details
19
- unless @details_retreived
20
- RestClient.get("http://www.bbc.co.uk/music/artists/#{id}.json") do |r|
21
- response = JSON.parse(r)
22
- populate_from response['artist']
23
- @details_retreived = true
24
- end
25
- end
26
- end
27
-
28
- private
29
-
30
- def populate_from(params)
31
- @name = params['name']
32
- @id = params['gid']
33
- @releases = Array.wrap(params['releases']).map { |release| BBC::Radio1::Release.new(release) }
34
- @links = Array.wrap(params['links']).map { |link| BBC::Radio1::Link.new(link) }
35
- @related_artists = Array.wrap(params['related_artists']).map { |artist| BBC::Radio1::Artist.new(artist['artist'])}
36
- @image_url = params['image'] ? params['image']['src'] : nil
37
- end
38
- end
39
- end
40
- end
@@ -1,29 +0,0 @@
1
- module BBC
2
- module Radio1
3
- class Song
4
-
5
- NOW_PLAYING_API = "http://www.bbc.co.uk/radio1/nowplaying/latest.json"
6
-
7
- attr_reader :artist_gid, :artist, :title
8
-
9
- def self.playing_now
10
- RestClient.get(NOW_PLAYING_API) do |response|
11
- new(JSON.parse(response)['nowplaying'].first)
12
- end
13
- end
14
-
15
- def self.played_recently
16
- RestClient.get(NOW_PLAYING_API) do |response|
17
- JSON.parse(response)['nowplaying'].slice(1, 6).map { |song| new(song) }
18
- end
19
- end
20
-
21
- def initialize(params)
22
- @artist_gid = params['artist_gid']
23
- @artist = params['artist']
24
- @title = params['title']
25
- end
26
-
27
- end
28
- end
29
- end