Class: Cosm::Feed

Constant Summary collapse

ALLOWED_KEYS =

The order of these keys is the order attributes are assigned in. (i.e. id should come before datastreams)

%w(id creator owner_login datastreams description email feed icon location_disposition location_domain location_ele location_exposure location_lat location_lon location_name location_waypoints private status tags title updated created website auto_feed_url product_id device_serial csv_version)

Instance Attribute Summary

Attributes included from Validations

#errors

Instance Method Summary collapse

Methods included from Parsers::CSV::FeedDefaults

detect_version, #from_csv

Methods included from Parsers::XML::FeedDefaults

#from_xml

Methods included from Parsers::JSON::FeedDefaults

#from_json

Methods included from Helpers

#join_tags, #parse_tag_string

Methods included from Templates::CSV::FeedDefaults

#generate_csv

Methods included from Templates::XML::FeedDefaults

#generate_xml

Methods included from Templates::JSON::FeedDefaults

#generate_json

Constructor Details

#initialize(input = {}, csv_version = nil, format = nil) ⇒ Feed

Returns a new instance of Feed.

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cosm-rb/feed.rb', line 41

def initialize(input = {}, csv_version = nil, format = nil)
  raise InvalidFormatError, "Unknown format specified, currently we can only parse JSON, XML or CSV." unless [nil,:json,:xml,:csv].include?(format)
  if input.is_a?(Hash)
    self.attributes = input
  elsif format == :json || (format.nil? && input.strip[0...1].to_s == "{")
    self.attributes = from_json(input)
  elsif format == :xml || (format.nil? && input.strip[0...5].to_s == "<?xml" || input.strip[0...5].to_s == "<eeml")
    self.attributes = from_xml(input)
  else
    self.attributes = from_csv(input, csv_version)
  end
end

Instance Method Details

#as_json(options = {}) ⇒ Object



87
88
89
90
# File 'lib/cosm-rb/feed.rb', line 87

def as_json(options = {})
  options[:version] ||= "1.0.0"
  generate_json(options.delete(:version), options)
end

#attributesObject



54
55
56
57
58
59
60
61
# File 'lib/cosm-rb/feed.rb', line 54

def attributes
  h = {}
  ALLOWED_KEYS.each do |key|
    value = self.send(key)
    h[key] = value unless value.nil?
  end
  return h
end

#attributes=(input) ⇒ Object



68
69
70
71
72
73
# File 'lib/cosm-rb/feed.rb', line 68

def attributes=(input)
  return if input.nil?
  input.deep_stringify_keys!
  ALLOWED_KEYS.each { |key| self.send("#{key}=", input[key]) }
  return attributes
end

#datastreamsObject



63
64
65
66
# File 'lib/cosm-rb/feed.rb', line 63

def datastreams
  return [] if @datastreams.nil?
  @datastreams
end

#datastreams=(array) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cosm-rb/feed.rb', line 75

def datastreams=(array)
  return unless array.is_a?(Array)
  @datastreams = []
  array.each do |datastream|
    if datastream.is_a?(Datastream)
      @datastreams << datastream
    elsif datastream.is_a?(Hash)
      @datastreams << Datastream.new(datastream)
    end
  end
end

#to_csv(options = {}) ⇒ Object



101
102
103
104
# File 'lib/cosm-rb/feed.rb', line 101

def to_csv(options = {})
  options[:version] ||= "2"
  generate_csv(options.delete(:version), options)
end

#to_json(options = {}) ⇒ Object



92
93
94
# File 'lib/cosm-rb/feed.rb', line 92

def to_json(options = {})
  MultiJson.dump as_json(options)
end

#to_xml(options = {}) ⇒ Object



96
97
98
99
# File 'lib/cosm-rb/feed.rb', line 96

def to_xml(options = {})
  options[:version] ||= "0.5.1"
  generate_xml(options.delete(:version), options)
end

#valid?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cosm-rb/feed.rb', line 16

def valid?
  pass = true
  if title.blank?
    errors[:title] = ["can't be blank"]
    pass = false
  end

  duplicate_datastream_ids = datastreams.inject({}) {|h,v| h[v.id]=h[v.id].to_i+1; h}.reject{|k,v| v==1}.keys
  if duplicate_datastream_ids.any?
    errors[:datastreams] = ["can't have duplicate IDs: #{duplicate_datastream_ids.join(',')}"]
    pass = false
  end

  datastreams.each do |ds|
    unless ds.valid?
      ds.errors.each { |attr, ds_errors|
        errors["datastreams_#{attr}".to_sym] = ([*errors["datastreams_#{attr}".to_sym]] | [*ds_errors]).compact
      }
      pass = false
    end
  end

  return pass
end