cosm-rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/.gitignore +12 -0
  2. data/.rbenv-version +1 -0
  3. data/.rspec +2 -0
  4. data/.rvmrc +1 -0
  5. data/.travis.yml +10 -0
  6. data/CHANGELOG.md +91 -0
  7. data/Gemfile +4 -0
  8. data/MIT-LICENSE +20 -0
  9. data/README.md +195 -0
  10. data/Rakefile +35 -0
  11. data/ci/build_hudson.sh +24 -0
  12. data/cosm-rb.gemspec +40 -0
  13. data/init.rb +2 -0
  14. data/lib/cosm-rb/array_extensions.rb +6 -0
  15. data/lib/cosm-rb/base/instance_methods.rb +28 -0
  16. data/lib/cosm-rb/base.rb +52 -0
  17. data/lib/cosm-rb/client.rb +7 -0
  18. data/lib/cosm-rb/datapoint.rb +71 -0
  19. data/lib/cosm-rb/datastream.rb +136 -0
  20. data/lib/cosm-rb/feed.rb +108 -0
  21. data/lib/cosm-rb/hash_extensions.rb +16 -0
  22. data/lib/cosm-rb/helpers.rb +42 -0
  23. data/lib/cosm-rb/key.rb +98 -0
  24. data/lib/cosm-rb/nil_content.rb +15 -0
  25. data/lib/cosm-rb/object_extensions.rb +6 -0
  26. data/lib/cosm-rb/parsers/csv/datastream_defaults.rb +12 -0
  27. data/lib/cosm-rb/parsers/csv/feed_defaults.rb +38 -0
  28. data/lib/cosm-rb/parsers/defaults.rb +13 -0
  29. data/lib/cosm-rb/parsers/json/datapoint_defaults.rb +12 -0
  30. data/lib/cosm-rb/parsers/json/datastream_defaults.rb +53 -0
  31. data/lib/cosm-rb/parsers/json/feed_defaults.rb +103 -0
  32. data/lib/cosm-rb/parsers/json/key_defaults.rb +15 -0
  33. data/lib/cosm-rb/parsers/json/search_result_defaults.rb +18 -0
  34. data/lib/cosm-rb/parsers/json/trigger_defaults.rb +12 -0
  35. data/lib/cosm-rb/parsers/xml/datapoint_defaults.rb +20 -0
  36. data/lib/cosm-rb/parsers/xml/datastream_defaults.rb +77 -0
  37. data/lib/cosm-rb/parsers/xml/feed_defaults.rb +127 -0
  38. data/lib/cosm-rb/parsers/xml/key_defaults.rb +40 -0
  39. data/lib/cosm-rb/parsers/xml/trigger_defaults.rb +22 -0
  40. data/lib/cosm-rb/permission.rb +67 -0
  41. data/lib/cosm-rb/resource.rb +43 -0
  42. data/lib/cosm-rb/search_result.rb +63 -0
  43. data/lib/cosm-rb/string_extensions.rb +6 -0
  44. data/lib/cosm-rb/template.rb +32 -0
  45. data/lib/cosm-rb/templates/csv/datapoint_defaults.rb +22 -0
  46. data/lib/cosm-rb/templates/csv/datastream_defaults.rb +43 -0
  47. data/lib/cosm-rb/templates/csv/feed_defaults.rb +47 -0
  48. data/lib/cosm-rb/templates/defaults.rb +14 -0
  49. data/lib/cosm-rb/templates/json/datapoint_defaults.rb +15 -0
  50. data/lib/cosm-rb/templates/json/datastream_defaults.rb +66 -0
  51. data/lib/cosm-rb/templates/json/feed_defaults.rb +134 -0
  52. data/lib/cosm-rb/templates/json/key_defaults.rb +41 -0
  53. data/lib/cosm-rb/templates/json/search_result_defaults.rb +47 -0
  54. data/lib/cosm-rb/templates/json/trigger_defaults.rb +21 -0
  55. data/lib/cosm-rb/templates/xml/datapoint_defaults.rb +25 -0
  56. data/lib/cosm-rb/templates/xml/datastream_defaults.rb +66 -0
  57. data/lib/cosm-rb/templates/xml/feed_defaults.rb +110 -0
  58. data/lib/cosm-rb/templates/xml/search_result_defaults.rb +116 -0
  59. data/lib/cosm-rb/templates/xml_headers.rb +17 -0
  60. data/lib/cosm-rb/trigger.rb +65 -0
  61. data/lib/cosm-rb/validations.rb +9 -0
  62. data/lib/cosm-rb/version.rb +3 -0
  63. data/lib/cosm-rb.rb +29 -0
  64. data/spec/cosm-rb/array_extensions_spec.rb +9 -0
  65. data/spec/cosm-rb/base/instance_methods_spec.rb +109 -0
  66. data/spec/cosm-rb/base_spec.rb +56 -0
  67. data/spec/cosm-rb/client_spec.rb +7 -0
  68. data/spec/cosm-rb/datapoint_spec.rb +169 -0
  69. data/spec/cosm-rb/datastream_spec.rb +301 -0
  70. data/spec/cosm-rb/feed_spec.rb +298 -0
  71. data/spec/cosm-rb/hash_extensions_spec.rb +20 -0
  72. data/spec/cosm-rb/helpers_spec.rb +56 -0
  73. data/spec/cosm-rb/key_spec.rb +178 -0
  74. data/spec/cosm-rb/parsers/csv/datastream_defaults_spec.rb +12 -0
  75. data/spec/cosm-rb/parsers/csv/feed_defaults_spec.rb +35 -0
  76. data/spec/cosm-rb/parsers/json/datapoint_defaults_spec.rb +15 -0
  77. data/spec/cosm-rb/parsers/json/datastream_defaults_spec.rb +82 -0
  78. data/spec/cosm-rb/parsers/json/feed_defaults_spec.rb +18 -0
  79. data/spec/cosm-rb/parsers/json/key_defaults_spec.rb +8 -0
  80. data/spec/cosm-rb/parsers/json/search_result_defaults_spec.rb +12 -0
  81. data/spec/cosm-rb/parsers/json/trigger_defaults_spec.rb +16 -0
  82. data/spec/cosm-rb/parsers/xml/datapoint_defaults_spec.rb +19 -0
  83. data/spec/cosm-rb/parsers/xml/datastream_defaults_spec.rb +63 -0
  84. data/spec/cosm-rb/parsers/xml/feed_defaults_spec.rb +65 -0
  85. data/spec/cosm-rb/parsers/xml/key_defaults_spec.rb +16 -0
  86. data/spec/cosm-rb/parsers/xml/trigger_defaults_spec.rb +16 -0
  87. data/spec/cosm-rb/search_result_spec.rb +258 -0
  88. data/spec/cosm-rb/string_extensions_spec.rb +12 -0
  89. data/spec/cosm-rb/template_spec.rb +74 -0
  90. data/spec/cosm-rb/templates/csv/datapoint_defaults_spec.rb +41 -0
  91. data/spec/cosm-rb/templates/csv/datastream_defaults_spec.rb +131 -0
  92. data/spec/cosm-rb/templates/csv/feed_defaults_spec.rb +78 -0
  93. data/spec/cosm-rb/templates/json/datapoint_defaults_spec.rb +14 -0
  94. data/spec/cosm-rb/templates/json/datastream_defaults_spec.rb +170 -0
  95. data/spec/cosm-rb/templates/json/feed_defaults_spec.rb +397 -0
  96. data/spec/cosm-rb/templates/json/key_defaults_spec.rb +29 -0
  97. data/spec/cosm-rb/templates/json/search_result_defaults_spec.rb +37 -0
  98. data/spec/cosm-rb/templates/json/trigger_defaults_spec.rb +19 -0
  99. data/spec/cosm-rb/templates/xml/datapoint_defaults_spec.rb +14 -0
  100. data/spec/cosm-rb/templates/xml/datastream_defaults_spec.rb +113 -0
  101. data/spec/cosm-rb/templates/xml/feed_defaults_spec.rb +131 -0
  102. data/spec/cosm-rb/templates/xml/search_result_defaults_spec.rb +44 -0
  103. data/spec/cosm-rb/trigger_spec.rb +117 -0
  104. data/spec/fixtures/models.rb +81 -0
  105. data/spec/spec_helper.rb +17 -0
  106. data/spec/support/contain_datapoint_eeml_matcher.rb +16 -0
  107. data/spec/support/contain_datastream_eeml_matcher.rb +60 -0
  108. data/spec/support/contain_feed_eeml_matcher.rb +96 -0
  109. data/spec/support/datapoint_helper.rb +53 -0
  110. data/spec/support/datastream_helper.rb +300 -0
  111. data/spec/support/describe_eeml_matcher.rb +23 -0
  112. data/spec/support/feed_helper.rb +771 -0
  113. data/spec/support/fully_represent_datapoint_matcher.rb +30 -0
  114. data/spec/support/fully_represent_datastream_matcher.rb +92 -0
  115. data/spec/support/fully_represent_feed_matcher.rb +226 -0
  116. data/spec/support/fully_represent_key_matcher.rb +74 -0
  117. data/spec/support/fully_represent_search_result_matcher.rb +67 -0
  118. data/spec/support/fully_represent_trigger_matcher.rb +29 -0
  119. data/spec/support/key_helper.rb +74 -0
  120. data/spec/support/search_result_helper.rb +252 -0
  121. data/spec/support/trigger_helper.rb +51 -0
  122. metadata +363 -0
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ .DS_Store
2
+ .bundle/*
3
+ *.swp
4
+ *.swo
5
+ Gemfile.lock
6
+ .gem/*
7
+ build_artefacts/*
8
+ coverage/*
9
+ debug.log
10
+ test.sqlite3.db
11
+ *.gem
12
+ *.orig
data/.rbenv-version ADDED
@@ -0,0 +1 @@
1
+ 1.8.7-p249
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format documentation
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create 1.8.7-p249@pachube_data_formats
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ branches:
2
+ only:
3
+ - cosm-rb
4
+ - master
5
+ - version_0.3.xx_branch
6
+ language: ruby
7
+ rvm:
8
+ - 1.8.7
9
+ - 1.9.2
10
+ - 1.9.3
data/CHANGELOG.md ADDED
@@ -0,0 +1,91 @@
1
+ # Changelog
2
+
3
+ ## 0.1.11
4
+ * Keys always have a root node of 'keys'
5
+
6
+ ## 0.1.10
7
+ * Adding label field to Keys
8
+ * should pass options through to Key#as_json
9
+ * Clearing up some confusion with trigger/key parsers
10
+ * Improving the Key.to_json error handling
11
+
12
+ ## 0.1.09
13
+ * Adding Key model for api keys, XML/JSON parsers, and JSON template
14
+
15
+ ## 0.1.08
16
+ * Rewrote tag string parser
17
+
18
+ ## 0.1.07
19
+ * Adding some validations on feed_id for datastreams and datapoints
20
+
21
+ ## 0.1.06
22
+ * Feeds should invalidate with duplicate datastream ids or invalid datastreams
23
+ * Fixed a bug in Template#method_missing
24
+
25
+ ## 0.1.05
26
+ * Added CSV feed parsing with autodetection of version
27
+
28
+ ## 0.1.04
29
+ * Added user node parsers to Feed eeml 0.5.1 and json 1.0.0
30
+
31
+ ## 0.1.03
32
+ * Added owner_login and owner_user_level to Feed
33
+ * user node added to json 1.0.0 and eeml 0.5.1 templates for Feed
34
+
35
+ ## 0.1.02
36
+ * Bug fix release
37
+ * Handling empty (nil) datastreams in json 1.0.0 (feeds and search results)
38
+
39
+ ## 0.1.01
40
+ * Major release that
41
+ * Eliminates requirement for ActiveSupport / ActiveModel
42
+ * PachubeDataFormat objects have basic validation
43
+
44
+ ## 0.0.3
45
+ * Ignore nil tags with json parsers
46
+ * Added templates for datastream level eeml 005 and 0.5.1
47
+ * Added templates for EEML 005 and 0.5.1 (v1 / v2)
48
+ * Eeml 0.5.1 and 5 datastream parser
49
+ * Eeml 0.5.1 and 5 feed parser
50
+ * Providing as_pachube_xml on AR records
51
+ * Adding creator to default xml parsers
52
+ * encode xml in utf-8
53
+ * Adding feed_id and feed_creator onto datastream (used by xml)
54
+ * Creator is always http://www.haque.co.uk in v1 xml
55
+ * Creator always http://www.haque.co.uk in v1 templates
56
+ * First pass at a Datapoint model w/ parsers and templates
57
+ * Added datapoints to json parser
58
+ * Creating datapoint templates
59
+ * Templates append format onto feed url
60
+ * order of xml attributes
61
+ * Hash#delete_if_nil_value
62
+ * Completely ignore unit nodes in xml if no unit attributes present
63
+ * iso8601 shouldn't be to precision of v2 api ignore nil.iso8601 calls
64
+ * Allow excluding datastreams
65
+ * Hooking up datapoints into ActiveRecord
66
+ * Bug fix: Datapoint json template wasn't formatting time to iso8601(6)
67
+ * datapoint templates need no version as there is only one
68
+ * moving private node
69
+ * Hide min/max value nodes on eeml 0.5.1 templates
70
+ * Simplifying ActiveRecord hooks Updated documentation
71
+ * Datapoint templates should iso8601 at Feed json template datapoints in iso8601(6)
72
+ * Added json template for search results
73
+ * XML and JSON templates for search results
74
+ * Adding opensearch namespace and nodes
75
+ * Merge branch 'hotfix-0.0.2' into develop
76
+ * Incorporating hotfix changes into search results
77
+ * Restructure of lib
78
+ * Basic csv for datapoints (full and minimal)
79
+ * Basic csv for datastreams and feeds * V1 * V2 * V2 full
80
+ * Allows options to be passed to to_csv
81
+ * KICK: Keep it consistent knucklehead
82
+ * Datastream#to_csv shows datapoints * options[:both] will include datastream as first element * Will only show datastream if no datapoints * Will only show all datapoints if any
83
+ * Escaping csv cells properly
84
+ * Handling datapoints in feed#to_csv
85
+ * Adding depth option to datapoint#to_csv
86
+ * Adding depth option to datastream#to_csv
87
+ * Ignoring units in eeml if no attributes present
88
+ * Refactoring specs
89
+ * Handle empty tags in xml
90
+ * Ignore primary nodes in xml feed templates
91
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ # Gem dependencies specified in pachube_data_formats.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2011 by Connected Enviroments Ltd.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+
20
+
data/README.md ADDED
@@ -0,0 +1,195 @@
1
+ [![Build Status](https://secure.travis-ci.org/pachube/pachube_data_formats.png)](http://travis-ci.org/pachube/pachube_data_formats)
2
+
3
+ Cosm gem
4
+ ========================
5
+
6
+ WORK IN PROGRESS
7
+
8
+ This gem will convert between Cosm Data Formats.
9
+ You can use it to prepare data for sending to Cosm or for parsing data received from Cosm.
10
+
11
+ Allowed inputs
12
+ --------------
13
+
14
+ * XML (EEML)
15
+ * JSON
16
+ * CSV
17
+
18
+ Outputs
19
+ -------
20
+
21
+ * XML (EEML)
22
+ * JSON
23
+ * CSV
24
+
25
+ Cosm Interface
26
+ ----------------------------
27
+
28
+ If you have your own model that maps to a Cosm Feed (such as an ActiveRecord model), this plugin will provide many convenience methods to convert your objects into Cosm objects.
29
+
30
+ ### Example with ActiveRecord
31
+
32
+ class Datastream < ActiveRecord::Base
33
+ belongs_to :feed
34
+ end
35
+
36
+ class Feed < ActiveRecord::Base
37
+ has_many :datastreams
38
+ extend Cosm::Base
39
+ is_cosm :feed
40
+ end
41
+
42
+ ### Provided methods
43
+
44
+ @cosm_feed = feed.to_cosm # returns an instance of Cosm::Feed
45
+ @cosm_feed.to_json(:version => "1.0.0") # converts your feed and associated datastreams into Cosm V2 JSON
46
+ @cosm_feed.as_json(:version => "0.6-alpha") # provides a json hash for 0.6-alpha
47
+ @cosm_feed.to_xml(:version => "0.5.1") # converts your feed and associated datastreams into Cosm V2 XML (EEML)
48
+
49
+ ### Supported formats
50
+
51
+ * JSON "1.0.0" - used by Cosm API v2
52
+ * JSON "0.6-alpha" - used by Cosm API v1
53
+ * XML "0.5.1" - used by Cosm API v2
54
+ * XML "5" - used by Cosm API v1
55
+ * CSV v1 - used by Cosm API v1
56
+ * CSV v2 - used by Cosm API v2
57
+
58
+ ### Mapped fields
59
+
60
+ See [the Cosm Api docs] [1] for a description of each field.
61
+
62
+ [1]: https://cosm.com/docs/v2/ "Cosm Api Docs"
63
+
64
+ By default the gem expects your object to have the following fields:
65
+
66
+ #### Feeds
67
+
68
+ * creator
69
+ * datastreams
70
+ * description
71
+ * email
72
+ * feed
73
+ * icon
74
+ * id
75
+ * location_disposition
76
+ * location_domain
77
+ * location_ele
78
+ * location_exposure
79
+ * location_lat
80
+ * location_lon
81
+ * location_name
82
+ * private
83
+ * status
84
+ * tags
85
+ * title
86
+ * updated
87
+ * website
88
+
89
+
90
+ #### Datastreams
91
+
92
+ * current_value
93
+ * datapoints
94
+ * feed_creator
95
+ * feed_id
96
+ * id
97
+ * max_value
98
+ * min_value
99
+ * tags
100
+ * unit_label
101
+ * unit_symbol
102
+ * unit_type
103
+ * updated
104
+
105
+ #### Datapoints
106
+
107
+ * at
108
+ * value
109
+ * feed_id
110
+ * datastream_id
111
+
112
+ If you use different field names, want to map custom fields or want to map fields onto instance methods you can:
113
+
114
+ class Feed < ActiveRecord::Base
115
+ extend Cosm::Base
116
+
117
+ has_one :geo_location
118
+ is_cosm :feed, {:location_lat => :geo_lat, :location_lon => :geo_lon}
119
+
120
+ def geo_lat
121
+ geo_location.try(:latitude)
122
+ end
123
+
124
+ def geo_lon
125
+ geo_location.try(:longitude)
126
+ end
127
+ end
128
+
129
+ Examples using the Cosm objects
130
+ --------------------------------------------
131
+
132
+ feed = Cosm::Feed.new('{"title":"Cosm Office Environment"}')
133
+ feed.as_json # {"title" => "Cosm Office Environment"}
134
+ feed.to_json # {"title":"Cosm Office Environment"}
135
+ feed.to_xml
136
+ # <?xml version="1.0" encoding="UTF-8"?
137
+ # <eeml xmlns="http://www.eeml.org/xsd/0.5.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="0.5.1" xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd">
138
+ # <environment updated="2010-11-11T15:57:17.637887Z" id="12567" creator="https://cosm.com">
139
+ # <title>Cosm Office Environment</title>
140
+ # </environment>
141
+ # </eeml>
142
+ feed.attributes # {:title => "Cosm Office Environment"}
143
+
144
+ ### Parsing a Datastream using json
145
+
146
+ json = '
147
+ {
148
+ "max_value": "658.0",
149
+ "current_value": "14",
150
+ "datapoints": [{
151
+ "value": "1",
152
+ "at": "2011-03-02T15:59:56.895922Z"
153
+ },
154
+ {
155
+ "value": "1",
156
+ "at": "2011-03-02T16:00:07.188648Z"
157
+ },
158
+ {
159
+ "value": "2",
160
+ "at": "2011-03-02T16:00:18.416500Z"
161
+ }],
162
+ "min_value": "0.0",
163
+ "id": "0",
164
+ "tags": ["humidity", "Temperature", "freakin lasers"],
165
+ "version": "1.0.0",
166
+ "unit": {
167
+ "label": "percentage",
168
+ "symbol": "%",
169
+ "type": "derived SI"
170
+ },
171
+ "at": "2011-02-16T16:21:01.834174Z"
172
+ }'
173
+ datastream = Cosm::Datastream.new(json)
174
+ datastream.to_xml # =>
175
+ # <?xml version="1.0" encoding="UTF-8"?>
176
+ # <eeml xmlns="http://www.eeml.org/xsd/0.5.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="0.5.1" xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd">
177
+ # <environment creator="" updated="2011-02-16T16:21:01.834174Z" id="">
178
+ # <data id="0">
179
+ # <tag>freakin lasers</tag>
180
+ # <tag>humidity</tag>
181
+ # <tag>Temperature</tag>
182
+ # <current_value at="2011-02-16T16:21:01.834174Z">14</current_value>
183
+ # <max_value>658.0</max_value>
184
+ # <min_value>0.0</min_value>
185
+ # <unit type="derived SI" symbol="%">percentage</unit>
186
+ # <datapoints>
187
+ # <value at="2011-03-02T15:59:56.895922Z">1</value>
188
+ # <value at="2011-03-02T16:00:07.188648Z">1</value>
189
+ # <value at="2011-03-02T16:00:18.416500Z">2</value>
190
+ # </datapoints>
191
+ # </data>
192
+ # </environment>
193
+ # </eeml>
194
+
195
+
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'bundler/setup'
2
+
3
+ #require 'rake/testtask'
4
+ require 'rake/clean'
5
+ require 'rake/rdoctask'
6
+ require "rspec/core/rake_task"
7
+
8
+ CLEAN.include('pkg')
9
+ CLEAN.include('coverage')
10
+ CLOBBER.include('html')
11
+
12
+ Bundler::GemHelper.install_tasks
13
+
14
+ task :default => :spec
15
+
16
+ # run tests before building
17
+ task :build => :spec
18
+
19
+ desc "Run all specs in spec directory"
20
+ RSpec::Core::RakeTask.new do |t|
21
+ end
22
+
23
+ desc "Run all specs with rcov"
24
+ RSpec::Core::RakeTask.new(:rcov => :clean) do |t|
25
+ t.rcov = true
26
+ t.rcov_opts = '--exclude .gem/*,spec/*,.bundle/*,config/*,.rvm/*'
27
+ end
28
+
29
+ namespace :spec do
30
+ desc "Run all specs tagged with :focus => true"
31
+ RSpec::Core::RakeTask.new(:focus) do |t|
32
+ t.rspec_opts = "--tag focus"
33
+ t.verbose = true
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ #!/bin/sh
2
+
3
+ set -e
4
+ set -u
5
+ set -x
6
+ #TODO: script the installation of the pre-reqs: sqlite3
7
+ #sudo apt-get install sqlite3 libsqlite3-dev libsqlite3-ruby
8
+
9
+ outdir='build_artefacts'
10
+ rm -rf $outdir
11
+ mkdir -p $outdir
12
+
13
+ echo "started build script $0 in `$outdir` at `date`"
14
+
15
+ show_platform.sh > $outdir/platform.txt 2>&1 || true
16
+
17
+ echo "Running custom build.sh - install bundle, rake"
18
+ bundle --path=.gem/
19
+ set +e
20
+ bundle exec rake rcov > $outdir/testtask.log
21
+ spec_rc=$?
22
+ set -e
23
+
24
+ exit $spec_rc
data/cosm-rb.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cosm-rb/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cosm-rb"
7
+ s.version = Cosm::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Paul Bellamy", "Levent Ali", "Sam Mulube"]
10
+ s.email = ["paul.a.bellamy@gmail.com", "lebreeze@gmail.com", "sam@pachube.com"]
11
+ s.homepage = "http://github.com/pachube/pachube_data_formats"
12
+ s.summary = "A library for communicating with the Cosm REST API, parsing and rendering Cosm feed formats"
13
+ s.description = "A library for communicating with the Cosm REST API, parsing and rendering Cosm feed formats"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.required_rubygems_version = ">=1.6.2"
21
+
22
+ s.add_dependency("yajl-ruby", ">=0.8.1")
23
+ s.add_dependency("nokogiri", ">=1.4.4")
24
+ s.add_dependency("httparty", ">=0.8.3")
25
+
26
+ begin
27
+ if RUBY_VERSION < "1.9"
28
+ s.add_development_dependency("ruby-debug")
29
+ s.add_development_dependency("rcov", ">=0.9.9")
30
+ end
31
+ rescue
32
+ p "Could not detect ruby version"
33
+ end
34
+ s.add_development_dependency("rake", "=0.8.7")
35
+ s.add_development_dependency("rspec", "=2.6.0")
36
+
37
+ s.extra_rdoc_files = ["README.md", "CHANGELOG.md", "MIT-LICENSE"]
38
+ s.rdoc_options << '--main' << 'README'
39
+ end
40
+
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'lib/cosm-rb'
2
+
@@ -0,0 +1,6 @@
1
+ class Array
2
+ def split(pattern = nil, limit = nil)
3
+ self
4
+ end
5
+ end
6
+
@@ -0,0 +1,28 @@
1
+ module Cosm
2
+ module Base
3
+ module InstanceMethods
4
+
5
+ # Converts a model that extends Cosm::Base into it's equivalent Cosm object
6
+ # this can then be used to convert into xml, json or csv
7
+ def to_cosm
8
+ attributes = {}
9
+ self.class.cosm_class::ALLOWED_KEYS.each do |key|
10
+ attributes[key] = self.send(key) if self.respond_to?(key)
11
+ end
12
+ self.class.cosm_class.new(attributes.merge(custom_cosm_attributes))
13
+ end
14
+
15
+ protected
16
+
17
+ def custom_cosm_attributes(options = {})
18
+ hash = {}
19
+ self.class.cosm_mappings.each do |key, value|
20
+ hash[key.to_s] = self.send(value)
21
+ end
22
+ hash
23
+ end
24
+
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,52 @@
1
+ require 'cosm-rb/base/instance_methods'
2
+
3
+ module Cosm
4
+ # Provide an interface for your model objects by extending this module:
5
+ # extend Cosm::Base
6
+ #
7
+ # This provides the following configuration class method:
8
+ # is_cosm:
9
+ # - specifies that this model represents all or part of a Cosm feed
10
+ module Base
11
+
12
+ # Provides methods for converting between the different Cosm API data formats
13
+ # An example for a model representing a Cosm feed:
14
+ #
15
+ # is_cosm :feed
16
+ #
17
+ # A datastream
18
+ #
19
+ # is_cosm :datastream
20
+ #
21
+ # To specify custom field mappings or map methods onto a field
22
+ #
23
+ # is_cosm :feed, {:title => :my_custom_instance_method, :status => :determine_feed_state}
24
+ #
25
+ def is_cosm(klass, options = {})
26
+ @options = options
27
+ case klass
28
+ when :feed
29
+ @cosm_class = Cosm::Feed
30
+ when :datastream
31
+ @cosm_class = Cosm::Datastream
32
+ when :datapoint
33
+ @cosm_class = Cosm::Datapoint
34
+ else
35
+ @cosm_class = nil
36
+ end
37
+
38
+ class << self
39
+ def cosm_mappings
40
+ @options
41
+ end
42
+
43
+ def cosm_class
44
+ @cosm_class
45
+ end
46
+ end
47
+
48
+ send :include, Cosm::Base::InstanceMethods
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,7 @@
1
+ require 'httparty'
2
+ module Cosm
3
+ class Client
4
+ include HTTParty
5
+ base_uri 'https://api.cosm.com'
6
+ end
7
+ end
@@ -0,0 +1,71 @@
1
+ module Cosm
2
+ class Datapoint
3
+ ALLOWED_KEYS = %w(feed_id datastream_id at value)
4
+ ALLOWED_KEYS.each { |key| attr_accessor(key.to_sym) }
5
+
6
+ include Cosm::Templates::JSON::DatapointDefaults
7
+ include Cosm::Templates::XML::DatapointDefaults
8
+ include Cosm::Templates::CSV::DatapointDefaults
9
+ include Cosm::Parsers::JSON::DatapointDefaults
10
+ include Cosm::Parsers::XML::DatapointDefaults
11
+
12
+ # validates_presence_of :datastream_id
13
+ # validates_presence_of :value
14
+
15
+ include Validations
16
+
17
+ def valid?
18
+ pass = true
19
+ [:datastream_id, :value, :feed_id].each do |attr|
20
+ if self.send(attr).blank?
21
+ errors[attr] = ["can't be blank"]
22
+ pass = false
23
+ end
24
+ end
25
+ return pass
26
+ end
27
+
28
+ def initialize(input = {})
29
+ if input.is_a? Hash
30
+ self.attributes = input
31
+ elsif input.strip[0...1].to_s == "{"
32
+ self.attributes = from_json(input)
33
+ else
34
+ self.attributes = from_xml(input)
35
+ end
36
+ end
37
+
38
+ def attributes
39
+ h = {}
40
+ ALLOWED_KEYS.each do |key|
41
+ value = self.send(key)
42
+ h[key] = value unless value.nil?
43
+ end
44
+ return h
45
+ end
46
+
47
+ def attributes=(input)
48
+ return if input.nil?
49
+ input.deep_stringify_keys!
50
+ ALLOWED_KEYS.each { |key| self.send("#{key}=", input[key]) }
51
+ return attributes
52
+ end
53
+
54
+ def as_json(options = {})
55
+ generate_json(options[:version])
56
+ end
57
+
58
+ def to_json(options = {})
59
+ ::JSON.generate as_json(options)
60
+ end
61
+
62
+ def to_xml(options = {})
63
+ generate_xml(options[:version])
64
+ end
65
+
66
+ def to_csv(options = {})
67
+ generate_csv(options.delete(:version), options)
68
+ end
69
+ end
70
+ end
71
+