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::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
Instance Attribute Summary collapse
-
#location ⇒ String
The
locationheader if one is returned in an API response.
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.
18 19 20 21 |
# File 'lib/oanda_api/resource_base.rb', line 18 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.
12 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 |
# File 'lib/oanda_api/resource_base.rb', line 12 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 private # @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 # Provides additional attributes used in serialization. # @return [Hash] returns a hash of customized attributes for serialization def custom_attributes {}.tap { |hash| hash[:location] = location if location } end end |
Instance Method Details
#to_json(*args) ⇒ String
Serializes an instance as JSON
25 26 27 |
# File 'lib/oanda_api/resource_base.rb', line 25 def to_json(*args) JSON.generate @_attributes.merge(custom_attributes), *args end |