Module: TAPI::Utils
- Included in:
- V3::GenericSearch
- Defined in:
- lib/tapi/v3/utils.rb
Class Method Summary collapse
- .append_query(url, hash) ⇒ Object
- .coerce_date(date) ⇒ Object
- .parse_date(date) ⇒ Object
- .symbolize_keys(data) ⇒ Object
Class Method Details
.append_query(url, hash) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/tapi/v3/utils.rb', line 27 def append_query(url, hash) pairs = hash.to_a.sort {|a, b| a.first.to_s <=> b.first.to_s} query_elements = pairs.inject([]) do |accumulator, key_value| key, value = key_value case value when Array value.each do |v| accumulator << "#{key}[]=#{v}" end else accumulator << "#{key}=#{value}" end accumulator end if query_elements.any? attributes = URI.escape(query_elements.join('&')) join_char = url.include?('?') ? '&' : '?' url + join_char + attributes else url end end |
.coerce_date(date) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/tapi/v3/utils.rb', line 7 def coerce_date(date) case date when Date then date when String then parse_date(date) when NilClass then nil else raise TypeError, "cannot coerce #{date.inspect}", caller end end |
.parse_date(date) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/tapi/v3/utils.rb', line 17 def parse_date(date) Date.strptime(date, '%d.%m.%Y') rescue ArgumentError begin Date.strptime(date, '%Y-%m-%d') rescue ArgumentError raise TypeError, "cannot parse #{date.inspect}", caller end end |
.symbolize_keys(data) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/tapi/v3/utils.rb', line 51 def symbolize_keys(data) case data when Hash data.inject({}){|acc, pair| acc[pair.first.to_sym] = symbolize_keys(pair.last); acc} when Array data.map {|e| symbolize_keys(e)} else data end end |