Class: RandomApi::Service

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/random_api/service.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ Service

Returns a new instance of Service.



10
11
12
# File 'lib/random_api/service.rb', line 10

def initialize(api_key = nil)
  @api_key = api_key
end

Instance Method Details

#inspectObject



37
38
39
# File 'lib/random_api/service.rb', line 37

def inspect
  "#<RandomApi::Service " + (@api_key.nil? ? "anonymous" : "setup with key") + ">"
end

#to_sObject



33
34
35
# 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 = {}) ⇒ Object



14
15
16
17
18
19
20
21
# 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 = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
# 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