Class: OandaAPI::Client::ResourceDescriptor
- Inherits:
-
Object
- Object
- OandaAPI::Client::ResourceDescriptor
- Defined in:
- lib/oanda_api/client/resource_descriptor.rb
Overview
Metadata about a resource request.
Constant Summary collapse
- RESOURCES_MAPPER =
Mapper for not "typical" resources. Key is a resource from the API path. Value is a hash that can contain "resource_name" from the code and/or "is_collection" (if true: will force treating response as a collection of resources, if false: will force treating response as a single resource).
{ alltransactions: { resource_name: "transaction_history", is_collection: false } }
Instance Attribute Summary collapse
-
#collection_name ⇒ Symbol
readonly
Method name that returns a collection of the resource from the API response.
-
#path ⇒ String
readonly
Path of the resource URI.
-
#resource_klass ⇒ Symbol
readonly
Class of the resource.
Instance Method Summary collapse
-
#initialize(path, method) ⇒ ResourceDescriptor
constructor
Analyzes the resource request and determines the type of resource expected from the API.
-
#is_collection? ⇒ Boolean
True if the request returns a collection.
Constructor Details
#initialize(path, method) ⇒ ResourceDescriptor
Analyzes the resource request and determines the type of resource expected from the API.
33 34 35 36 37 38 |
# File 'lib/oanda_api/client/resource_descriptor.rb', line 33 def initialize(path, method) @path = path path.match(/\/(?<resource_name>[a-z]*)\/?(?<resource_id>\w*?)$/) do |names| initialize_from_resource_name(names[:resource_name], method, names[:resource_id]) end end |
Instance Attribute Details
#collection_name ⇒ Symbol (readonly)
Returns method name that returns a collection of the resource from the API response.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/oanda_api/client/resource_descriptor.rb', line 15 class ResourceDescriptor attr_reader :collection_name, :path, :resource_klass # Mapper for not "typical" resources. # Key is a resource from the API path. # Value is a hash that can contain "resource_name" from the code and/or # "is_collection" (if true: will force treating response as a collection of resources, # if false: will force treating response as a single resource). RESOURCES_MAPPER = { alltransactions: { resource_name: "transaction_history", is_collection: false } } # Analyzes the resource request and determines the type of resource # expected from the API. # # @param [String] path a path to a resource. # # @param [Symbol] method an http verb (see {OandaAPI::Client.map_method_to_http_verb}). def initialize(path, method) @path = path path.match(/\/(?<resource_name>[a-z]*)\/?(?<resource_id>\w*?)$/) do |names| initialize_from_resource_name(names[:resource_name], method, names[:resource_id]) end end # True if the request returns a collection. # @return [Boolean] def is_collection? @is_collection end private # The resource type # @param [String] resource_name # @return [void] def resource_klass=(resource_name) klass_symbol = OandaAPI::Utils.classify(resource_name).to_sym fail ArgumentError, "Invalid resource" unless OandaAPI::Resource.constants.include?(klass_symbol) @resource_klass = OandaAPI::Resource.const_get klass_symbol end # Will set instance attributes based on resource_name, method and resource_id. # # @param [String] resource_name name of the resource (from the path). # @param [Symbol] method an http verb (see {OandaAPI::Client.map_method_to_http_verb}). # @param [Symbol] resource_id id of the resource. # @return [void] def initialize_from_resource_name(resource_name, method, resource_id) mapped_resource = RESOURCES_MAPPER.fetch(resource_name.to_sym, { resource_name: Utils.singularize(resource_name) }) self.resource_klass = mapped_resource.fetch :resource_name @is_collection = mapped_resource.fetch :is_collection, method == :get && resource_id.empty? @collection_name = Utils.pluralize(mapped_resource.fetch(:resource_name)).to_sym if is_collection? end end |
#path ⇒ String (readonly)
Returns path of the resource URI.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/oanda_api/client/resource_descriptor.rb', line 15 class ResourceDescriptor attr_reader :collection_name, :path, :resource_klass # Mapper for not "typical" resources. # Key is a resource from the API path. # Value is a hash that can contain "resource_name" from the code and/or # "is_collection" (if true: will force treating response as a collection of resources, # if false: will force treating response as a single resource). RESOURCES_MAPPER = { alltransactions: { resource_name: "transaction_history", is_collection: false } } # Analyzes the resource request and determines the type of resource # expected from the API. # # @param [String] path a path to a resource. # # @param [Symbol] method an http verb (see {OandaAPI::Client.map_method_to_http_verb}). def initialize(path, method) @path = path path.match(/\/(?<resource_name>[a-z]*)\/?(?<resource_id>\w*?)$/) do |names| initialize_from_resource_name(names[:resource_name], method, names[:resource_id]) end end # True if the request returns a collection. # @return [Boolean] def is_collection? @is_collection end private # The resource type # @param [String] resource_name # @return [void] def resource_klass=(resource_name) klass_symbol = OandaAPI::Utils.classify(resource_name).to_sym fail ArgumentError, "Invalid resource" unless OandaAPI::Resource.constants.include?(klass_symbol) @resource_klass = OandaAPI::Resource.const_get klass_symbol end # Will set instance attributes based on resource_name, method and resource_id. # # @param [String] resource_name name of the resource (from the path). # @param [Symbol] method an http verb (see {OandaAPI::Client.map_method_to_http_verb}). # @param [Symbol] resource_id id of the resource. # @return [void] def initialize_from_resource_name(resource_name, method, resource_id) mapped_resource = RESOURCES_MAPPER.fetch(resource_name.to_sym, { resource_name: Utils.singularize(resource_name) }) self.resource_klass = mapped_resource.fetch :resource_name @is_collection = mapped_resource.fetch :is_collection, method == :get && resource_id.empty? @collection_name = Utils.pluralize(mapped_resource.fetch(:resource_name)).to_sym if is_collection? end end |
#resource_klass ⇒ Symbol
Returns class of the resource.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/oanda_api/client/resource_descriptor.rb', line 15 class ResourceDescriptor attr_reader :collection_name, :path, :resource_klass # Mapper for not "typical" resources. # Key is a resource from the API path. # Value is a hash that can contain "resource_name" from the code and/or # "is_collection" (if true: will force treating response as a collection of resources, # if false: will force treating response as a single resource). RESOURCES_MAPPER = { alltransactions: { resource_name: "transaction_history", is_collection: false } } # Analyzes the resource request and determines the type of resource # expected from the API. # # @param [String] path a path to a resource. # # @param [Symbol] method an http verb (see {OandaAPI::Client.map_method_to_http_verb}). def initialize(path, method) @path = path path.match(/\/(?<resource_name>[a-z]*)\/?(?<resource_id>\w*?)$/) do |names| initialize_from_resource_name(names[:resource_name], method, names[:resource_id]) end end # True if the request returns a collection. # @return [Boolean] def is_collection? @is_collection end private # The resource type # @param [String] resource_name # @return [void] def resource_klass=(resource_name) klass_symbol = OandaAPI::Utils.classify(resource_name).to_sym fail ArgumentError, "Invalid resource" unless OandaAPI::Resource.constants.include?(klass_symbol) @resource_klass = OandaAPI::Resource.const_get klass_symbol end # Will set instance attributes based on resource_name, method and resource_id. # # @param [String] resource_name name of the resource (from the path). # @param [Symbol] method an http verb (see {OandaAPI::Client.map_method_to_http_verb}). # @param [Symbol] resource_id id of the resource. # @return [void] def initialize_from_resource_name(resource_name, method, resource_id) mapped_resource = RESOURCES_MAPPER.fetch(resource_name.to_sym, { resource_name: Utils.singularize(resource_name) }) self.resource_klass = mapped_resource.fetch :resource_name @is_collection = mapped_resource.fetch :is_collection, method == :get && resource_id.empty? @collection_name = Utils.pluralize(mapped_resource.fetch(:resource_name)).to_sym if is_collection? end end |
Instance Method Details
#is_collection? ⇒ Boolean
True if the request returns a collection.
42 43 44 |
# File 'lib/oanda_api/client/resource_descriptor.rb', line 42 def is_collection? @is_collection end |