Class: Forem::APIResource
- Inherits:
-
ForemObject
- Object
- ForemObject
- Forem::APIResource
- Defined in:
- lib/forem/api_resource.rb
Overview
Base class for all Forem API resource objects (articles, users, etc.).
APIResource extends ForemObject with the concepts of a canonical
resource path and the ability to refresh an instance from the API.
Concrete resource classes must define a RESOURCE_PATH constant
(e.g. "/api/articles").
Resource classes mix in Forem::APIOperations::Request so that both the class
and its instances can issue authenticated HTTP requests. Class methods
require an explicit :requestor option (normally injected by a
Client via its service objects); instance methods fall back to
the requestor stored on the object at construction time.
Direct Known Subclasses
AdminConcept, AdminUser, AgentSession, Analytics, Article, Billboard, Comment, Concept, Follow, Follower, HealthCheck, Organization, Page, PodcastEpisode, ProfileImage, Reaction, ReadingList, RecommendedArticlesList, RequestRedirect, Segment, Survey, Tag, Trend, User, Video
Instance Attribute Summary
Attributes inherited from ForemObject
Class Method Summary collapse
-
.resource_path ⇒ String
Return the API collection path for this resource class.
Instance Method Summary collapse
-
#refresh(opts = {}) ⇒ self
Reload this resource instance from the API, replacing all attributes with the latest server data.
-
#resource_url ⇒ String
Return the API path for this specific resource instance.
Methods inherited from ForemObject
#==, #[], #[]=, construct_from, cursor_list, #initialize, #inspect, #method_missing, paginated_list, #respond_to_missing?, #to_hash
Methods included from Forem::APIOperations::Request
Constructor Details
This class inherits a constructor from Forem::ForemObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Forem::ForemObject
Class Method Details
.resource_path ⇒ String
Return the API collection path for this resource class.
Delegates to the RESOURCE_PATH constant that every concrete subclass
must define.
32 33 34 |
# File 'lib/forem/api_resource.rb', line 32 def self.resource_path self::RESOURCE_PATH end |
Instance Method Details
#refresh(opts = {}) ⇒ self
Reload this resource instance from the API, replacing all attributes with the latest server data.
70 71 72 73 74 75 |
# File 'lib/forem/api_resource.rb', line 70 def refresh(opts = {}) resp = request(:get, resource_url, {}, opts) @values = {} send(:update_attributes, resp.parsed_body) self end |
#resource_url ⇒ String
Return the API path for this specific resource instance.
Combines resource_path with the instance's id attribute.
47 48 49 50 51 |
# File 'lib/forem/api_resource.rb', line 47 def resource_url id = self["id"] raise InvalidRequestError.new("Could not determine resource ID") unless id "#{self.class.resource_path}/#{id}" end |