Class: Xively::Datapoint

Constant Summary collapse

ALLOWED_KEYS =
%w(feed_id datastream_id at value)

Instance Attribute Summary

Attributes included from Validations

#errors

Instance Method Summary collapse

Methods included from Parsers::XML::DatapointDefaults

#from_xml

Methods included from Parsers::JSON::DatapointDefaults

#from_json

Methods included from Templates::CSV::DatapointDefaults

#generate_csv

Methods included from Templates::XML::DatapointDefaults

#generate_xml

Methods included from Templates::JSON::DatapointDefaults

#generate_json

Constructor Details

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

Returns a new instance of Datapoint.

Raises:



28
29
30
31
32
33
34
35
36
37
# File 'lib/xively-rb/datapoint.rb', line 28

def initialize(input = {}, format = nil)
  raise InvalidFormatError, "Unknown format specified, currently we can only parse JSON or XML." unless [nil,:json,:xml].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)
  else
    self.attributes = from_xml(input)
  end
end

Instance Method Details

#as_json(options = {}) ⇒ Object



55
56
57
# File 'lib/xively-rb/datapoint.rb', line 55

def as_json(options = {})
  generate_json(options[:version])
end

#attributesObject



39
40
41
42
43
44
45
46
# File 'lib/xively-rb/datapoint.rb', line 39

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



48
49
50
51
52
53
# File 'lib/xively-rb/datapoint.rb', line 48

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

#to_csv(options = {}) ⇒ Object



67
68
69
# File 'lib/xively-rb/datapoint.rb', line 67

def to_csv(options = {})
  generate_csv(options.delete(:version), options)
end

#to_json(options = {}) ⇒ Object



59
60
61
# File 'lib/xively-rb/datapoint.rb', line 59

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

#to_xml(options = {}) ⇒ Object



63
64
65
# File 'lib/xively-rb/datapoint.rb', line 63

def to_xml(options = {})
  generate_xml(options[:version])
end

#valid?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
# File 'lib/xively-rb/datapoint.rb', line 17

def valid?
  pass = true
  [:datastream_id, :value, :feed_id].each do |attr|
    if self.send(attr).blank?
      errors[attr] = ["can't be blank"]
      pass = false
    end
  end
  return pass
end