Module: INat::Entity::BySLUG

Includes:
INat
Included in:
Place, Project
Defined in:
lib/inat/data/entity.rb

Instance Method Summary collapse

Instance Method Details

#by_slug(slug) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/inat/data/entity.rb', line 283

def by_slug(slug)
  # Status::status '[fetch]', "#{ self } : #{ slug } ..."
  @entities ||= {}
  results = @entities.values.select { |e| e.slug == slug.intern }
  if results.empty?
    data = DB.execute "SELECT * FROM #{table} WHERE slug = ?", slug.to_s
    results = from_db_rows data
  end
  if results.empty?
    data = INat::API.get @api_path, :path, 1, slug
    results = data.map { |d| parse(d) }
  end
  # Status::status '[fetch]', "#{ self } : #{ slug } DONE"
  if results.empty?
    nil
  else
    results.first
  end
end