memo_tomato 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in memo_tomato.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Memo Tomato #
2
+
3
+ Memo Tomato is an api wrapper for the http://api.rottentomatoes.com/
4
+ The gem is created to fulfil the needs of the project http://watchmemo.com
5
+
6
+ ## Installation ##
7
+
8
+ gem install memo_tomato
9
+
10
+ ## How to use it? ##
11
+
12
+ First you have to create a client with your api key
13
+
14
+ client = MemoTomato::Client.new(:key => "abcd1234")
15
+
16
+ You can search tv shows with
17
+
18
+ client.search("NAME OF THE MOVIE")
19
+
20
+ Get show info with
21
+
22
+ client.movie_info("THE ID OF THE SHOW")
23
+
24
+ Get similar movies info with
25
+
26
+ client.similar_movies("THE ID OF THE SHOW")
27
+
28
+ Get upcoming movies
29
+
30
+ client.upcoming_movies
31
+
32
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ module MemoTomato
2
+ class Actor < Node
3
+ attr_reader :name
4
+ attr_reader :characters # Array[]
5
+ end
6
+ end
@@ -0,0 +1,45 @@
1
+ module MemoTomato
2
+ class Client
3
+ attr_reader :params
4
+
5
+ def initialize(options)
6
+ @params = { :apikey => options[:apikey] }
7
+ end
8
+
9
+ def search(query)
10
+ content = get('movies', { :q => query })
11
+ parser = MemoTomato::Parser::SearchMovie.new content
12
+ parser.parse
13
+ end
14
+
15
+ def movie_info(id)
16
+ content = get("movies/#{id}")
17
+ parser = MemoTomato::Parser::MovieInfo.new content
18
+ parser.parse
19
+ end
20
+
21
+ def similar_movies(id)
22
+ content = get("movies/#{id}/similar")
23
+ parser = MemoTomato::Parser::SimilarMovies.new content
24
+ parser.parse
25
+ end
26
+
27
+ def upcoming_movies
28
+ content = get("lists/movies/upcoming")
29
+ parser = MemoTomato::Parser::UpcomingMovies.new content
30
+ parser.parse
31
+ end
32
+ private
33
+ def get(path, params = {})
34
+ HTTPClient.get build_endpoint(path), build_params(params)
35
+ end
36
+
37
+ def build_params(params = {})
38
+ @params.merge(params)
39
+ end
40
+
41
+ def build_endpoint(path)
42
+ File.join(MemoTomato::ROOT_URL, "#{path}.json")
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ module MemoTomato
2
+ class Director < Node
3
+ attr_reader :name
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module MemoTomato
2
+ class Genre < Node
3
+ attr_reader :type
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ module MemoTomato
2
+ class Movie < Node
3
+ attr_reader :id
4
+ attr_reader :title
5
+ attr_reader :year
6
+ attr_reader :mpaa_rating
7
+ attr_reader :runtime
8
+ attr_reader :release_date
9
+ attr_reader :synopsis
10
+ attr_reader :image
11
+ attr_reader :studio
12
+
13
+ attr_reader :cast # Array[] Actors
14
+ attr_reader :directors # Array[] Directors
15
+ attr_reader :genres # Array[] Genres
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module MemoTomato
2
+ class Node
3
+ def initialize (params)
4
+ return if params.nil?
5
+
6
+ params.each do |key, value|
7
+ name = key.to_s
8
+ instance_variable_set("@#{name}", value) if respond_to?(name)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,50 @@
1
+ module MemoTomato
2
+ module Parser
3
+ class Base
4
+ def initialize(content)
5
+ @content = content
6
+ end
7
+
8
+ def parse
9
+ parse_content @content
10
+ end
11
+
12
+ def parse_movie(movie)
13
+ MemoTomato::Movie.new(
14
+ :id => movie.id,
15
+ :title => movie.title,
16
+ :year => movie.year,
17
+ :mpaa_rating => movie.year,
18
+ :runtime => movie.runtime,
19
+ :release_date => movie.release_dates.theater,
20
+ :synopsis => movie.synopsis,
21
+ :image => movie.posters.detailed,
22
+ :studio => movie.studio,
23
+ :cast =>
24
+ movie.abridged_cast.collect do |actor|
25
+ MemoTomato::Actor.new(
26
+ :name => actor.name,
27
+ :characters => actor.characters
28
+ )
29
+ end,
30
+ :directors =>
31
+ unless movie.abridged_directors == nil
32
+ movie.abridged_directors.collect do |director|
33
+ MemoTomato::Director.new(
34
+ :name => director.name
35
+ )
36
+ end
37
+ end,
38
+ :genres =>
39
+ unless movie.genres == nil
40
+ movie.genres.collect do |genre|
41
+ MemoTomato::Genre.new(
42
+ :type => genre
43
+ )
44
+ end
45
+ end
46
+ )
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ module MemoTomato
2
+ module Parser
3
+ class MovieInfo < Base
4
+ def parse_content(content)
5
+ parsed_response = MultiJson.decode(content.body)
6
+ parsed_response = Hashie::Mash.new(parsed_response)
7
+
8
+ parse_movie(parsed_response)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module MemoTomato
2
+ module Parser
3
+ class SearchMovie < Base
4
+ def parse_content(content)
5
+ parsed_response = MultiJson.decode(content.body)
6
+ parsed_response = Hashie::Mash.new(parsed_response)
7
+
8
+ # In our case we want the first 30 movies
9
+ parsed_response.movies.collect do |movie|
10
+ parse_movie(movie)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ module MemoTomato
2
+ module Parser
3
+ class SimilarMovies < Base
4
+ def parse_content(content)
5
+ parsed_response = MultiJson.decode(content.body)
6
+ parsed_response = Hashie::Mash.new(parsed_response)
7
+
8
+ parsed_response.movies.collect do |movie|
9
+ parse_movie(movie)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module MemoTomato
2
+ module Parser
3
+ class UpcomingMovies < Base
4
+ def parse_content(content)
5
+ parsed_response = MultiJson.decode(content.body)
6
+ parsed_response = Hashie::Mash.new(parsed_response)
7
+
8
+ parsed_response.movies.collect do |movie|
9
+ parse_movie(movie)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ require "memo_tomato/parser/base"
2
+ require "memo_tomato/parser/search_movie"
3
+ require "memo_tomato/parser/movie_info"
4
+ require "memo_tomato/parser/similar_movies"
5
+ require "memo_tomato/parser/upcoming_movies"
6
+
@@ -0,0 +1,3 @@
1
+ module MemoTomato
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,16 @@
1
+ require 'multi_json'
2
+ require 'httpclient'
3
+ require 'hashie/mash'
4
+ require "memo_tomato/version"
5
+
6
+ module MemoTomato
7
+ ROOT_URL = "http://api.rottentomatoes.com/api/public/v1.0"
8
+ end
9
+
10
+ require "memo_tomato/node"
11
+ require "memo_tomato/client"
12
+ require "memo_tomato/parser"
13
+ require "memo_tomato/movie"
14
+ require "memo_tomato/actor"
15
+ require "memo_tomato/director"
16
+ require "memo_tomato/genre"
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "memo_tomato/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "memo_tomato"
7
+ s.version = MemoTomato::VERSION
8
+ s.authors = ["Todor Grudev", "Alex Ganov"]
9
+ s.email = ["tagrudev@gmail.com", "aganov@gmail.com"]
10
+ s.homepage = "http://www.appsbakery.eu"
11
+ s.summary = %q{Memo Tomato is an api wrapper for the http://api.rottentomatoes.com/ The gem is created to fulfil the needs of the project http://watchmemo.com}
12
+ s.description = %q{https://github.com/appsbakery/memo_tomato}
13
+
14
+ s.rubyforge_project = "memo_tomato"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_runtime_dependency "multi_json", "~> 1.0"
23
+ s.add_runtime_dependency "httpclient", ">= 2.2.0.2"
24
+ s.add_runtime_dependency "hashie", ">= 1.1.0"
25
+ s.add_development_dependency "pry", ">= 0.9.8.4"
26
+ s.add_development_dependency "rake", ">= 0.8.7"
27
+ s.add_development_dependency "rspec", ">= 2"
28
+ s.add_development_dependency "webmock", ">= 1.6.2"
29
+ s.add_development_dependency "timecop", ">= 0.3.5"
30
+ end
@@ -0,0 +1,73 @@
1
+ require 'webmock/rspec'
2
+
3
+ module WebMockHelper
4
+
5
+ # Mock http request with webmock
6
+ #
7
+ # @param [Symbol] method http method
8
+ # @param [String] path endpoint path
9
+ # @param [String] response_file json response file name/path
10
+ # @param [Hash] options optional options params
11
+ def mock_api(method, path, response_file, options = {})
12
+ stub_request(method, endpoint_for(path)).with(
13
+ request_for(method, options)
14
+ ).to_return(
15
+ response_for(response_file, options)
16
+ )
17
+
18
+ yield
19
+ end
20
+
21
+ # Load json response file and return its content
22
+ #
23
+ # @param [String] response_file json response file name/path
24
+ # @return [String]
25
+ def load_json(response_file)
26
+ File.new(File.join(File.dirname(__FILE__), '../mock_json', "#{response_file}.json"))
27
+ end
28
+
29
+ private
30
+
31
+ # Get final url for specific endpoint
32
+ #
33
+ # @param [String] path is the endpoint path ex: `showinfo`
34
+ # @return [String] the formatted endpoint, if anonymize is enabled prefixed with anonymous
35
+ def endpoint_for(path)
36
+ File.join(MemoTomato::ROOT_URL, "#{path}.json")
37
+ end
38
+
39
+ # Prepare request params for HTTPClient
40
+ #
41
+ # @param [Symbol] http method
42
+ # @param [Hash] options like params, etc
43
+ # @return [Hash] formatted http request hash
44
+ def request_for(method, options = {})
45
+ request = {}
46
+ if options[:params]
47
+ case method
48
+ when :post, :put
49
+ request[:body] = options[:params]
50
+ else
51
+ request[:query] = options[:params]
52
+ end
53
+ end
54
+ request
55
+ end
56
+
57
+ # Build response hash for HTTPClient
58
+ #
59
+ # @param [String] response_file json response file name/path
60
+ # @param [Hash] options like status, etc
61
+ # @return [Hash] formatted http response hash
62
+ def response_for(response_file, options = {})
63
+ response = {}
64
+ response[:body] = load_json(response_file)
65
+ if options[:status]
66
+ response[:status] = options[:status]
67
+ end
68
+ response
69
+ end
70
+ end
71
+
72
+ include WebMockHelper
73
+ WebMock.disable_net_connect!
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe MemoTomato::Client do
4
+ let(:client) { MemoTomato::Client.new(:apikey => 'abcd1234') }
5
+
6
+ describe '#build_params' do
7
+ it 'should merge provided params with api key' do
8
+ params = client.send :build_params, { :q => 'Jack' }
9
+ params[:apikey].should == 'abcd1234'
10
+ params[:q].should == 'Jack'
11
+ end
12
+ end
13
+
14
+ describe '#build_endpoint' do
15
+ it 'should merge path with root url' do
16
+ client.send(:build_endpoint, 'movies').should == "http://api.rottentomatoes.com/api/public/v1.0/movies.json"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ describe MemoTomato::Parser::MovieInfo do
4
+ let(:client) { MemoTomato::Client.new(:apikey => "abcd1234") }
5
+
6
+ it 'should be awesome' do
7
+ mock_api :get, 'movies/770672122' , 'movies/770672122', :params => client.params do
8
+ m = client.movie_info("770672122")
9
+ m.id.should == 770672122
10
+ m.directors.first.name.should == "Lee Unkrich"
11
+ m.genres.first.type.should == "Animation"
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ describe MemoTomato::Parser::SearchMovie do
4
+ let(:client) { MemoTomato::Client.new(:apikey => "abcd1234") }
5
+
6
+ it 'should be awesome' do
7
+ mock_api :get, 'movies', 'movies', :params => client.params.merge(:q => "Jack") do
8
+ m = client.search("Jack")
9
+ m.class.should == Array
10
+ m.count.should == 30
11
+ m.first.title.should == "Jack the Giant Killer"
12
+ m.first.release_date == "2012-06-15"
13
+ m.first.studio.should == nil
14
+ m.first.cast.first.name.should == "Nicholas Hoult"
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ describe MemoTomato::Parser::SimilarMovies do
4
+ let(:client) { MemoTomato::Client.new(:apikey => "abcd1234") }
5
+
6
+ it 'should be awesome' do
7
+ mock_api :get, 'movies/770672122/similar' , 'movies/770672122/similar', :params => client.params do
8
+ m = client.similar_movies("770672122")
9
+ m.first.id.should == "770671912"
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ describe MemoTomato::Parser::UpcomingMovies do
4
+ let(:client) { MemoTomato::Client.new(:apikey => "abcd1234") }
5
+
6
+ it 'should be awesome' do
7
+ mock_api :get, 'lists/movies/upcoming' , 'lists/movies/upcoming', :params => client.params do
8
+ m = client.upcoming_movies
9
+ m.first.id.should == "770863876"
10
+ end
11
+ end
12
+ end
13
+