Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 9219de0554
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 58 lines (45 sloc) 1.542 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
module Yql

  class Client
    
    BASE_URL = 'query.yahooapis.com'
    VERSION = 'v1'
    URL_SUFFIX = 'public/yql'
    YQL_ENV = 'http://datatables.org/alltables.env'
    
    attr_accessor :query, :diagnostics, :format
    attr_reader :version
    
    def initialize(args={})
      @diagnostics = args[:diagnostics] || false
      @version = args[:version] || VERSION
      @format = args[:format] || 'xml'
      @query = args[:query]
    end
    
    def query
      @query.kind_of?(Yql::QueryBuilder) ? @query.to_s : @query
    end
    
    def path_without_domain
      "/#{version}/#{URL_SUFFIX}"
    end
    
    def get
      if query.nil?
        raise Yql::IncompleteRequestParameter, "Query not specified"
      end
      http = Net::HTTP.new(BASE_URL, Net::HTTP.https_default_port)
      http.use_ssl = true
      Yql::Response.new(http.post(path_without_domain, parameters), format)
    end
    
    def parameters
      url_parameters = "q=#{CGI.escape(query)}&env=#{YQL_ENV}"
      url_parameters = add_format(url_parameters)
      add_diagnostics(url_parameters)
    end
    
    def add_format(existing_parameters)
      return unless existing_parameters
      return existing_parameters unless format
      return existing_parameters + "&format=#{format}"
    end
    
    def add_diagnostics(existing_parameters)
      return unless existing_parameters
      return existing_parameters unless diagnostics
      return existing_parameters + "&diagnostics=true"
    end
    
  end
  
end
Something went wrong with that request. Please try again.