class ApiQueryProvider::Provider

Provides method chaining support replaces parameters in the api_path named parameters will be replaced by the assosicated value from where calls if a :where symbol is present in the string, any further values provided by where will be concatted to a key1=value,key2=value string and replaced there a value provided by limit will be replaced in to the :limit symbol if you can request additional fields or restrict the fields provided, place a :select symbol in the string

Attributes

count_constraint[R]
klass[R]
select_fields[R]
where_constraints[R]

Public Class Methods

interface() click to toggle source
# File lib/api-query-provider/provider.rb, line 91
def self.interface
  ApiQueryProvider::Provider.new(self)
end
new(base) click to toggle source
# File lib/api-query-provider/provider.rb, line 15
def initialize (base)    
  api_url = base.api_url
  api_path = base.api_path
  if base.is_a? Class
    @where_constraints = {}
    @select_fields = []
    @count_constraint = 0
    @klass = base
  else
    @where_constraints = base.where_constraints
    @select_fields = base.select_fields
    @count_constraint = base.count_constraint
  end

end
system_symbols() click to toggle source
# File lib/api-query-provider/provider.rb, line 95
def self.system_symbols
  [ :where, :count, :select ]
end

Public Instance Methods

execute() click to toggle source
# File lib/api-query-provider/provider.rb, line 83
def execute
  local_response = self.response
  
  [klass.data_selector.call(JSON.parse(local_response.body))].flatten.map do |elem|
    klass.new elem
  end
end
limit(count) click to toggle source
# File lib/api-query-provider/provider.rb, line 37
def limit(count)
  @count_constraint = count
  
  return self
end
replace_path() click to toggle source
# File lib/api-query-provider/provider.rb, line 49
def replace_path
  replaced_path = klass.api_path.dup
  
  used_keys = []
  
  @where_constraints.each do |key, value|
    if replaced_path.gsub! /:#{key}/, value.to_s
      used_keys << key
    end
  end
  
  @where_constraints.reject! { |key, value| used_keys.include? key }
  
  replaced_path.gsub! /:where/, @where_constraints.to_a.map { |e| e.join("=") }.join("&")
  replaced_path.gsub! /:select/, @select_fields.join(",")
  replaced_path.gsub! /:limit/, @count_constraint.to_s
  
  if replaced_path.include?(":")
    raise "you didn't replace all fields in the api_path"
  end
  
  replaced_path
end
response() click to toggle source
# File lib/api-query-provider/provider.rb, line 73
def response
  begin
    uri = URI.join(klass.api_url, self.replace_path)
  rescue
    raise "invalid uri"
  end
  
  HTTParty.get(uri.to_s)
end
select(*fields) click to toggle source
# File lib/api-query-provider/provider.rb, line 43
def select(*fields)
  @select_fields |= fields.flatten
  
  return self
end
where(opt = {}) click to toggle source
# File lib/api-query-provider/provider.rb, line 31
def where(opt = {})
  @where_constraints.merge! opt
  
  return self
end