Class: Lita::Handlers::Cat
- Inherits:
-
Object
- Object
- Lita::Handlers::Cat
- 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
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(url) ⇒ Cat
constructor
A new instance of Cat.
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
#url ⇒ Object
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_categories ⇒ Object
35 36 37 |
# File 'lib/lita/handlers/cat.rb', line 35 def fetch_categories CATEGORIES.keys.map(&:to_s) end |