Module: TeamSnap::Item
- Defined in:
- lib/teamsnap/item.rb
Class Method Summary collapse
- .hashify(arr) ⇒ Object
- .load_class(type, data) ⇒ Object
- .load_items(client, collection) ⇒ Object
- .parse_data(item) ⇒ Object
- .type_of(item) ⇒ Object
- .url_attributes(item) ⇒ Object
Class Method Details
.hashify(arr) ⇒ Object
72 73 74 |
# File 'lib/teamsnap/item.rb', line 72 def hashify(arr) arr.inject({}) { |hash, (key, value)| hash[key] = value; hash } end |
.load_class(type, data) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# 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) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# 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) ⇒ Object
36 37 38 39 40 41 |
# 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) ⇒ Object
65 66 67 68 69 70 |
# 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 |