Sha256: 78a0bdb2ad40edc67776c2b3758f43378eaae0f5d1d654333cb2088a1f98facf

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require "addressable/uri"
require "rest_client"
require "json"

module FreebaseImporters
  class Query
    include Enumerable

    def initialize(mql, cursor = nil)
      @mql, @cursor = mql, cursor
    end

    def response
      @response ||= json_query
    end

    def result
      json_query['result']
    end

    # https://developers.google.com/freebase/v1/mql-overview#querying-with-cursor-paging-results
    def next
      if (cursor = result["cursor"])
        self.class.new(mql, cursor)
      end
    end

    def each(*args, &block)
      return enum_for(__callee__) unless block_given?
      result.each(*args, &block)
    end

    private
    attr_reader :mql, :cursor

    def base_url
      Addressable::URI.parse('https://www.googleapis.com/freebase/v1/mqlread')
    end

    def query
      # puts "About to mql: #{mql.inspect}"
      url = base_url
      url.query_values = {
        'query' => [mql].to_json,
        'cursor' => cursor
      }
      RestClient.get url.normalize.to_s, format: :json
    end

    def json_query
      JSON.parse(query)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
freebase-importers-0.0.1 lib/freebase_importers/query.rb