module TeamSnap::Item

Public Class Methods

hashify(arr) click to toggle source
# File lib/teamsnap/item.rb, line 72
def hashify(arr)
  arr.inject({}) { |hash, (key, value)| hash[key] = value; hash }
end
load_class(type, data) click to toggle source
# File lib/teamsnap/item.rb, line 43
def load_class(type, data)
  TeamSnap.const_get(Inflecto.camelize(type), false).tap { |cls|
    if cls.include?(Virtus::Model::Core)
      cls.class_eval do
        attributes = cls.attribute_set.map(&:name)
        data
          .select { |name, _| !attributes.include?(name.to_sym) }
          .each { |name, value| attribute name, value.class }
      end
    else
      cls.class_eval do
        include Virtus.value_object

        attribute :href, String
        values do
          data.each { |name, value| attribute name, value.class }
        end
      end
    end
  }
end
load_items(client, collection) click to toggle source
# File lib/teamsnap/item.rb, line 5
def load_items(client, collection)
  collection
    .fetch(:items) { [] }
    .map { |item|
      data = parse_data(item)
        .merge(:href => item[:href])
        .merge(url_attributes(item))
      type = type_of(item)
      cls = load_class(type, data)

      cls.new(data).tap { |obj|
        obj.send(:load_links, client, item.fetch(:links) { [] })
      }
    }
end
parse_data(item) click to toggle source
# File lib/teamsnap/item.rb, line 21
def parse_data(item)
  data = item
    .fetch(:data)
    .map { |datum|
      name = datum.fetch(:name)
      value = datum.fetch(:value)
      type = datum.fetch(:type) { :default }

      value = DateTime.parse(value) if value && type == "DateTime"

      [name, value]
    }
  hashify(data)
end
type_of(item) click to toggle source
# File lib/teamsnap/item.rb, line 36
def type_of(item)
  item
    .fetch(:data)
    .find { |datum| datum.fetch(:name) == "type" }
    .fetch(:value)
end
url_attributes(item) click to toggle source
# File lib/teamsnap/item.rb, line 65
def url_attributes(item)
  links = item
    .fetch(:links) { [] }
    .map { |link| ["#{link.fetch(:rel)}_url", link.fetch(:href)] }
  hashify(links)
end

Private Instance Methods