Module: IGMarkets::PayloadFormatter
- Defined in:
- lib/ig_markets/payload_formatter.rb
Overview
Contains methods for formatting payloads that can be passed to the IG Markets API.
Class Method Summary collapse
-
.format(model) ⇒ Hash
Takes a Model and returns its attributes in a hash ready to be passed as a payload to the IG Markets API.
-
.format_value(value, options) ⇒ String
Formats an individual value, see #format for details.
-
.snake_case_to_camel_case(value) ⇒ Symbol
Takes a string or symbol that uses snake case and converts it to a camel case symbol.
Class Method Details
.format(model) ⇒ Hash
Takes a Model and returns its attributes in a hash ready to be passed as a payload to the IG Markets API. Attribute names will be converted to use camel case rather than snake case, Symbol attributes will be converted to strings and will be uppercased, and both Date and Time attributes will be converted to strings using their ‘:format’ option.
14 15 16 17 18 19 20 21 22 |
# File 'lib/ig_markets/payload_formatter.rb', line 14 def format(model) model.class.defined_attributes.each_with_object({}) do |(name, ), formatted| value = model.send name next if value.nil? formatted[snake_case_to_camel_case(name)] = format_value value, end end |
.format_value(value, options) ⇒ String
Formats an individual value, see #format for details.
30 31 32 33 34 35 36 37 38 |
# File 'lib/ig_markets/payload_formatter.rb', line 30 def format_value(value, ) return value.to_s.upcase if [:type] == Symbol value = value.utc if [:type] == Time return value.strftime(.fetch(:format)) if [Date, Time].include? [:type] value end |
.snake_case_to_camel_case(value) ⇒ Symbol
Takes a string or symbol that uses snake case and converts it to a camel case symbol.
45 46 47 48 49 |
# File 'lib/ig_markets/payload_formatter.rb', line 45 def snake_case_to_camel_case(value) pieces = value.to_s.split '_' (pieces[0] + pieces[1..-1].map(&:capitalize).join).to_sym end |