Class: Craigslist::API::Reference
- Inherits:
-
Object
- Object
- Craigslist::API::Reference
- Defined in:
- lib/craigslist/api/reference.rb
Overview
Areas and categories, from craigslist's public reference service.
These endpoints need no authentication and return bare JSON arrays rather than the Bulkpost envelope, so they bypass JsonTransport entirely.
Both payloads are static enough to fetch once and hold — areas is around 165KB — so results are memoized per client behind a mutex.
Constant Summary collapse
- AREAS_PATH =
Public endpoint listing every area and its subareas.
"/Areas"- CATEGORIES_PATH =
Public endpoint listing every posting category.
"/Categories"
Instance Method Summary collapse
- #area(abbreviation) ⇒ Area?
- #areas ⇒ Array<Area>
- #categories ⇒ Array<Category>
- #category(abbreviation) ⇒ Category?
-
#initialize(connection) ⇒ Reference
constructor
A new instance of Reference.
-
#reload ⇒ void
Drops the cached payloads so the next call refetches.
Constructor Details
#initialize(connection) ⇒ Reference
Returns a new instance of Reference.
21 22 23 24 25 26 |
# File 'lib/craigslist/api/reference.rb', line 21 def initialize(connection) @connection = connection @mutex = Mutex.new @areas = nil @categories = nil end |
Instance Method Details
#area(abbreviation) ⇒ Area?
44 45 46 |
# File 'lib/craigslist/api/reference.rb', line 44 def area(abbreviation) areas.find { |a| a.abbreviation == abbreviation.to_s } end |
#areas ⇒ Array<Area>
29 30 31 32 33 |
# File 'lib/craigslist/api/reference.rb', line 29 def areas @mutex.synchronize do @areas ||= get(AREAS_PATH).map { |entry| Area.from(entry) }.freeze end end |
#categories ⇒ Array<Category>
36 37 38 39 40 |
# File 'lib/craigslist/api/reference.rb', line 36 def categories @mutex.synchronize do @categories ||= get(CATEGORIES_PATH).map { |entry| Category.from(entry) }.freeze end end |
#category(abbreviation) ⇒ Category?
50 51 52 |
# File 'lib/craigslist/api/reference.rb', line 50 def category(abbreviation) categories.find { |c| c.abbreviation == abbreviation.to_s } end |
#reload ⇒ void
This method returns an undefined value.
Drops the cached payloads so the next call refetches.
57 58 59 60 61 62 |
# File 'lib/craigslist/api/reference.rb', line 57 def reload @mutex.synchronize do @areas = nil @categories = nil end end |