Permalink
Browse files

admin: provide link to TMDB entry, show raw movie data on demand

  • Loading branch information...
1 parent 3a038ac commit 7a7fd8e1219014d86b81c4bdf2298e3a199c2aea @mislav committed Jan 31, 2012
View
4 app/assets/stylesheets/movie.scss
@@ -165,6 +165,10 @@ ol.movies { clear:left; padding-left:0;
}
}
+.movie-raw pre {
+ font-size: 12px
+}
+
/* mobile */
@media only screen and (max-device-width:480px) {
ol.movies {
View
12 app/controllers/movies_controller.rb
@@ -47,6 +47,18 @@ def show
def edit
render :layout => !request.xhr?
end
+
+ def raw
+ @data = case params[:kind]
+ when 'tmdb'
+ resp = Tmdb.get_raw(:movie_info, tmdb_id: @movie.tmdb_id)
+ data = resp.body.first
+ data['posters'].sort_by! {|p| p['image']['width'].to_i }
+ data
+ else
+ render text: "Unsupported kind.", status: 400
+ end
+ end
def update
@movie.update_and_lock params[:movie]
View
4 app/views/movies/show.html.erb
@@ -55,6 +55,10 @@
<li><%= link_to "unlink from Netflix", link_to_netflix_movie_path(@movie.id), :method => 'put' %></li>
<% end %>
<li><%= link_to "edit movie", edit_movie_path(@movie.id), :rel => 'facebox.edit' %></li>
+ <% if @movie.tmdb_url.present? %>
+ <li><%= link_to "see TMDB entry", @movie.tmdb_url %></li>
+ <% end %>
+ <li><%= link_to "see raw: TMDB", raw_movie_path(kind: 'tmdb') %></li>
</aside>
<% end %>
</div>
View
1 config/routes.rb
@@ -8,6 +8,7 @@
resources :movies, :only => [:show, :edit, :update] do
member do
get :wikipedia
+ get :raw
put :change_plot_field
put :link_to_netflix
put :add_to_watch
View
36 lib/nibble_endpoints.rb
@@ -4,7 +4,16 @@
require 'addressable/template'
module NibbleEndpoints
+ def get(endpoint, params = {}, &block)
+ process_request(:get, endpoint, params, &block)
+ end
+
+ def get_raw(endpoint, params = {}, &block)
+ make_request(:get, endpoint, params, &block)
+ end
+
protected
+
attr_reader :connection
def define_connection(url_prefix, &block)
@@ -17,14 +26,31 @@ def endpoint(name, url, parser = nil)
endpoints[name.to_sym] = [template, parser]
end
+ def default_params(params = nil)
+ if params or block_given?
+ @default_params = params || Proc.new
+ else
+ @default_params = {} unless defined? @default_params
+ @default_params = @default_params.call if @default_params.is_a? Proc
+ @default_params
+ end
+ end
+
def make_request(method, endpoint, params = {})
- template, parser = endpoints.fetch(endpoint.to_sym) {
+ template, = endpoints.fetch(endpoint.to_sym) {
raise ArgumentError, "unknown endpoint #{endpoint.inspect}"
}
- url = template.expand(params)
- response = connection.run_request(method, url, nil, nil) do |request|
+ url = template.expand(default_params.merge(params))
+ connection.run_request(method, url, nil, nil) do |request|
yield request if block_given?
end
+ end
+
+ def process_request(method, endpoint, params = {}, &block)
+ _, parser = endpoints.fetch(endpoint.to_sym) {
+ raise ArgumentError, "unknown endpoint #{endpoint.inspect}"
+ }
+ response = make_request(method, endpoint, params, &block)
if parser
process_response(response, parser)
@@ -33,10 +59,6 @@ def make_request(method, endpoint, params = {})
end
end
- def get(endpoint, params = {}, &block)
- make_request(:get, endpoint, params, &block)
- end
-
private
def endpoints
View
8 lib/tmdb.rb
@@ -35,6 +35,10 @@ def call(env)
conn.adapter :net_http
end
+ default_params do
+ { api_key: Movies::Application.config.tmdb.api_key }
+ end
+
class << self
attr_accessor :ignore_ids
@@ -93,7 +97,7 @@ def release_date=(date)
end
def self.search query
- result = get(:movie_search, :api_key => Movies::Application.config.tmdb.api_key, :query => query)
+ result = get(:movie_search, :query => query)
result.movies.map! {|mov| process_movie mov unless ignore_ids.include? mov.id }.compact!
result
end
@@ -104,7 +108,7 @@ def self.search query
end
def self.movie_details tmdb_id
- result = get(:movie_info, :api_key => Movies::Application.config.tmdb.api_key, :tmdb_id => tmdb_id)
+ result = get(:movie_info, :tmdb_id => tmdb_id)
process_movie result.movies.first
end
end

0 comments on commit 7a7fd8e

Please sign in to comment.