Class: RightApi::Resource
- Inherits:
-
Object
- Object
- RightApi::Resource
- Includes:
- Helper
- Defined in:
- lib/right_api_client/resource.rb
Overview
Represents a Resource. This is a filler class for a single resource. This class dynamically adds methods and properties to instances depending on what type of resource they are.
Constant Summary
Constants included from Helper
Helper::INCONSISTENT_RESOURCE_TYPES, Helper::INSTANCE_FACING_RESOURCES, Helper::RESOURCE_SPECIAL_ACTIONS
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#href ⇒ Object
readonly
Returns the value of attribute href.
-
#resource_type ⇒ Object
readonly
Returns the value of attribute resource_type.
Class Method Summary collapse
-
.process(client, resource_type, path, data = {}) ⇒ Object
Will create a (or an array of) new Resource object(s) All parameters are treated as read only.
-
.process_detailed(client, resource_type, path, data = {}) ⇒ Object
Data may already be ‘detailed’ (i.e. has a self-href) so avoid returning an undetailed resource in that case.
Instance Method Summary collapse
-
#initialize(client, resource_type, href, hash = {}) ⇒ Resource
constructor
Hash is only used for index calls so we can parse out the name and resource_uid for the inspect call All parameters are treated as read only.
- #inspect ⇒ Object
-
#method_missing(m, *args) ⇒ Object
Any other method other than standard actions(show,update,destroy) is simply appended to the href and called with a POST.
Methods included from Helper
#add_id_and_params_to_path, #api_methods, #define_instance_method, #fix_array_of_hashes, #get_and_delete_href_from_links, #get_associated_resources, #get_href_from_links, #get_singular, #has_id, #insert_in_path, #is_singular?, #simple_singularize
Constructor Details
#initialize(client, resource_type, href, hash = {}) ⇒ Resource
Hash is only used for index calls so we can parse out the name and resource_uid for the inspect call All parameters are treated as read only
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/right_api_client/resource.rb', line 53 def initialize(client, resource_type, href, hash={}) if INCONSISTENT_RESOURCE_TYPES.has_key?(resource_type) resource_type = INCONSISTENT_RESOURCE_TYPES[resource_type] end # For the inspect function: @client = client @resource_type = resource_type @hash = hash @href = href # Add destroy method to relevant resources define_instance_method('destroy') do |*args| client.send(:do_delete, href, *args) end # Add update method to relevant resources define_instance_method('update') do |*args| client.send(:do_put, href, *args) end # Add show method to relevant resources define_instance_method('show') do |*args| RightApi::ResourceDetail.new(client, *client.send(:do_get, href, *args)) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
Any other method other than standard actions(show,update,destroy) is simply appended to the href and called with a POST.
81 82 83 |
# File 'lib/right_api_client/resource.rb', line 81 def method_missing(m, *args) client.send(:do_post, [ href, m.to_s ].join('/'), *args) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
6 7 8 |
# File 'lib/right_api_client/resource.rb', line 6 def client @client end |
#href ⇒ Object (readonly)
Returns the value of attribute href.
6 7 8 |
# File 'lib/right_api_client/resource.rb', line 6 def href @href end |
#resource_type ⇒ Object (readonly)
Returns the value of attribute resource_type.
6 7 8 |
# File 'lib/right_api_client/resource.rb', line 6 def resource_type @resource_type end |
Class Method Details
.process(client, resource_type, path, data = {}) ⇒ Object
Will create a (or an array of) new Resource object(s) All parameters are treated as read only
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/right_api_client/resource.rb', line 10 def self.process(client, resource_type, path, data={}) if data.kind_of?(Array) # This is needed for the index call to return an array of all the resources data.collect do |obj| # Ideally all objects should have a links attribute that will have a link called 'self' which is the href. # For exceptions like inputs, use the path itself. obj_href = client.get_href_from_links(obj["links"]) || path ResourceDetail.new(client, resource_type, obj_href, obj) end else RightApi::Resource.new(client, resource_type, path, data) end end |
.process_detailed(client, resource_type, path, data = {}) ⇒ Object
Data may already be ‘detailed’ (i.e. has a self-href) so avoid returning an undetailed resource in that case. this is because calling #show on the undetailed resource would generate a redundant call to
client#do_get(...)
FIX: this logic should probably be the behavior of the Resource.process() method but we are not willing to change legacy behavior for RightAPI v1.5. the RightAPI v1.6+ client should only use this logic going forward.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/right_api_client/resource.rb', line 31 def self.process_detailed(client, resource_type, path, data={}) if data.kind_of?(Array) process(client, resource_type, path, data) else if obj_href = client.get_href_from_links(data["links"]) ResourceDetail.new(client, resource_type, obj_href, data) else # no self-href means make an undetailed resource (legacy behavior). process(client, resource_type, path, data) end end end |
Instance Method Details
#inspect ⇒ Object
44 45 46 47 48 49 |
# File 'lib/right_api_client/resource.rb', line 44 def inspect "#<#{self.class.name} " + "resource_type=\"#{@resource_type}\"" + "#{', name='+@hash["name"].inspect if @hash.has_key?("name")}" + "#{', resource_uid='+@hash["resource_uid"].inspect if @hash.has_key?("resource_uid")}>" end |