Sha256: 3bced8e3b48fddc7a09e9e6165359fddd99f82cdad47059deadab76bbc6cda53

Contents?: true

Size: 982 Bytes

Versions: 1

Compression:

Stored size: 982 Bytes

Contents

module Loquor
  class ApiCall::Index < ApiCall

    attr_reader :criteria

    def initialize(path)
      super(path)
      @criteria = {}
    end

    def where(value)
      value.each do |key, value|
        @criteria[key] = value
      end
      self
    end

    # Proxy everything to the results so that this this class
    # transparently acts as an Array.
    def method_missing(name, *args, &block)
      results.send(name, *args, &block)
    end

    private

    def results
      if @results.nil?
        @results = Loquor.get(generate_url)
      end
      @results
    end

    def generate_url
      query_string = @criteria.map { |key,value|
        if value.is_a?(String)
          "#{key}=#{URI.encode(value)}"
        elsif value.is_a?(Array)
          "#{key}=[#{URI.encode(value.join(","))}]"
        else
          raise LoquorError.new("Filter values must be strings or arrays.")
        end
      }.join("&")
      "#{@path}?#{query_string}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
loquor-0.0.2 lib/loquor/api_calls/index.rb