xively-rb 0.2.09

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. data/.gitignore +13 -0
  2. data/.rbenv-version +1 -0
  3. data/.rspec +2 -0
  4. data/.rvmrc +1 -0
  5. data/.travis.yml +7 -0
  6. data/CHANGELOG.md +91 -0
  7. data/CONTRIBUTING.md +95 -0
  8. data/Gemfile +15 -0
  9. data/LICENSE.md +13 -0
  10. data/README.md +10 -0
  11. data/Rakefile +25 -0
  12. data/ci/build_hudson.sh +24 -0
  13. data/init.rb +2 -0
  14. data/lib/xively-rb.rb +44 -0
  15. data/lib/xively-rb/array_extensions.rb +6 -0
  16. data/lib/xively-rb/base.rb +52 -0
  17. data/lib/xively-rb/base/instance_methods.rb +28 -0
  18. data/lib/xively-rb/client.rb +43 -0
  19. data/lib/xively-rb/datapoint.rb +72 -0
  20. data/lib/xively-rb/datastream.rb +127 -0
  21. data/lib/xively-rb/exceptions.rb +5 -0
  22. data/lib/xively-rb/feed.rb +109 -0
  23. data/lib/xively-rb/hash_extensions.rb +16 -0
  24. data/lib/xively-rb/helpers.rb +41 -0
  25. data/lib/xively-rb/key.rb +99 -0
  26. data/lib/xively-rb/nil_content.rb +15 -0
  27. data/lib/xively-rb/object_extensions.rb +6 -0
  28. data/lib/xively-rb/parsers/csv/datastream_defaults.rb +50 -0
  29. data/lib/xively-rb/parsers/csv/feed_defaults.rb +97 -0
  30. data/lib/xively-rb/parsers/defaults.rb +15 -0
  31. data/lib/xively-rb/parsers/json/datapoint_defaults.rb +16 -0
  32. data/lib/xively-rb/parsers/json/datastream_defaults.rb +58 -0
  33. data/lib/xively-rb/parsers/json/feed_defaults.rb +114 -0
  34. data/lib/xively-rb/parsers/json/key_defaults.rb +20 -0
  35. data/lib/xively-rb/parsers/json/search_result_defaults.rb +24 -0
  36. data/lib/xively-rb/parsers/json/trigger_defaults.rb +16 -0
  37. data/lib/xively-rb/parsers/xml/datapoint_defaults.rb +27 -0
  38. data/lib/xively-rb/parsers/xml/datastream_defaults.rb +53 -0
  39. data/lib/xively-rb/parsers/xml/errors.rb +7 -0
  40. data/lib/xively-rb/parsers/xml/feed_defaults.rb +83 -0
  41. data/lib/xively-rb/parsers/xml/helpers.rb +116 -0
  42. data/lib/xively-rb/parsers/xml/key_defaults.rb +46 -0
  43. data/lib/xively-rb/parsers/xml/trigger_defaults.rb +28 -0
  44. data/lib/xively-rb/permission.rb +67 -0
  45. data/lib/xively-rb/resource.rb +43 -0
  46. data/lib/xively-rb/search_result.rb +68 -0
  47. data/lib/xively-rb/string_extensions.rb +6 -0
  48. data/lib/xively-rb/templates/csv/datapoint_defaults.rb +22 -0
  49. data/lib/xively-rb/templates/csv/datastream_defaults.rb +43 -0
  50. data/lib/xively-rb/templates/csv/feed_defaults.rb +47 -0
  51. data/lib/xively-rb/templates/defaults.rb +14 -0
  52. data/lib/xively-rb/templates/json/datapoint_defaults.rb +15 -0
  53. data/lib/xively-rb/templates/json/datastream_defaults.rb +61 -0
  54. data/lib/xively-rb/templates/json/feed_defaults.rb +90 -0
  55. data/lib/xively-rb/templates/json/key_defaults.rb +39 -0
  56. data/lib/xively-rb/templates/json/search_result_defaults.rb +42 -0
  57. data/lib/xively-rb/templates/json/trigger_defaults.rb +21 -0
  58. data/lib/xively-rb/templates/xml/datapoint_defaults.rb +25 -0
  59. data/lib/xively-rb/templates/xml/datastream_defaults.rb +66 -0
  60. data/lib/xively-rb/templates/xml/feed_defaults.rb +112 -0
  61. data/lib/xively-rb/templates/xml/search_result_defaults.rb +118 -0
  62. data/lib/xively-rb/templates/xml_headers.rb +17 -0
  63. data/lib/xively-rb/trigger.rb +66 -0
  64. data/lib/xively-rb/validations.rb +9 -0
  65. data/lib/xively-rb/version.rb +3 -0
  66. data/spec/fixtures/models.rb +81 -0
  67. data/spec/spec_helper.rb +29 -0
  68. data/spec/support/contain_datapoint_eeml_matcher.rb +16 -0
  69. data/spec/support/contain_datastream_eeml_matcher.rb +60 -0
  70. data/spec/support/contain_feed_eeml_matcher.rb +98 -0
  71. data/spec/support/datapoint_helper.rb +53 -0
  72. data/spec/support/datastream_helper.rb +324 -0
  73. data/spec/support/describe_eeml_matcher.rb +23 -0
  74. data/spec/support/feed_helper.rb +783 -0
  75. data/spec/support/fully_represent_datapoint_matcher.rb +30 -0
  76. data/spec/support/fully_represent_datastream_matcher.rb +96 -0
  77. data/spec/support/fully_represent_feed_matcher.rb +234 -0
  78. data/spec/support/fully_represent_key_matcher.rb +74 -0
  79. data/spec/support/fully_represent_search_result_matcher.rb +67 -0
  80. data/spec/support/fully_represent_trigger_matcher.rb +29 -0
  81. data/spec/support/key_helper.rb +74 -0
  82. data/spec/support/search_result_helper.rb +252 -0
  83. data/spec/support/trigger_helper.rb +51 -0
  84. data/spec/xively-rb/array_extensions_spec.rb +9 -0
  85. data/spec/xively-rb/base/instance_methods_spec.rb +109 -0
  86. data/spec/xively-rb/base_spec.rb +56 -0
  87. data/spec/xively-rb/client_spec.rb +51 -0
  88. data/spec/xively-rb/datapoint_spec.rb +187 -0
  89. data/spec/xively-rb/datastream_spec.rb +344 -0
  90. data/spec/xively-rb/feed_spec.rb +341 -0
  91. data/spec/xively-rb/hash_extensions_spec.rb +20 -0
  92. data/spec/xively-rb/helpers_spec.rb +56 -0
  93. data/spec/xively-rb/key_spec.rb +198 -0
  94. data/spec/xively-rb/parsers/csv/datastream_defaults_spec.rb +110 -0
  95. data/spec/xively-rb/parsers/csv/feed_defaults_spec.rb +234 -0
  96. data/spec/xively-rb/parsers/json/datapoint_defaults_spec.rb +21 -0
  97. data/spec/xively-rb/parsers/json/datastream_defaults_spec.rb +105 -0
  98. data/spec/xively-rb/parsers/json/feed_defaults_spec.rb +45 -0
  99. data/spec/xively-rb/parsers/json/key_defaults_spec.rb +14 -0
  100. data/spec/xively-rb/parsers/json/search_result_defaults_spec.rb +18 -0
  101. data/spec/xively-rb/parsers/json/trigger_defaults_spec.rb +22 -0
  102. data/spec/xively-rb/parsers/xml/datapoint_defaults_spec.rb +19 -0
  103. data/spec/xively-rb/parsers/xml/datastream_defaults_spec.rb +148 -0
  104. data/spec/xively-rb/parsers/xml/feed_defaults_spec.rb +254 -0
  105. data/spec/xively-rb/parsers/xml/key_defaults_spec.rb +22 -0
  106. data/spec/xively-rb/parsers/xml/trigger_defaults_spec.rb +22 -0
  107. data/spec/xively-rb/search_result_spec.rb +257 -0
  108. data/spec/xively-rb/string_extensions_spec.rb +12 -0
  109. data/spec/xively-rb/templates/csv/datapoint_defaults_spec.rb +41 -0
  110. data/spec/xively-rb/templates/csv/datastream_defaults_spec.rb +131 -0
  111. data/spec/xively-rb/templates/csv/feed_defaults_spec.rb +78 -0
  112. data/spec/xively-rb/templates/json/datapoint_defaults_spec.rb +14 -0
  113. data/spec/xively-rb/templates/json/datastream_defaults_spec.rb +170 -0
  114. data/spec/xively-rb/templates/json/feed_defaults_spec.rb +399 -0
  115. data/spec/xively-rb/templates/json/key_defaults_spec.rb +29 -0
  116. data/spec/xively-rb/templates/json/search_result_defaults_spec.rb +37 -0
  117. data/spec/xively-rb/templates/json/trigger_defaults_spec.rb +19 -0
  118. data/spec/xively-rb/templates/xml/datapoint_defaults_spec.rb +14 -0
  119. data/spec/xively-rb/templates/xml/datastream_defaults_spec.rb +113 -0
  120. data/spec/xively-rb/templates/xml/feed_defaults_spec.rb +131 -0
  121. data/spec/xively-rb/templates/xml/search_result_defaults_spec.rb +44 -0
  122. data/spec/xively-rb/trigger_spec.rb +157 -0
  123. data/xively-rb.gemspec +41 -0
  124. metadata +333 -0
@@ -0,0 +1,68 @@
1
+ module Xively
2
+ class SearchResult
3
+ ALLOWED_KEYS = %w(totalResults startIndex itemsPerPage results)
4
+ ALLOWED_KEYS.each { |key| attr_accessor(key.to_sym) }
5
+
6
+ include Xively::Templates::JSON::SearchResultDefaults
7
+ include Xively::Templates::XML::SearchResultDefaults
8
+ include Xively::Parsers::JSON::SearchResultDefaults
9
+
10
+ @@feed_class = Xively::Feed
11
+
12
+ def initialize(input = {})
13
+ if input.is_a?(Hash)
14
+ self.attributes = input
15
+ else
16
+ self.attributes = from_json(input)
17
+ end
18
+ end
19
+
20
+ def attributes
21
+ h = {}
22
+ ALLOWED_KEYS.each do |key|
23
+ value = self.send(key)
24
+ h[key] = value unless value.nil?
25
+ end
26
+ return h
27
+ end
28
+
29
+ def attributes=(input)
30
+ return if input.nil?
31
+ ALLOWED_KEYS.each { |key| self.send("#{key}=", input[key]) }
32
+ return attributes
33
+ end
34
+
35
+ def results=(array)
36
+ return unless array.is_a?(Array)
37
+ @results = []
38
+ array.each do |feed|
39
+ if feed.is_a?(@@feed_class)
40
+ @results << feed
41
+ elsif feed.is_a?(Hash)
42
+ @results << @@feed_class.new(feed)
43
+ end
44
+ end
45
+ end
46
+
47
+ def results
48
+ return [] if @results.nil?
49
+ @results
50
+ end
51
+
52
+ def as_json(options = {})
53
+ options[:version] ||= "1.0.0"
54
+ generate_json(options[:version])
55
+ end
56
+
57
+ def to_json(options = {})
58
+ MultiJson.dump as_json(options)
59
+ end
60
+
61
+ def to_xml(options = {})
62
+ options[:version] ||= "0.5.1"
63
+ generate_xml(options[:version])
64
+ end
65
+
66
+ end
67
+ end
68
+
@@ -0,0 +1,6 @@
1
+ class String
2
+ def iso8601(precision = nil)
3
+ self
4
+ end
5
+ end
6
+
@@ -0,0 +1,22 @@
1
+ module Xively
2
+ module Templates
3
+ module CSV
4
+ module DatapointDefaults
5
+ def generate_csv(version, options = {})
6
+ options[:depth] = 4 if options[:full]
7
+ case options[:depth].to_i
8
+ when 4
9
+ Xively::CSV.generate_line([feed_id, datastream_id, at.iso8601(6), value]).strip
10
+ when 3
11
+ Xively::CSV.generate_line([datastream_id, at.iso8601(6), value]).strip
12
+ when 2
13
+ Xively::CSV.generate_line([at.iso8601(6), value]).strip
14
+ else
15
+ Xively::CSV.generate_line([value]).strip
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,43 @@
1
+ module Xively
2
+ module Templates
3
+ module CSV
4
+ module DatastreamDefaults
5
+ def generate_csv(version, options = {})
6
+ case version
7
+ when "2"
8
+ csv_2(options)
9
+ when "1"
10
+ csv_1
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def csv_2(options)
17
+ csv = []
18
+ options[:depth] = 4 if options[:full]
19
+ case options[:depth].to_i
20
+ when 4
21
+ datapoints.collect {|datapoint| csv << [feed_id, id, datapoint.at.iso8601(6), datapoint.value] }
22
+ csv << [feed_id, id, updated.iso8601(6), current_value] if csv.empty?
23
+ when 3
24
+ datapoints.collect {|datapoint| csv << [id, datapoint.at.iso8601(6), datapoint.value] }
25
+ csv << [id, updated.iso8601(6), current_value] if csv.empty?
26
+ when 1
27
+ datapoints.collect {|datapoint| csv << [datapoint.value] }
28
+ csv << [current_value] if csv.empty?
29
+ else
30
+ datapoints.collect {|datapoint| csv << [datapoint.at.iso8601(6), datapoint.value] }
31
+ csv << [updated.iso8601(6), current_value] if csv.empty?
32
+ end
33
+ csv.collect {|row| Xively::CSV.generate_line(row).strip }.join("\n")
34
+ end
35
+
36
+ def csv_1
37
+ Xively::CSV.generate_line([current_value]).strip
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+
@@ -0,0 +1,47 @@
1
+ module Xively
2
+ module Templates
3
+ module CSV
4
+ module FeedDefaults
5
+ def generate_csv(version, options = {})
6
+ case version
7
+ when "2"
8
+ csv_2(options)
9
+ when "1"
10
+ csv_1
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def csv_2(options)
17
+ csv = []
18
+ if options[:full]
19
+ datastreams.collect do |datastream|
20
+ if datastream.datapoints.any?
21
+ datastream.datapoints.collect { |datapoint| csv << Xively::CSV.generate_line([id, datastream.id, datapoint.at.iso8601(6), datapoint.value]).strip }
22
+ else
23
+ csv << Xively::CSV.generate_line([id, datastream.id, datastream.updated.iso8601(6), datastream.current_value]).strip
24
+ end
25
+
26
+
27
+ end
28
+ else
29
+ datastreams.collect do |datastream|
30
+ if datastream.datapoints.any?
31
+ datastream.datapoints.collect { |datapoint| csv << Xively::CSV.generate_line([datastream.id, datapoint.at.iso8601(6), datapoint.value]).strip }
32
+ else
33
+ csv << Xively::CSV.generate_line([datastream.id, datastream.updated.iso8601(6), datastream.current_value]).strip
34
+ end
35
+ end
36
+ end
37
+ csv.join("\n")
38
+ end
39
+
40
+ def csv_1
41
+ Xively::CSV.generate_line(datastreams.collect {|ds| ds.current_value }).strip
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,14 @@
1
+ require 'xively-rb/templates/xml_headers'
2
+ require 'xively-rb/templates/json/feed_defaults'
3
+ require 'xively-rb/templates/json/datastream_defaults'
4
+ require 'xively-rb/templates/json/datapoint_defaults'
5
+ require 'xively-rb/templates/json/trigger_defaults'
6
+ require 'xively-rb/templates/json/search_result_defaults'
7
+ require 'xively-rb/templates/json/key_defaults'
8
+ require 'xively-rb/templates/xml/feed_defaults'
9
+ require 'xively-rb/templates/xml/datastream_defaults'
10
+ require 'xively-rb/templates/xml/datapoint_defaults'
11
+ require 'xively-rb/templates/xml/search_result_defaults'
12
+ require 'xively-rb/templates/csv/datapoint_defaults'
13
+ require 'xively-rb/templates/csv/datastream_defaults'
14
+ require 'xively-rb/templates/csv/feed_defaults'
@@ -0,0 +1,15 @@
1
+ module Xively
2
+ module Templates
3
+ module JSON
4
+ module DatapointDefaults
5
+ def generate_json(version = nil)
6
+ {
7
+ :at => at.iso8601(6),
8
+ :value => value
9
+ }.delete_if_nil_value
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,61 @@
1
+ module Xively
2
+ module Templates
3
+ module JSON
4
+ module DatastreamDefaults
5
+
6
+ def generate_json(version, options={})
7
+ if version == "1.0.0"
8
+ output = json_100(options)
9
+ elsif version == "0.6-alpha"
10
+ output = json_06alpha(options)
11
+ end
12
+ output[:version] = version unless options[:hide_version]
13
+ !options[:include_blank] ? output.delete_if_nil_value : output
14
+ end
15
+
16
+ private
17
+
18
+ # As used by http://xively.com/api/v2/FEED_ID/datastreams/DATASTREAM_ID.json
19
+ def json_100(options = {})
20
+ datapoints = self.datapoints.map {|dp| {:value => dp.value, :at => dp.at.iso8601(6)}}
21
+ {
22
+ :id => self.id,
23
+ :current_value => self.current_value,
24
+ :at => self.updated.iso8601(6),
25
+ :max_value => self.max_value.to_s,
26
+ :min_value => self.min_value.to_s,
27
+ :tags => parse_tag_string(self.tags),
28
+ :unit => unit_hash(options),
29
+ :datapoints_function => datapoints_function,
30
+ :datapoints => datapoints
31
+ }
32
+ end
33
+
34
+ # As used by http://xively.com/api/v1/FEED_ID/datastreams/DATASTREAM_ID.json
35
+ def json_06alpha(options = {})
36
+ {
37
+ :id => self.id,
38
+ :values =>
39
+ [{
40
+ :recorded_at => updated.iso8601,
41
+ :value => current_value,
42
+ :max_value => max_value.to_s,
43
+ :min_value => min_value.to_s
44
+ }.delete_if_nil_value],
45
+ :tags => parse_tag_string(self.tags),
46
+ :unit => unit_hash(options),
47
+ }
48
+ end
49
+
50
+ def unit_hash(options={})
51
+ hash = { :type => unit_type,
52
+ :symbol => unit_symbol,
53
+ :label => unit_label }
54
+ !options[:include_blank] ? (hash.delete_if_nil_value if unit_type || unit_label || unit_symbol) : hash
55
+ end
56
+
57
+ end
58
+ end
59
+ end
60
+ end
61
+
@@ -0,0 +1,90 @@
1
+ module Xively
2
+ module Templates
3
+ module JSON
4
+ module FeedDefaults
5
+
6
+ include Xively::Helpers
7
+
8
+ def generate_json(version, options = {})
9
+ if version == "1.0.0"
10
+ output = json_100(options)
11
+ elsif version == "0.6-alpha"
12
+ output = json_06alpha(options)
13
+ end
14
+ !options[:include_blank] ? output.delete_if_nil_value : output
15
+ end
16
+
17
+ private
18
+
19
+ # As used by http://xively.com/api/v2/FEED_ID.json
20
+ def json_100(options = {})
21
+ {
22
+ :id => self.id,
23
+ :title => self.title,
24
+ :private => self.private.to_s,
25
+ :icon => icon,
26
+ :website => website,
27
+ :tags => parse_tag_string(tags),
28
+ :description => self.description,
29
+ :feed => (feed.blank? ? "" : "#{feed}.json"),
30
+ :auto_feed_url => auto_feed_url,
31
+ :status => status,
32
+ :updated => updated.iso8601(6),
33
+ :created => created.iso8601(6),
34
+ :email => email,
35
+ :creator => creator,
36
+ :user => ({ :login => owner_login } if owner_login),
37
+ :version => "1.0.0",
38
+ :datastreams => datastreams.map {|ds| ds.generate_json("1.0.0", options.merge({:hide_version => true}))},
39
+ :location => location_hash(options),
40
+ :product_id => product_id,
41
+ :device_serial => device_serial
42
+ }
43
+ end
44
+
45
+
46
+ # As used by http://xively.com/api/v1/FEED_ID.json
47
+ def json_06alpha(options = {})
48
+ {
49
+ :id => self.id,
50
+ :title => self.title,
51
+ :icon => icon,
52
+ :website => website,
53
+ :description => self.description,
54
+ :feed => (feed.blank? ? "" : "#{feed}.json"),
55
+ :status => status,
56
+ :updated => updated.iso8601(6),
57
+ :email => email,
58
+ :version => "0.6-alpha",
59
+ :datastreams => datastreams.map {|ds| ds.generate_json("0.6-alpha", options.merge({:hide_version => true}))},
60
+ :location => location_hash(options)
61
+ }
62
+ end
63
+
64
+ private
65
+
66
+ def location_hash(options={})
67
+ hash = { :disposition => location_disposition,
68
+ :name => location_name,
69
+ :exposure => location_exposure,
70
+ :domain => location_domain,
71
+ :ele => location_ele,
72
+ :lat => location_lat,
73
+ :lon => location_lon }
74
+ hash[:waypoints] = format_location_waypoints if location_waypoints
75
+ !options[:include_blank] ? (hash.delete_if_nil_value if location_disposition || location_name || location_exposure || location_domain || location_ele || location_lat || location_lon) : hash
76
+ end
77
+
78
+ def format_location_waypoints
79
+ output = []
80
+ location_waypoints.each{ |item|
81
+ output << item
82
+ output.last[:at] = output.last[:at].iso8601(6)
83
+ }
84
+ output
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+
@@ -0,0 +1,39 @@
1
+ module Xively
2
+ module Templates
3
+ module JSON
4
+ module KeyDefaults
5
+ def generate_json(options={})
6
+ if self.permissions
7
+ s = self.permissions.collect { |s|
8
+ if s.resources
9
+ res = s.resources.collect { |r| { :feed_id => r.feed_id, :datastream_id => r.datastream_id, :datastream_trigger_id => r.datastream_trigger_id }.delete_if_nil_value
10
+ }
11
+ end
12
+ {
13
+ :referer => s.referer,
14
+ :source_ip => s.source_ip,
15
+ :label => s.label,
16
+ :minimum_interval => s.minimum_interval,
17
+ :access_methods => s.access_methods.collect { |a| a.to_s.downcase },
18
+ :resources => res
19
+ }
20
+ }
21
+ end
22
+ output = {
23
+ :key => {
24
+ :id => id,
25
+ :expires_at => expires_at.respond_to?(:iso8601) ? expires_at.iso8601(6) : expires_at,
26
+ :api_key => key,
27
+ :user => user,
28
+ :label => label,
29
+ :private_access => private_access,
30
+ :permissions => s
31
+ }
32
+ }
33
+ !options[:include_blank] ? output.delete_if_nil_value : output
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,42 @@
1
+ module Xively
2
+ module Templates
3
+ module JSON
4
+ module SearchResultDefaults
5
+
6
+ include Helpers
7
+
8
+ def generate_json(version)
9
+ if version == "1.0.0"
10
+ json_100.delete_if_nil_value
11
+ elsif version == "0.6-alpha"
12
+ json_06alpha.delete_if_nil_value
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ # As used by http://xively.com/api/v2/feeds.json
19
+ def json_100
20
+ {
21
+ :totalResults => totalResults,
22
+ :startIndex => startIndex,
23
+ :itemsPerPage => itemsPerPage,
24
+ :results => results.collect{|f| f.generate_json("1.0.0")}
25
+ }
26
+ end
27
+
28
+ # As used by http://xively.com/api/v1/feeds.json
29
+ def json_06alpha
30
+ {
31
+ :totalResults => totalResults,
32
+ :startIndex => startIndex,
33
+ :itemsPerPage => itemsPerPage,
34
+ :results => results.collect{|f| f.generate_json("0.6-alpha")}
35
+ }
36
+ end
37
+
38
+ end
39
+ end
40
+ end
41
+ end
42
+