Skip to content

Commit

Permalink
admin: provide link to TMDB entry, show raw movie data on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Jan 31, 2012
1 parent 3a038ac commit 7a7fd8e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/movie.scss
Expand Up @@ -165,6 +165,10 @@ ol.movies { clear:left; padding-left:0;
} }
} }


.movie-raw pre {
font-size: 12px
}

/* mobile */ /* mobile */
@media only screen and (max-device-width:480px) { @media only screen and (max-device-width:480px) {
ol.movies { ol.movies {
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/movies_controller.rb
Expand Up @@ -47,6 +47,18 @@ def show
def edit def edit
render :layout => !request.xhr? render :layout => !request.xhr?
end 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 def update
@movie.update_and_lock params[:movie] @movie.update_and_lock params[:movie]
Expand Down
4 changes: 4 additions & 0 deletions app/views/movies/show.html.erb
Expand Up @@ -55,6 +55,10 @@
<li><%= link_to "unlink from Netflix", link_to_netflix_movie_path(@movie.id), :method => 'put' %></li> <li><%= link_to "unlink from Netflix", link_to_netflix_movie_path(@movie.id), :method => 'put' %></li>
<% end %> <% end %>
<li><%= link_to "edit movie", edit_movie_path(@movie.id), :rel => 'facebox.edit' %></li> <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> </aside>
<% end %> <% end %>
</div> </div>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -8,6 +8,7 @@
resources :movies, :only => [:show, :edit, :update] do resources :movies, :only => [:show, :edit, :update] do
member do member do
get :wikipedia get :wikipedia
get :raw
put :change_plot_field put :change_plot_field
put :link_to_netflix put :link_to_netflix
put :add_to_watch put :add_to_watch
Expand Down
36 changes: 29 additions & 7 deletions lib/nibble_endpoints.rb
Expand Up @@ -4,7 +4,16 @@
require 'addressable/template' require 'addressable/template'


module NibbleEndpoints 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 protected

attr_reader :connection attr_reader :connection


def define_connection(url_prefix, &block) def define_connection(url_prefix, &block)
Expand All @@ -17,14 +26,31 @@ def endpoint(name, url, parser = nil)
endpoints[name.to_sym] = [template, parser] endpoints[name.to_sym] = [template, parser]
end 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 = {}) 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}" raise ArgumentError, "unknown endpoint #{endpoint.inspect}"
} }
url = template.expand(params) url = template.expand(default_params.merge(params))
response = connection.run_request(method, url, nil, nil) do |request| connection.run_request(method, url, nil, nil) do |request|
yield request if block_given? yield request if block_given?
end 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 if parser
process_response(response, parser) process_response(response, parser)
Expand All @@ -33,10 +59,6 @@ def make_request(method, endpoint, params = {})
end end
end end


def get(endpoint, params = {}, &block)
make_request(:get, endpoint, params, &block)
end

private private


def endpoints def endpoints
Expand Down
8 changes: 6 additions & 2 deletions lib/tmdb.rb
Expand Up @@ -35,6 +35,10 @@ def call(env)
conn.adapter :net_http conn.adapter :net_http
end end


default_params do
{ api_key: Movies::Application.config.tmdb.api_key }
end

class << self class << self
attr_accessor :ignore_ids attr_accessor :ignore_ids


Expand Down Expand Up @@ -93,7 +97,7 @@ def release_date=(date)
end end


def self.search query 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.movies.map! {|mov| process_movie mov unless ignore_ids.include? mov.id }.compact!
result result
end end
Expand All @@ -104,7 +108,7 @@ def self.search query
end end


def self.movie_details tmdb_id 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 process_movie result.movies.first
end end
end end
Expand Down

0 comments on commit 7a7fd8e

Please sign in to comment.