Class: Xively::Datapoint
- Includes:
- Parsers::JSON::DatapointDefaults, Parsers::XML::DatapointDefaults, Templates::CSV::DatapointDefaults, Templates::JSON::DatapointDefaults, Templates::XML::DatapointDefaults, Validations
- Defined in:
- lib/xively-rb/datapoint.rb
Constant Summary collapse
- ALLOWED_KEYS =
%w(feed_id datastream_id at value)
Instance Attribute Summary
Attributes included from Validations
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #attributes ⇒ Object
- #attributes=(input) ⇒ Object
-
#initialize(input = {}, format = nil) ⇒ Datapoint
constructor
A new instance of Datapoint.
- #to_csv(options = {}) ⇒ Object
- #to_json(options = {}) ⇒ Object
- #to_xml(options = {}) ⇒ Object
- #valid? ⇒ Boolean
Methods included from Parsers::XML::DatapointDefaults
Methods included from Parsers::JSON::DatapointDefaults
Methods included from Templates::CSV::DatapointDefaults
Methods included from Templates::XML::DatapointDefaults
Methods included from Templates::JSON::DatapointDefaults
Constructor Details
#initialize(input = {}, format = nil) ⇒ Datapoint
Returns a new instance of Datapoint.
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( = {}) generate_json([:version]) end |
#attributes ⇒ Object
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( = {}) generate_csv(.delete(:version), ) end |
#to_json(options = {}) ⇒ Object
59 60 61 |
# File 'lib/xively-rb/datapoint.rb', line 59 def to_json( = {}) MultiJson.dump as_json() end |
#to_xml(options = {}) ⇒ Object
63 64 65 |
# File 'lib/xively-rb/datapoint.rb', line 63 def to_xml( = {}) generate_xml([:version]) end |
#valid? ⇒ 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 |