the_tvdb 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # TheTvdb
2
2
 
3
- Ruby wrapper for TheTVDB.com
3
+ Ruby wrapper for [TheTVDB.com](http://thetvdb.com/).
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,9 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- Instructions coming soon!
21
+ If you are using this in a Rails project, create the initializer:
22
+
23
+ rails generate the_tvdb:install
22
24
 
23
25
  ## Contributing
24
26
 
@@ -27,3 +29,7 @@ Instructions coming soon!
27
29
  3. Commit your changes (`git commit -am 'Add some feature'`)
28
30
  4. Push to the branch (`git push origin my-new-feature`)
29
31
  5. Create new Pull Request
32
+
33
+ ## License
34
+
35
+ This gem is released under the [MIT License](http://www.opensource.org/licenses/MIT). Copyright 2012 freego. http://freegoweb.it
@@ -0,0 +1,3 @@
1
+ TheTvdb.setup do |config|
2
+ config.api_key = 'ABC123DEF456GHI7' # Your API here, get one at http://thetvdb.com/?tab=apiregister
3
+ end
@@ -0,0 +1,14 @@
1
+ module TheTvdb
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+
6
+ desc "Creates a TheTvdb initializer and copy locale files to your application."
7
+
8
+ def copy_initializer
9
+ template "the_tvdb.rb", "config/initializers/the_tvdb.rb"
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -1,11 +1,13 @@
1
1
  module TheTvdb
2
-
3
- def self.config
4
- @config ||= Configuration.new
5
- end
6
-
7
2
  class Configuration
8
-
3
+
4
+ include Singleton
5
+
6
+ attr_accessor :api_key
7
+ def initialize
8
+ @api_key = ENV['TVDBKEY']
9
+ end
10
+
9
11
  def dump_path
10
12
  @dump_path ||= default_path
11
13
  FileUtils.mkdir_p("#{@dump_path}/zipfiles") if !File.directory?("#{@dump_path}/zipfiles")
@@ -9,7 +9,7 @@ module TheTvdb
9
9
  @season_number = info['Combined_season'].to_i
10
10
  @name = info['EpisodeName']
11
11
  @number = info['EpisodeNumber'].to_i
12
- @aired_at = Date.parse(info['FirstAired'])
12
+ @aired_at = info['FirstAired']
13
13
  @description = info['Overview']
14
14
  end
15
15
 
@@ -17,6 +17,14 @@ module TheTvdb
17
17
  info = TheTvdb.gateway.get_episode_details(remote_id)
18
18
  self.new(info)
19
19
  end
20
+
21
+ def to_hash
22
+ {
23
+ remote_id: @remote_id, name: @name, description: @description,
24
+ season_remote_id: @season_remote_id, season_number: @season_number,
25
+ number: @number, aired_at: @aired_at
26
+ }
27
+ end
20
28
 
21
29
  end
22
30
  end
@@ -5,15 +5,12 @@ require 'zip/zip'
5
5
  require 'active_support/core_ext'
6
6
 
7
7
  module TheTvdb
8
-
9
- def self.gateway
10
- @gateway ||= Gateway.new
11
- end
12
-
13
8
  class Gateway
14
9
 
10
+ include Singleton
11
+
15
12
  def config
16
- TheTvdb.config
13
+ TheTvdb.configuration
17
14
  end
18
15
 
19
16
  def zip_path
@@ -28,8 +25,8 @@ module TheTvdb
28
25
 
29
26
  # TODO: setup a reliable env system for apikey
30
27
  def initialize(api_key = nil)
31
- @api_key = api_key || ENV['TVDBKEY']
32
- raise 'No API key was provided. Please set one as environment variable (e.g.: `export TVDBKEY=1234567898765432`).' if !@api_key
28
+ @api_key = config.api_key
29
+ raise 'No API key was provided. Please set one as TheTvdb::Configuration.apikey or as an environment variable (e.g.: `export TVDBKEY=1234567898765432`).' if !@api_key
33
30
  @mirror = get_mirror
34
31
  end
35
32
 
@@ -48,8 +45,13 @@ module TheTvdb
48
45
  "#{hash['mirrorpath']}/api/#{@api_key}"
49
46
  end
50
47
 
51
- def get_time
52
- xml_to_hash "#{endpoint}/Updates.php?type=none", 'Time'
48
+ def update(time)
49
+ hash = xml_to_hash "#{endpoint}/Updates.php?time=#{time || last_updated}", 'Items'
50
+ end
51
+
52
+ attr_accessor :last_updated
53
+ def time
54
+ @last_updated = xml_to_hash "#{endpoint}/Updates.php?type=none", 'Time'
53
55
  end
54
56
 
55
57
  def get_series(name)
data/lib/the_tvdb/show.rb CHANGED
@@ -8,19 +8,23 @@ module TheTvdb
8
8
  shows.map {|s| self.new(s) }
9
9
  end
10
10
 
11
+ def self.find(remote_id)
12
+ info = TheTvdb.gateway.get_series_package(remote_id)
13
+ show = self.new(info['Series'])
14
+ show.episodes = info['Episode'].map {|e| TheTvdb::Episode.new(e) }
15
+ show
16
+ end
17
+
11
18
  def initialize(info)
12
- @remote_id = info['seriesid'].to_i
19
+ @remote_id = info['id'].to_i
13
20
  @name = info['SeriesName']
14
21
  @banner = info['banner']
15
22
  @description = info['Overview']
16
23
  #@aired_at = info['FirstAired']
17
24
  end
18
25
 
19
- def self.find(remote_id)
20
- info = TheTvdb.gateway.get_series_package(remote_id)
21
- show = self.new(info['Series'])
22
- show.episodes = info['Episode'].map {|e| TheTvdb::Episode.new(e) }
23
- show
26
+ def to_hash
27
+ { remote_id: @remote_id, name: @name, banner: @banner, description: @description }
24
28
  end
25
29
 
26
30
  end
@@ -1,3 +1,3 @@
1
1
  module TheTvdb
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/the_tvdb.rb CHANGED
@@ -5,5 +5,30 @@ require "the_tvdb/show"
5
5
  require "the_tvdb/episode"
6
6
 
7
7
  module TheTvdb
8
+
9
+ def self.setup
10
+ yield self.configuration
11
+ end
12
+
13
+ def self.configuration
14
+ @config ||= Configuration.instance
15
+ end
16
+
17
+ def self.gateway
18
+ @gateway ||= Gateway.instance
19
+ end
20
+
21
+ # Starting from the last update recorded on the gateway, a hash with the new
22
+ # update time and the updated shows are returned
23
+ #
24
+ # @param [Integer] timestamp the last update timestamp (if not provided the last_updated attribute of the gateway is used instead)
25
+ # @return [Hash] the new last_updated time and the Shows that have been updated
26
+ def self.update(time = nil)
27
+ update_hash = gateway.update(time)
28
+ result = { time: update_hash['Time'].to_i }
29
+ result[:shows] = update_hash['Series'].map {|show_remote_id| Show.find(show_remote_id) }
30
+ gateway.last_updated = result[:time]
31
+ result
32
+ end
8
33
 
9
34
  end
@@ -2,7 +2,7 @@
2
2
  http_interactions:
3
3
  - request:
4
4
  method: get
5
- uri: http://www.thetvdb.com/api/A4EF663990171DE4/mirrors.xml
5
+ uri: http://www.thetvdb.com/api/1234567898765432/mirrors.xml
6
6
  body:
7
7
  encoding: US-ASCII
8
8
  string: ''
@@ -48,7 +48,7 @@ http_interactions:
48
48
  recorded_at: Thu, 20 Dec 2012 10:48:57 GMT
49
49
  - request:
50
50
  method: get
51
- uri: http://thetvdb.com/api/A4EF663990171DE4/episodes/4245778/en.xml
51
+ uri: http://thetvdb.com/api/1234567898765432/episodes/4245778/en.xml
52
52
  body:
53
53
  encoding: US-ASCII
54
54
  string: ''