Class: OandaAPI::ResourceBase
- Inherits:
-
Object
- Object
- OandaAPI::ResourceBase
- Defined in:
- lib/oanda_api/resource_base.rb
Overview
Base class for all Resources.
Direct Known Subclasses
OandaAPI::Resource::Account, OandaAPI::Resource::Candle, OandaAPI::Resource::Heartbeat, OandaAPI::Resource::Instrument, OandaAPI::Resource::Labs::CalendarEvent, OandaAPI::Resource::Labs::SpreadHistory, OandaAPI::Resource::Order, OandaAPI::Resource::Order::OrderOpened, OandaAPI::Resource::Order::TradeOpened, OandaAPI::Resource::Order::TradeReduced, OandaAPI::Resource::Position, OandaAPI::Resource::Price, OandaAPI::Resource::Trade, OandaAPI::Resource::Transaction, OandaAPI::Resource::Transaction::TradeOpened, OandaAPI::Resource::Transaction::TradeReduced, OandaAPI::Resource::TransactionHistory
Constant Summary collapse
- NOT_PLURALIZED =
List of API resources that don't follow normal REST naming standards, and are not pluralized despite being collections.
[:calendar]
Instance Attribute Summary collapse
-
#location ⇒ String
The
location
header if one is returned in an API response.
Class Method Summary collapse
-
.class_from_symbol(resource_symbol) ⇒ Nil, Class
Returns the class of a Resource if klass_symbol is the name of a resource, otherwise nil.
-
.labs_resource?(klass) ⇒ Boolean
Tests whether a class is a Labs Resource.
-
.pluralize(klass_symbol) ⇒ String
Returns a pluralized version of the resource class name.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ ResourceBase
constructor
A new instance of ResourceBase.
-
#to_json(*args) ⇒ String
Serializes an instance as JSON.
Constructor Details
#initialize(attributes = {}) ⇒ ResourceBase
Returns a new instance of ResourceBase.
19 20 21 22 |
# File 'lib/oanda_api/resource_base.rb', line 19 def initialize(attributes = {}) initialize_attributes Utils.rubyize_keys(attributes) @location = attributes.location if attributes.respond_to? :location end |
Instance Attribute Details
#location ⇒ String
Returns the location
header if one is returned in an API
response.
13 14 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/oanda_api/resource_base.rb', line 13 class ResourceBase attr_accessor :location # @param [Hash] attributes collection of resource attributes. See the # {http://developer.oanda.com/rest-live/development-guide/ Oanda Developer Guide} # for documentation about resource attributes. def initialize(attributes = {}) initialize_attributes Utils.rubyize_keys(attributes) @location = attributes.location if attributes.respond_to? :location end # Serializes an instance as JSON. # @return [String] a stringified JSON representation of an instance def to_json(*args) JSON.generate @_attributes.merge(custom_attributes), *args end # Returns the class of a Resource if klass_symbol is the name # of a resource, otherwise nil. # # @example Example: # ResourceBase.class_from_symbol(:transaction_history) => OandaAPI::Resource::TransactionHistory # ResourceBase.class_from_symbol(:calendar_event) => OandaAPI::Resource::Labs::CalendarEvent # # @param [Symbol] resource_symbol symbolized resource name # # @return [Nil] if resource_symbol is not a Resource. # # @return [Class] if resource_symbol is identifies a Resource. def self.class_from_symbol(resource_symbol) descendant resource_symbol end # Tests whether a class is a Labs Resource. # # @param [Class] klass the class to be tested. # # @return [Boolean] True if the class is a Labs Resource. def self.labs_resource?(klass) (@lab_resources ||=[]).include? klass end # List of API resources that don't follow normal REST naming standards, # and are not pluralized despite being collections. NOT_PLURALIZED = [:calendar] # Returns a pluralized version of the resource class name # @param [Symbol] klass_symbol # @return [String] the pluralized resource class name def self.pluralize(klass_symbol) NOT_PLURALIZED.include?(klass_symbol.to_sym) ? klass_symbol.to_s : OandaAPI::Utils.pluralize(klass_symbol) end private # @private # Called whenever ResourceBase class is inherited by a descendant class. # Used to track descendant classes. # Used to identify classes within the "::Labs::" namespace. def self.inherited(klass) ResourceBase.inherited(klass) unless self == OandaAPI::ResourceBase klass.to_s.match(/^(?<labs>(.*::)?Labs::)?(.*?)?(?<resource_name>(\w)+)$/) do |matched| resource_symbol = OandaAPI::Utils.underscore(matched[:resource_name]).to_sym (@descendants ||= {})[resource_symbol] = klass (@lab_resources ||= []) << klass if matched[:labs] end end # @private # Initializes attributes. # # @param [Hash] attributes collection of resource attributes. # @return [void] def initialize_attributes(attributes) @_attributes = attributes attributes.each do |key, value| send("#{key}=", value) if respond_to? key end end # @private # Provides additional attributes used in serialization. # @return [Hash] a hash of customized attributes for serialization def custom_attributes {}.tap { |hash| hash[:location] = location if location } end # @private # Returns a class name if klass_symbol is the name of a class that # descends from {OandaAPI::ResourceBase}, otherwise nil. # @param [Symbol] klass_symbol class name of a resource class # @return [Class] # @return [Nil] if klass_symbol is not a class that descends from # {OandaAPI::ResourceBase}. def self.descendant(klass_symbol) (@descendants || {})[klass_symbol] end end |
Class Method Details
.class_from_symbol(resource_symbol) ⇒ Nil, Class
Returns the class of a Resource if klass_symbol is the name of a resource, otherwise nil.
@example Example: ResourceBase.class_from_symbol(:transaction_history) => OandaAPI::Resource::TransactionHistory ResourceBase.class_from_symbol(:calendar_event) => OandaAPI::Resource::Labs::CalendarEvent
42 43 44 |
# File 'lib/oanda_api/resource_base.rb', line 42 def self.class_from_symbol(resource_symbol) descendant resource_symbol end |
.labs_resource?(klass) ⇒ Boolean
Tests whether a class is a Labs Resource.
51 52 53 |
# File 'lib/oanda_api/resource_base.rb', line 51 def self.labs_resource?(klass) (@lab_resources ||=[]).include? klass end |
.pluralize(klass_symbol) ⇒ String
Returns a pluralized version of the resource class name
62 63 64 |
# File 'lib/oanda_api/resource_base.rb', line 62 def self.pluralize(klass_symbol) NOT_PLURALIZED.include?(klass_symbol.to_sym) ? klass_symbol.to_s : OandaAPI::Utils.pluralize(klass_symbol) end |
Instance Method Details
#to_json(*args) ⇒ String
Serializes an instance as JSON.
26 27 28 |
# File 'lib/oanda_api/resource_base.rb', line 26 def to_json(*args) JSON.generate @_attributes.merge(custom_attributes), *args end |