Sha256: 15d0cda2886c955442682df43a2e63a9e0d3d50a1d9b125e5b133830fec08a8b

Contents?: true

Size: 963 Bytes

Versions: 1

Compression:

Stored size: 963 Bytes

Contents

module Gnib
  class Response
    SOURCES = [:web, :image, :news, :spell, :phonebook, :related_search, :translation, :video]
    def initialize(body)
      @response_hash = ActiveSupport::JSON.decode(body)['SearchResponse']
    end

    def results_for(source)
      results_container = @response_hash[source.to_s.camelize]

      if results_container
        @response_hash[source.to_s.camelize]['Results'].map do |result|
          SearchResult.new result
        end
      else
        []
      end
    end

    def result_count_of(source)
      @response_hash[source.to_s.camelize]['Total'].to_i
    end

    def results
      ret = []

      SOURCES.each do |type, v|
        ret |= results_for(type)
      end

      ret
    end

    def method_missing(method, *args, &block)
      singluar_form = method.to_s.singularize.to_sym

      if SOURCES.include? singluar_form
        results_for(singluar_form)
      else
        super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gnib-0.1.0 lib/gnib/response.rb