Module: MilkCap::RTM::DataNormalization

Included in:
Task
Defined in:
lib/milk_cap/rtm/data_normalization.rb

Overview

Helper methods to normalize incoming data

Instance Method Summary collapse

Instance Method Details

#normalize_rtm_tags_hash(tags_hash) ⇒ Object

Normalize data values returned in RTM’s json ‘tags’ keys to an Array of strings.

RTM returns the value for it’s ‘tags’ key in a few formats:

When there are no tags, an empty JSON array: []

When there is one tag, a hash with “tag” key, set to a string for the tagname: { “tag”: “tagname” }

When there are multiple tags, a hash with “tag” key, set to a JSON array of strings: { “tag”: [“tagname1”, “tagname2”] }



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/milk_cap/rtm/data_normalization.rb', line 31

def normalize_rtm_tags_hash(tags_hash)
  if tags_hash.is_a?(Array)
    tags_hash
  else
    if tags_hash['tag'].is_a?(Array)
      tags_hash['tag']
    else
      [tags_hash['tag']]
    end
  end
end

#normalize_tags_array(tags) ⇒ Object

Converts various forms of tag lists into an Array of strings.

tags can be nil, a string of one or more comma separated tags, or an array. Returns an array of stripped strings.



10
11
12
13
14
15
16
17
# File 'lib/milk_cap/rtm/data_normalization.rb', line 10

def normalize_tags_array(tags)
  return [] if tags.nil?

  if tags.kind_of? String
    tags = tags.split(",")
  end
  return tags.map { |i| i.strip }
end