Class: Supercast::Resource
- Inherits:
-
DataObject
- Object
- DataObject
- Supercast::Resource
- Includes:
- Operations::Request
- Defined in:
- lib/supercast/resource.rb
Direct Known Subclasses
Creator, Episode, Invite, Role, Singleton, Subscriber, UsageAlert
Class Method Summary collapse
- .class_name ⇒ Object
-
.custom_method(name, http_verb:, http_path: nil) ⇒ Object
Adds a custom method to a resource class.
- .object_name ⇒ Object
- .resource_url ⇒ Object
- .retrieve(id, opts = {}) ⇒ Object
Instance Method Summary collapse
Methods included from Operations::Request
Methods inherited from DataObject
#==, #[], #[]=, #as_json, construct_from, #dirty!, #each, #eql?, #hash, #initialize, #inspect, #keys, #marshal_dump, #marshal_load, protected_fields, #serialize_params, #to_hash, #to_json, #to_s, #update_attributes, #values
Constructor Details
This class inherits a constructor from Supercast::DataObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Supercast::DataObject
Class Method Details
.class_name ⇒ Object
7 8 9 |
# File 'lib/supercast/resource.rb', line 7 def self.class_name name.split('::')[-1] end |
.custom_method(name, http_verb:, http_path: nil) ⇒ Object
Adds a custom method to a resource class. This is used to add support for non-CRUD API requests. custom_method takes the following parameters:
-
name: the name of the custom method to create (as a symbol)
-
http_verb: the HTTP verb for the API request (:get, :post, or :delete)
-
http_path: the path to append to the resource’s URL. If not provided,
the name is used as the path
For example, this call:
custom_method :suspend, http_verb: post
adds a ‘suspend` class method to the resource class that, when called, will send a POST request to `/<object_name>/suspend`.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/supercast/resource.rb', line 36 def self.custom_method(name, http_verb:, http_path: nil) unless %i[get patch post delete].include?(http_verb) raise ArgumentError, "Invalid http_verb value: #{http_verb.inspect}. Should be one " \ 'of :get, :patch, :post or :delete.' end http_path ||= name.to_s define_singleton_method(name) do |id, params = {}, opts = {}| url = "#{resource_url}/#{CGI.escape(id.to_s)}/#{CGI.escape(http_path)}" resp, opts = request(http_verb, url, params, opts) Util.convert_to_supercast_object(resp.data, opts) end end |
.object_name ⇒ Object
21 22 23 |
# File 'lib/supercast/resource.rb', line 21 def self.object_name self::OBJECT_NAME end |
.resource_url ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/supercast/resource.rb', line 11 def self.resource_url if self == Resource raise NotImplementedError, 'Resource is an abstract class. You should perform actions ' \ 'on its subclasses (Episode, Creator, etc.)' end "/#{self::OBJECT_NAME.downcase}s" end |
.retrieve(id, opts = {}) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/supercast/resource.rb', line 52 def self.retrieve(id, opts = {}) opts = Util.normalize_opts(opts) instance = new(id, opts) instance.refresh instance end |
Instance Method Details
#object_name ⇒ Object
70 71 72 |
# File 'lib/supercast/resource.rb', line 70 def object_name self.class::OBJECT_NAME end |
#refresh ⇒ Object
74 75 76 77 |
# File 'lib/supercast/resource.rb', line 74 def refresh resp, opts = request(:get, resource_url, @retrieve_params) initialize_from(resp.data, opts) end |
#resource_url ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/supercast/resource.rb', line 59 def resource_url unless (id = self['id']) raise InvalidRequestError.new( "Could not determine which URL to request: #{self.class} instance " \ "has invalid ID: #{id.inspect}", 'id' ) end "#{self.class.resource_url}/#{CGI.escape(id.to_s)}" end |