Sha256: 81bc462f99ea6d11a4b694e4e89157b017b4c30a492e7915090d307007832163

Contents?: true

Size: 963 Bytes

Versions: 7

Compression:

Stored size: 963 Bytes

Contents

module Imdb
  class Search
    attr_reader :query

    ##
    # Initialize a new IMDB search with the specified query
    #
    #   search = Imdb::Search.new(:title => "True Grit", :year => "1969")
    #
    def initialize(query={})
      @query = query
    end

    ##
    # Return new Imdb::Movie object with json hash as attributes
    #
    def movie
      @movie ||= parse_json
    end

    private

    ##
    # Retrive json utilizing imdbapi.com
    #
    def document
      json = Net::HTTP.get_response URI.parse(query_string(@query))
      json.body
    end

    ##
    # Bulid url from @query hash
    #
    def query_string(query)
      "http://www.imdbapi.com/?t=#{CGI::escape(query[:title])}&y=#{CGI::escape(query[:year]) unless query[:year].blank?}"
    end

    ##
    # Parse returned json and initialize new Imdb::Movie object
    #
    def parse_json
      attributes = JSON.parse(document)
      Imdb::Movie.new(attributes)
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
imdb_api-0.0.6 lib/imdb/search.rb
imdb_api-0.0.5 lib/imdb/search.rb
imdb_api-0.0.4 lib/imdb/search.rb
imdb_api-0.0.3 lib/imdb/search.rb
imdb_api-0.0.2 lib/imdb/search.rb
imdb_api-0.0.1 lib/imdb/search.rb
imdb_api-0.0.0 lib/imdb/search.rb