Sha256: 809ce54b84161854782676da31e3bd8b2d1b16f15a9751177d1bbe8caf91eb01

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

module GoogleAjax
  module Request
    HOST = 'ajax.googleapis.com'
    PATH_PREFIX = "/ajax/services/"
    attr_accessor :api_key
    attr_accessor :referer
    alias_method :referrer, :referer  # See mispelling section in http://en.wikipedia.org/wiki/HTTP_referrer
    alias_method :referrer=, :referer=

    # Api doc is at http://code.google.com/apis/ajaxsearch/documentation/reference.html#_intro_fonje
    # We use one special option (not forwarded to google):  :html_method => :post will use a Post instead of a Get.
    def get(api, method, query, args = nil)
      raise "You must assign a value to GoogleAjax.referer" unless referer
      args ||= {}
      use_post = args.delete(:http_method) == :post
      args = args.merge!('q' => query)
      args['key'] = api_key if api_key
      if use_post
        request = Net::HTTP::Post.new("#{PATH_PREFIX}#{api}/#{method}", "Referer" => referer)
        request.set_form_data(args)
      else
        request = Net::HTTP::Get.new("#{PATH_PREFIX}#{api}/#{method}?" + args.map{|k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join("&"), "Referer" => referer)
      end
      Net::HTTP.new(HOST).start do |http|
        http.request(request).body
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
googleajax-1.0.1 lib/googleajax/request.rb