Class: Lita::Handlers::Cat

Inherits:
Object
  • Object
show all
Defined in:
lib/lita/handlers/cat.rb

Constant Summary collapse

BASE_URL =
'http://api.thecatapi.com/v1'.freeze
CATEGORIES =
{
  hats: 1,
  space: 2,
  funny: 3,
  sunglasses: 4,
  boxes: 5,
  caturday: 6,
  ties: 7,
  dream: 9,
  kittens: 10,
  sinks: 14,
  clothes: 15,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Cat

Returns a new instance of Cat.



46
47
48
# File 'lib/lita/handlers/cat.rb', line 46

def initialize(url)
  @url = url
end

Instance Attribute Details

#urlObject

Returns the value of attribute url.



19
20
21
# File 'lib/lita/handlers/cat.rb', line 19

def url
  @url
end

Class Method Details

.fetch(count: nil, category: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lita/handlers/cat.rb', line 22

def fetch(count: nil, category: nil)
  url = "#{BASE_URL}/images/search?format=json"
  url += "&limit=#{count}" unless count.nil?
  if category.present?
    return [] unless valid_category?(category)
    url += "&category_ids=#{CATEGORIES[category.to_sym]}"
  end

  JSON.parse(open(url) { |io| io.read }).each_with_object([]) do |cat, cats|
    cats << new(cat["url"])
  end
end

.fetch_categoriesObject



35
36
37
# File 'lib/lita/handlers/cat.rb', line 35

def fetch_categories
  CATEGORIES.keys.map(&:to_s)
end