Class: Xively::Datastream

Constant Summary collapse

ALLOWED_KEYS =
%w(feed_id id feed_creator current_value datapoints max_value min_value tags unit_label unit_symbol unit_type updated datapoints_function)
VALID_UNIT_TYPES =
%w(basicSI derivedSI conversionBasedUnits derivedUnits contextDependentUnits)

Instance Attribute Summary

Attributes included from Validations

#errors

Instance Method Summary collapse

Methods included from Parsers::CSV::DatastreamDefaults

#from_csv

Methods included from Parsers::XML::DatastreamDefaults

#from_xml

Methods included from Parsers::JSON::DatastreamDefaults

#from_json

Methods included from Templates::CSV::DatastreamDefaults

#generate_csv

Methods included from Templates::XML::DatastreamDefaults

#generate_xml

Methods included from Helpers

#join_tags, #parse_tag_string

Methods included from Templates::JSON::DatastreamDefaults

#generate_json

Constructor Details

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

Returns a new instance of Datastream.

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/xively-rb/datastream.rb', line 62

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...1].to_s == "<")
    self.attributes = from_xml(input)
  else
    self.attributes = from_csv(input, csv_version)
  end
end

Instance Method Details

#as_json(options = {}) ⇒ Object



107
108
109
110
# File 'lib/xively-rb/datastream.rb', line 107

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

#attributesObject



75
76
77
78
79
80
81
82
# File 'lib/xively-rb/datastream.rb', line 75

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



84
85
86
87
88
89
# File 'lib/xively-rb/datastream.rb', line 84

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

#datapointsObject



91
92
93
# File 'lib/xively-rb/datastream.rb', line 91

def datapoints
  @datapoints.nil? ? [] : @datapoints
end

#datapoints=(array) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/xively-rb/datastream.rb', line 95

def datapoints=(array)
  return unless array.is_a?(Array)
  @datapoints = []
  array.each do |datapoint|
    if datapoint.is_a?(Datapoint)
      @datapoints << datapoint
    elsif datapoint.is_a?(Hash)
      @datapoints << Datapoint.new(datapoint)
    end
  end
end

#to_csv(options = {}) ⇒ Object



121
122
123
124
# File 'lib/xively-rb/datastream.rb', line 121

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

#to_json(options = {}) ⇒ Object



112
113
114
# File 'lib/xively-rb/datastream.rb', line 112

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

#to_xml(options = {}) ⇒ Object



116
117
118
119
# File 'lib/xively-rb/datastream.rb', line 116

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

#valid?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/xively-rb/datastream.rb', line 17

def valid?
  pass = true
  [:id].each do |attr|
    if self.send(attr).blank?
      errors[attr] = ["can't be blank"]
      pass = false
    end
  end
  if !unit_type.blank?
    if !VALID_UNIT_TYPES.include?(unit_type)
      errors[:unit_type] = ["is not a valid unit_type (pick one from #{VALID_UNIT_TYPES.join(', ')} or leave blank)"]
      pass = false
    end
  end
  if current_value && current_value.length > 255
    errors[:current_value] = ["is too long (maximum is 255 characters)"]
    pass = false
  end
  if tags
    self.tags = join_tags(self.tags)
    if tags && tags.length > 255
      errors[:tags] = ["is too long (maximum is 255 characters)"]
      pass = false
    end
  end

  stream_id_regexp = RUBY_VERSION.to_f >= 1.9 ? /\A[\p{L}\w\-\+\.]+\Z/u : /\A[\w\-\+\.]+\Z/u

  unless self.id =~ stream_id_regexp
    errors[:id] = ["is invalid"]
    pass = false
  end
  if self.id.blank?
    errors[:id] = ["can't be blank"]
    pass = false
  end

  unless self.feed_id.to_s =~ /\A\d*\Z/
    errors[:feed_id] = ["is invalid"]
    pass = false
  end

  return pass
end