class RandomApi::Service

Public Class Methods

new(api_key = nil) click to toggle source
# File lib/random_api/service.rb, line 10
def initialize(api_key = nil)
  @api_key = api_key
end

Public Instance Methods

inspect() click to toggle source
# File lib/random_api/service.rb, line 37
def inspect
  "#<RandomApi::Service " + (@api_key.nil? ? "anonymous" : "setup with key") + ">"
end
to_s() click to toggle source
# File lib/random_api/service.rb, line 33
def to_s
  "RandomAPI Service " + (@api_key.nil? ? "no api key given" : @api_key)
end
user(options = {}) click to toggle source
# File lib/random_api/service.rb, line 14
def user(options = {})
  options = options.dup
  options.delete(:results) # Single user request, no need
  options = {
    query: default_query_options.merge(query_options_from(options))
  }
  RandomApi::User.new(self.class.get("/", options)["results"].first)
end
users(count, options = {}) click to toggle source
# File lib/random_api/service.rb, line 23
def users(count, options = {})
  options = options.dup
  options[:results] = count
  options = {
    query: default_query_options.merge(query_options_from(options))
  }
  results = self.class.get("/", options)["results"]
  results.map { |data| RandomApi::User.new(data) }
end

Private Instance Methods

default_query_options() click to toggle source
# File lib/random_api/service.rb, line 55
def default_query_options
  query = {
    results: 1
  }
  query[:key] = @api_key unless @api_key.nil?
  query
end
query_options_from(options) click to toggle source
# File lib/random_api/service.rb, line 43
def query_options_from(options)
  query = {}
  valid_options.each do |key|
    query[key] = options[key] if options.has_key?(key)
  end
  query
end
request_options() click to toggle source
# File lib/random_api/service.rb, line 63
def request_options
  opts = {}
  opts[:query] = {}
  opts[:query][:key] = @api_key unless @api_key.nil?
end
valid_options() click to toggle source
# File lib/random_api/service.rb, line 51
def valid_options
  @_valid_options ||= [:results, :gender, :seed]
end