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
@@ -0,0 +1,65 @@
1
+ module Cosm
2
+ class Trigger
3
+ ALLOWED_KEYS = %w(threshold_value user notified_at url trigger_type id environment_id stream_id)
4
+ ALLOWED_KEYS.each { |key| attr_accessor(key.to_sym) }
5
+
6
+ include Cosm::Templates::JSON::TriggerDefaults
7
+ include Cosm::Parsers::JSON::TriggerDefaults
8
+ include Cosm::Parsers::XML::TriggerDefaults
9
+
10
+ # validates_presence_of :url
11
+ # validates_presence_of :stream_id
12
+ # validates_presence_of :environment_id
13
+ # validates_presence_of :user
14
+
15
+ include Validations
16
+
17
+ def valid?
18
+ pass = true
19
+ [:url, :stream_id, :environment_id, :user].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
+ ALLOWED_KEYS.each { |key| self.send("#{key}=", input[key]) }
50
+ return attributes
51
+ end
52
+
53
+ def as_json(options = {})
54
+ generate_json
55
+ end
56
+
57
+ def to_json(options = {})
58
+ ::JSON.generate as_json(options)
59
+ end
60
+
61
+
62
+ end
63
+ end
64
+
65
+
@@ -0,0 +1,9 @@
1
+ module Cosm
2
+ module Validations
3
+ attr_accessor :errors
4
+ def errors
5
+ @errors ||= {}
6
+ end
7
+ end
8
+ end
9
+
@@ -0,0 +1,3 @@
1
+ module Cosm #:nodoc:
2
+ VERSION = '0.0.1'
3
+ end
data/lib/cosm-rb.rb ADDED
@@ -0,0 +1,29 @@
1
+ require 'nokogiri'
2
+ require 'yajl/json_gem'
3
+ require 'csv'
4
+
5
+ $:.unshift(File.dirname(File.expand_path(__FILE__)))
6
+
7
+ $KCODE = 'u' if RUBY_VERSION.to_f < 1.9
8
+
9
+ require 'cosm-rb/helpers'
10
+ require 'cosm-rb/base'
11
+ require 'cosm-rb/validations'
12
+ require 'cosm-rb/object_extensions'
13
+ require 'cosm-rb/nil_content'
14
+ require 'cosm-rb/string_extensions'
15
+ require 'cosm-rb/array_extensions'
16
+ require 'cosm-rb/hash_extensions'
17
+ require 'cosm-rb/template'
18
+ require 'cosm-rb/templates/defaults'
19
+ require 'cosm-rb/parsers/defaults'
20
+ require 'cosm-rb/feed'
21
+ require 'cosm-rb/datastream'
22
+ require 'cosm-rb/datapoint'
23
+ require 'cosm-rb/search_result'
24
+ require 'cosm-rb/trigger'
25
+ require 'cosm-rb/key'
26
+ require 'cosm-rb/permission'
27
+ require 'cosm-rb/resource'
28
+
29
+ require 'cosm-rb/client'
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Array extensions" do
4
+ it "should ignore array.split calls" do
5
+ array = ['a', 'b']
6
+ array.split.should == array
7
+ end
8
+ end
9
+
@@ -0,0 +1,109 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cosm::Base::InstanceMethods do
4
+ before(:each) do
5
+ @feed = Feed.new
6
+ @feed.updated = Time.parse("20110202")
7
+ @feed.title = "Feed Title"
8
+ @feed.private = true
9
+ @feed.icon = "http://cosm.com/logo.png"
10
+ @feed.website = "http://cosm.com"
11
+ @feed.tags = "kittens, sofa, aardvark"
12
+ @feed.description = "Test feed"
13
+ @feed.feed = "http://test.host/testfeed.html?random=890299&rand2=91"
14
+ @feed.email = "abc@example.com"
15
+ @feed.creator = "http://www.cosm.com"
16
+ @feed.location_name = "London"
17
+ @feed.location_disposition = "fixed"
18
+ @feed.location_domain = "here"
19
+ @feed.location_ele = "2000"
20
+ @feed.location_exposure = "naked"
21
+ @feed.location_lat = 51.5423
22
+ @feed.location_lon = 3.243
23
+ @datastream1 = Datastream.new
24
+ datastream_as_(:hash, :with => {"stream_id" => "0"}, :except => ["datapoints"]).each do |k,v|
25
+ @datastream1.send("#{k}=", v)
26
+ end
27
+ # @feed.datastreams.create!(datastream_as_(:hash, :with => {"stream_id" => "0"}, :except => ["datapoints"]))
28
+ @datastream2 = Datastream.new
29
+ datastream_as_(:hash, :with => {"stream_id" => "two"}, :except => ["datapoints"]).each do |k,v|
30
+ @datastream2.send("#{k}=", v)
31
+ end
32
+ @feed.datastreams = [@datastream1, @datastream2]
33
+ datapoints = []
34
+ 10.times do
35
+ datapoint = Datapoint.new
36
+ datapoint.value = "#{rand(10000)}"
37
+ datapoint.at = Time.now - rand(10000) * 60
38
+ datapoints << datapoint
39
+ end
40
+ @datastream2.datapoints = datapoints
41
+ end
42
+
43
+ describe "#to_cosm" do
44
+ it "should return the appropriate Cosm Object" do
45
+ @feed.to_cosm.should be_kind_of(Cosm::Feed)
46
+ @datastream1.to_cosm.should be_kind_of(Cosm::Datastream)
47
+ @datastream2.datapoints.first.to_cosm.should be_kind_of(Cosm::Datapoint)
48
+ end
49
+
50
+ context "Feed" do
51
+ it "should map the default fields by default" do
52
+ Cosm::Feed.should_receive(:new).with(@feed.attributes)
53
+ @feed.to_cosm
54
+ end
55
+ end
56
+
57
+ context "Datastream" do
58
+ it "should map the default fields merged with the optionals by default" do
59
+ Cosm::Datastream.should_receive(:new).with(@datastream2.attributes.merge({"id" => "two"}))
60
+ @datastream2.to_cosm
61
+ end
62
+ end
63
+
64
+ context "Datapoint" do
65
+ it "should map the default fields by default" do
66
+ Cosm::Datapoint.should_receive(:new).with(@datastream2.datapoints.first.attributes)
67
+ @datastream2.datapoints.first.to_cosm
68
+ end
69
+ end
70
+ end
71
+
72
+ context "custom mappings" do
73
+ it "should allow custom method mappings" do
74
+ class CustomFeed
75
+ extend Cosm::Base
76
+
77
+ undef_method :id if method_defined?(:id)
78
+ attr_accessor :title
79
+ is_cosm :feed, {:feed => :custom_method}
80
+
81
+ def custom_method
82
+ "I haz customer"
83
+ end
84
+ end
85
+ feed = CustomFeed.new
86
+ feed.title = "Name"
87
+ Cosm::Feed.should_receive(:new).with({"feed" => feed.custom_method, "title" => feed.title})
88
+ feed.to_cosm
89
+ end
90
+
91
+ it "should not rely on datastreams using this gem" do
92
+ class NewCustomFeed
93
+ extend Cosm::Base
94
+
95
+ attr_accessor :title
96
+ is_cosm :feed
97
+
98
+ def datastreams
99
+ ["fake datastream"]
100
+ end
101
+ end
102
+
103
+ feed = NewCustomFeed.new
104
+ feed.title = "Name"
105
+ feed.to_cosm
106
+ end
107
+ end
108
+ end
109
+
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cosm::Base do
4
+ describe "#is_cosm" do
5
+ it "should take a class name and a hash of options" do
6
+ lambda { Feed.is_cosm }.should raise_error(ArgumentError, "wrong number of arguments (0 for 1)")
7
+ lambda { Feed.is_cosm(:feed, {}, "bogus third arg") }.should raise_error(ArgumentError, "wrong number of arguments (3 for 2)")
8
+ end
9
+
10
+ it "should assign mapping class" do
11
+ class TestClass
12
+ extend Cosm::Base
13
+ is_cosm :datastream
14
+ end
15
+ TestClass.cosm_class.should == Cosm::Datastream
16
+ end
17
+
18
+ it "should assign mapping class for datapoints" do
19
+ class TestClass
20
+ extend Cosm::Base
21
+ is_cosm :datapoint
22
+ end
23
+ TestClass.cosm_class.should == Cosm::Datapoint
24
+ end
25
+
26
+ it "should not assign mapping class if unrecognized" do
27
+ class TestClass
28
+ extend Cosm::Base
29
+ is_cosm :hero_stream
30
+ end
31
+ TestClass.cosm_class.should be_nil
32
+ end
33
+
34
+ it "should assign optional mappings" do
35
+ class TestClass
36
+ extend Cosm::Base
37
+ is_cosm :feed, {:title => :custom_method, :website => :custom_method}
38
+ def custom_method
39
+ "Custom title"
40
+ end
41
+ end
42
+ TestClass.cosm_mappings.should == {:title => :custom_method, :website => :custom_method}
43
+ end
44
+
45
+ it "should include our instance methods into a class when we call it" do
46
+ class TestClass
47
+ extend Cosm::Base
48
+ end
49
+ TestClass.should_receive(:include).with(Cosm::Base::InstanceMethods)
50
+ class TestClass
51
+ is_cosm :feed
52
+ end
53
+ end
54
+ end
55
+ end
56
+
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Cosm::Client" do
4
+ it "should have the base uri defined" do
5
+ Cosm::Client.base_uri.should == 'https://api.cosm.com'
6
+ end
7
+ end
@@ -0,0 +1,169 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cosm::Datapoint do
4
+
5
+ it "should have a constant that defines the allowed keys" do
6
+ Cosm::Datapoint::ALLOWED_KEYS.should == %w(feed_id datastream_id at value)
7
+ end
8
+
9
+ describe "validation" do
10
+ before(:each) do
11
+ @datapoint = Cosm::Datapoint.new
12
+ end
13
+
14
+ %w(datastream_id value feed_id).each do |field|
15
+ it "should require a '#{field}'" do
16
+ @datapoint.send("#{field}=".to_sym, nil)
17
+ @datapoint.should_not be_valid
18
+ @datapoint.errors[field.to_sym].should include("can't be blank")
19
+ end
20
+ end
21
+ end
22
+
23
+ describe "#initialize" do
24
+ it "should create a blank slate when passed no arguments" do
25
+ datapoint = Cosm::Datapoint.new
26
+ Cosm::Datapoint::ALLOWED_KEYS.each do |attr|
27
+ datapoint.attributes[attr.to_sym].should be_nil
28
+ end
29
+ end
30
+
31
+ it "should accept xml" do
32
+ datapoint = Cosm::Datapoint.new(datapoint_as_(:xml))
33
+ datapoint.value.should == "2000"
34
+ end
35
+
36
+ it "should accept json" do
37
+ datapoint = Cosm::Datapoint.new(datapoint_as_(:json))
38
+ datapoint.value.should == "2000"
39
+ end
40
+
41
+ it "should accept a hash of attributes" do
42
+ datapoint = Cosm::Datapoint.new(datapoint_as_(:hash))
43
+ datapoint.value.should == "2000"
44
+ end
45
+ end
46
+
47
+ describe "#attributes" do
48
+ it "should return a hash of datapoint properties" do
49
+ attrs = {}
50
+ Cosm::Datapoint::ALLOWED_KEYS.each do |key|
51
+ attrs[key] = "key #{rand(1000)}"
52
+ end
53
+ datapoint = Cosm::Datapoint.new(attrs)
54
+
55
+ datapoint.attributes.should == attrs
56
+ end
57
+
58
+ it "should not return nil values" do
59
+ attrs = {}
60
+ Cosm::Datapoint::ALLOWED_KEYS.each do |key|
61
+ attrs[key] = "key #{rand(1000)}"
62
+ end
63
+ attrs["created_at"] = nil
64
+ datapoint = Cosm::Datapoint.new(attrs)
65
+
66
+ datapoint.attributes.should_not include("created_at")
67
+ end
68
+ end
69
+
70
+ describe "#attributes=" do
71
+ it "should accept and save a hash of datapoint properties" do
72
+ datapoint = Cosm::Datapoint.new({})
73
+
74
+ attrs = {}
75
+ Cosm::Datapoint::ALLOWED_KEYS.each do |key|
76
+ value = "key #{rand(1000)}"
77
+ attrs[key] = value
78
+ datapoint.should_receive("#{key}=").with(value)
79
+ end
80
+ datapoint.attributes=(attrs)
81
+ end
82
+ end
83
+
84
+ # Provided by Cosm::Templates::DatapointDefaults
85
+ describe "#generate_json" do
86
+ it "should take a version and generate the appropriate template" do
87
+ datapoint = Cosm::Datapoint.new({})
88
+ Cosm::Template.should_receive(:new).with(datapoint, :json)
89
+ lambda {datapoint.generate_json}.should raise_error(NoMethodError)
90
+ end
91
+ end
92
+
93
+ describe "#to_csv" do
94
+ it "should call the csv generator with default version (nil as there only is one version)" do
95
+ datapoint = Cosm::Datapoint.new({})
96
+ datapoint.should_receive(:generate_csv).with(nil, {}).and_return("3")
97
+ datapoint.to_csv.should == "3"
98
+ end
99
+
100
+ it "should accept optional csv version" do
101
+ datapoint = Cosm::Datapoint.new({})
102
+ datapoint.should_receive(:generate_csv).with("1", {}).and_return("34")
103
+ datapoint.to_csv(:version => "1").should == "34"
104
+ end
105
+
106
+ it "should accept additional options" do
107
+ datapoint = Cosm::Datapoint.new({})
108
+ datapoint.should_receive(:generate_csv).with("1", {:full => true}).and_return("34")
109
+ datapoint.to_csv(:version => "1", :full => true).should == "34"
110
+ end
111
+ end
112
+
113
+ describe "#to_xml" do
114
+
115
+ it "should call the xml generator with default version (nil as there only is one version)" do
116
+ datapoint = Cosm::Datapoint.new({})
117
+ datapoint.should_receive(:generate_xml).with(nil).and_return("<xml></xml>")
118
+ datapoint.to_xml.should == "<xml></xml>"
119
+ end
120
+
121
+ it "should accept optional xml version" do
122
+ datapoint = Cosm::Datapoint.new({})
123
+ datapoint.should_receive(:generate_xml).with("5").and_return("<xml></xml>")
124
+ datapoint.to_xml(:version => "5").should == "<xml></xml>"
125
+ end
126
+
127
+ end
128
+
129
+ describe "#as_json" do
130
+
131
+ it "should call the json generator with default version (nil as there only is one version)" do
132
+ datapoint = Cosm::Datapoint.new({})
133
+ datapoint.should_receive(:generate_json).with(nil).and_return({"title" => "Environment"})
134
+ datapoint.as_json.should == {"title" => "Environment"}
135
+ end
136
+
137
+ it "should accept optional json version" do
138
+ datapoint = Cosm::Datapoint.new({})
139
+ datapoint.should_receive(:generate_json).with("0.6-alpha").and_return({"title" => "Environment"})
140
+ datapoint.as_json(:version => "0.6-alpha").should == {"title" => "Environment"}
141
+ end
142
+
143
+ end
144
+
145
+ describe "#to_json" do
146
+ it "should call #as_json" do
147
+ datapoint_hash = {"id" => "env001", "value" => "2344"}
148
+ datapoint = Cosm::Datapoint.new(datapoint_hash)
149
+ datapoint.should_receive(:as_json).with({})
150
+ datapoint.to_json
151
+ end
152
+
153
+ it "should pass options through to #as_json" do
154
+ datapoint_hash = {"id" => "env001", "value" => "2344"}
155
+ datapoint = Cosm::Datapoint.new(datapoint_hash)
156
+ datapoint.should_receive(:as_json).with({:crazy => "options"})
157
+ datapoint.to_json({:crazy => "options"})
158
+ end
159
+
160
+ it "should pass the output of #as_json to yajl" do
161
+ datapoint_hash = {"id" => "env001", "value" => "2344"}
162
+ datapoint = Cosm::Datapoint.new(datapoint_hash)
163
+ datapoint.should_receive(:as_json).and_return({:awesome => "hash"})
164
+ ::JSON.should_receive(:generate).with({:awesome => "hash"})
165
+ datapoint.to_json
166
+ end
167
+ end
168
+ end
169
+