neo-tmdb 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,4 +4,5 @@ require 'faraday'
4
4
  require 'tmdb'
5
5
  require 'tmdb/configuration'
6
6
  require 'tmdb/null_cache'
7
+ require 'tmdb/null_person'
7
8
  require 'tmdb/person'
@@ -1,12 +1,16 @@
1
1
  module TMDb
2
2
  extend self
3
3
 
4
+ class ServiceUnavailable < StandardError; end
5
+
4
6
  # Returns the global TMDb configuration.
7
+ #
5
8
  def configuration
6
9
  @configuration ||= Configuration.new
7
10
  end
8
11
 
9
12
  # Configure TMDb by calling this method.
13
+ #
10
14
  def configure
11
15
  yield configuration
12
16
  end
@@ -16,6 +20,10 @@ module TMDb
16
20
  # hash (parsed from the original JSON). This method is not intended to be
17
21
  # called directly by client code, instead you should call methods such as
18
22
  # +Person.find+ that return TMDb wrapper objects.
23
+ #
24
+ # Raises a +TMDb::ServiceUnavailable+ error if the service is unavailable or
25
+ # the API request limits have been exceeded.
26
+ #
19
27
  def get_api_response(path, params = {})
20
28
  configuration.cache.fetch([path, params]) do
21
29
  connection = Faraday.new(:url => 'http://api.themoviedb.org/3/') do |builder|
@@ -26,7 +34,11 @@ module TMDb
26
34
  path,
27
35
  params.merge({ :api_key => TMDb.configuration.api_key })
28
36
  )
29
- JSON.parse(response.body)
37
+ if response.status == 503
38
+ raise ServiceUnavailable
39
+ else
40
+ JSON.parse(response.body)
41
+ end
30
42
  end
31
43
  end
32
44
  end
@@ -1,8 +1,12 @@
1
1
  module TMDb
2
+
2
3
  # A module for managing TMDb-derived attributes of a class.
4
+ #
3
5
  module Attributes
6
+
4
7
  # Adds an attribute reader with the given +name+ (symbol) that delegates to
5
8
  # the instance's +@tmdb_attrs+ hash entry of the same name.
9
+ #
6
10
  def tmdb_attr(name)
7
11
  define_method(name) { @tmdb_attrs[name.to_s] }
8
12
  end
@@ -1,30 +1,35 @@
1
1
  module TMDb
2
2
  class Configuration
3
- attr_accessor :api_key, :cache
3
+ attr_accessor :api_key, :cache, :null_person
4
4
 
5
5
  def initialize
6
6
  @cache = NullCache.new
7
+ @null_person = NullPerson.new
7
8
  end
8
9
 
9
10
  # Returns the base URL for use in constructing image URLs.
11
+ #
10
12
  def image_base_url
11
13
  tmdb_config_images['base_url']
12
14
  end
13
15
 
14
16
  # Returns the list of film backdrop image sizes for use in constructing
15
17
  # image URLs.
18
+ #
16
19
  def image_backdrop_sizes
17
20
  tmdb_config_images['backdrop_sizes']
18
21
  end
19
22
 
20
23
  # Returns the list of film poster image sizes for use in constructing image
21
24
  # URLs.
25
+ #
22
26
  def image_poster_sizes
23
27
  tmdb_config_images['poster_sizes']
24
28
  end
25
29
 
26
30
  # Returns the list of person profile image sizes for use in constructing
27
31
  # image URLs.
32
+ #
28
33
  def image_profile_sizes
29
34
  tmdb_config_images['profile_sizes']
30
35
  end
@@ -0,0 +1,15 @@
1
+ module TMDb
2
+
3
+ # This class is a Null Object version of the +Person+ class.
4
+ #
5
+ class NullPerson
6
+ attr_accessor :adult, :also_known_as, :biography, :birthday,
7
+ :deathday, :homepage, :id, :name, :place_of_birth, :profile_path
8
+
9
+ attr_writer :profile_image_url
10
+
11
+ def profile_image_url(size)
12
+ @profile_image_url
13
+ end
14
+ end
15
+ end
@@ -1,4 +1,5 @@
1
1
  require 'tmdb/attributes'
2
+ require 'tmdb/null_person'
2
3
 
3
4
  module TMDb
4
5
  class Person
@@ -22,21 +23,25 @@ module TMDb
22
23
  # Returns the person with TMDb id of +id+.
23
24
  #
24
25
  def self.find(id)
25
- response = TMDb.get_api_response("person/#{id}")
26
- new(response)
26
+ begin
27
+ response = TMDb.get_api_response("person/#{id}")
28
+ new(response)
29
+ rescue ServiceUnavailable
30
+ TMDb.configuration.null_person || raise
31
+ end
27
32
  end
28
33
 
29
34
  # Returns an enumerable containing all the people matching the
30
35
  # condition hash. Currently the only condition that can be specified
31
36
  # is name, e.g.
32
37
  #
33
- # people = Person.where(:name => "Reeves")
38
+ # people = Person.where(:name => 'Reeves')
34
39
  #
35
40
  # Only the first page of results (20 people) are returned.
36
41
  #
37
42
  def self.where(args)
38
43
  response = TMDb.get_api_response('search/person', :query => args[:name])
39
- response["results"].map {|attrs| new(attrs) }
44
+ response['results'].map {|attrs| new(attrs) }
40
45
  end
41
46
 
42
47
  # Returns a URL for the person's profile image at the given +size+. Valid
@@ -1,3 +1,3 @@
1
1
  module TMDb
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neo-tmdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-27 00:00:00.000000000 Z
12
+ date: 2012-07-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: 0.8.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: aruba
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.4'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.4'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: activesupport
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -43,6 +59,22 @@ dependencies:
43
59
  - - ~>
44
60
  - !ruby/object:Gem::Version
45
61
  version: '3.2'
62
+ - !ruby/object:Gem::Dependency
63
+ name: cucumber
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '1.2'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '1.2'
46
78
  - !ruby/object:Gem::Dependency
47
79
  name: rspec
48
80
  requirement: !ruby/object:Gem::Requirement
@@ -103,10 +135,9 @@ files:
103
135
  - lib/tmdb/person.rb
104
136
  - lib/tmdb/attributes.rb
105
137
  - lib/tmdb/version.rb
138
+ - lib/tmdb/null_person.rb
106
139
  - lib/neo-tmdb.rb
107
140
  - LICENSE
108
- - Changelog.markdown
109
- - README.markdown
110
141
  homepage: https://github.com/andrewdsmith/neo-tmdb
111
142
  licenses:
112
143
  - MIT
@@ -1,39 +0,0 @@
1
- # Changelog
2
-
3
- ## In git master branch
4
-
5
- Nothing yet.
6
-
7
- ## 0.3.1 (2012-06-27)
8
-
9
- Fixes:
10
-
11
- * Return nil from `Person#profile_image_url` when `Person#profile_path` is nil.
12
-
13
- ## 0.3.0 (2012-06-25)
14
-
15
- New features:
16
-
17
- * Caching support through `Configuration#cache`.
18
-
19
- ## 0.2.0 (2012-06-15)
20
-
21
- New features:
22
-
23
- * The `Person#profile_image_url` method, which uses the base image URL fetched
24
- from the TMDb configuration.
25
- * Methods on `Configuration` for fetching and caching the [TMDb
26
- configuration][], which can be used to build image URLs from a
27
- `Person#profile_path`.
28
- * An initial implementation of the `Person.find` method for getting a person by
29
- their TMDb id.
30
-
31
- [TMDb configuration]: http://help.themoviedb.org/kb/api/configuration
32
-
33
- ## 0.1.0 (2012-06-05)
34
-
35
- New features:
36
-
37
- * An initial implementation of the `Person.where` method that searches for
38
- people on TMDb by name.
39
-
@@ -1,91 +0,0 @@
1
- # Neo TMDb
2
-
3
- Neo TMDb is a Ruby wrapper for the v3 [TMDb API][api] from www.themoviedb.org.
4
- It provides read-only access with caching.
5
-
6
- [api]: http://help.themoviedb.org/kb/api/about-3
7
-
8
- ## Use
9
-
10
- Currently you can find people by their TMDb id or search for people.
11
-
12
- ```ruby
13
- require 'neo-tmdb'
14
-
15
- TMDb.configure do |config|
16
- # You must configure this library with a TMDb API key before you can use it.
17
- config.api_key = 'my-tmdb-api-key-here'
18
- end
19
-
20
- person = TMDb::Person.find(6384)
21
- puts "#{person.name}, born #{person.birthday} in #{person.place_of_birth}"
22
- # => Keanu Reeves, born 1964-09-02 in Beirut, Lebanon
23
-
24
- smallest = TMDb.configuration.image_profile_sizes.first
25
- puts person.profile_image_url(smallest)
26
- # => http://cf2.imgobject.com/t/p/w45/jmjeALlAVaPB8SonLR3qBN5myjc.jpg
27
-
28
- # Note: Only the first 20 results are returned.
29
- people = TMDb::Person.where(:name => "Reeves")
30
- people.each do |person|
31
- # Note: Only attributes available in the search API will be populated here.
32
- puts "#{person.name} has TMDb id #{person.id}"
33
- end
34
- ```
35
-
36
- ### Configure caching
37
-
38
- You can configure caching so that duplicate requests for the same person are
39
- read from the cache rather than directly from the TMDb servers. This helps
40
- improve the performance of you application but also prevents it from exceeding
41
- the [API request limits][limits] on the TMDb servers.
42
-
43
- [limits]: http://help.themoviedb.org/kb/general/api-request-limits
44
-
45
- ```ruby
46
- require 'active_support'
47
- require 'benchmark'
48
- require 'neo-tmdb'
49
-
50
- TMDb.configure do |config|
51
- config.api_key = 'my-tmdb-api-key-here'
52
- # You should configure your cache to expire entries after an appropriate
53
- # period. Note that MemoryStore may not be the best choice for your
54
- # application.
55
- config.cache = ActiveSupport::Cache::MemoryStore.new
56
- end
57
-
58
- # Note in the following how the first request takes considerable longer than
59
- # the subsequent cached requests.
60
- 100.times do |n|
61
- Benchmark.benchmark('find ') do |b|
62
- b.report(n.to_s) do
63
- person = TMDb::Person.find(6384)
64
- puts " #{person.name} load #{n}"
65
- end
66
- end
67
- end
68
- ```
69
-
70
- You can use any cache that implements ActiveSupport's `Cache` interface.
71
-
72
- ### Documentation
73
-
74
- Further [documentation can be found on rdoc.info][docs].
75
-
76
- [docs]: http://rdoc.info/github/andrewdsmith/neo-tmdb/master/frames
77
-
78
- ## Contribute
79
-
80
- * Source hosted on [GitHub][].
81
- * Report issues on [GitHub Issues][].
82
- * Pull requests are very welcome! Please include spec coverage for every patch
83
- and create a topic branch for every separate change you make.
84
-
85
- [GitHub]: https://github.com/andrewdsmith/neo-tmdb
86
- [GitHub Issues]: https://github.com/andrewdsmith/neo-tmdb/issues
87
-
88
- ## Copyright
89
-
90
- See LICENSE for details.
91
-