cosm-rb 0.0.10 → 0.1.00

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,7 +18,6 @@ require 'cosm-rb/exceptions'
18
18
  require 'cosm-rb/string_extensions'
19
19
  require 'cosm-rb/array_extensions'
20
20
  require 'cosm-rb/hash_extensions'
21
- require 'cosm-rb/template'
22
21
  require 'cosm-rb/templates/defaults'
23
22
  require 'cosm-rb/parsers/defaults'
24
23
  require 'cosm-rb/feed'
@@ -6,15 +6,14 @@ module Cosm
6
6
  tags = []
7
7
  quoted_mode = false
8
8
  tags << string.chars.reduce("") do |buffer, char|
9
- case char
10
- when ','
9
+ if char == ','
11
10
  if !quoted_mode
12
11
  tags << buffer
13
12
  buffer = ""
14
13
  else
15
14
  buffer << char
16
15
  end
17
- when '"'
16
+ elsif char == '"'
18
17
  if buffer.length > 0 && buffer[-1,1] == '\\'
19
18
  buffer[-1] = char
20
19
  else
@@ -44,6 +44,11 @@ module Cosm
44
44
  end
45
45
  end
46
46
 
47
+ def results
48
+ return [] if @results.nil?
49
+ @results
50
+ end
51
+
47
52
  def as_json(options = {})
48
53
  options[:version] ||= "1.0.0"
49
54
  generate_json(options[:version])
@@ -3,10 +3,10 @@ module Cosm
3
3
  module JSON
4
4
  module DatapointDefaults
5
5
  def generate_json(version = nil)
6
- template = Template.new(self, :json)
7
- template.at {at.iso8601(6)}
8
- template.value
9
- template.output!
6
+ {
7
+ :at => at.iso8601(6),
8
+ :value => value
9
+ }.delete_if_nil_value
10
10
  end
11
11
  end
12
12
  end
@@ -2,61 +2,56 @@ module Cosm
2
2
  module Templates
3
3
  module JSON
4
4
  module DatastreamDefaults
5
+
5
6
  def generate_json(version, options={})
6
- case version
7
- when "1.0.0"
8
- json_1_0_0 options
9
- when "0.6-alpha"
10
- json_0_6_alpha options
7
+ if version == "1.0.0"
8
+ output = json_100(options)
9
+ elsif version == "0.6-alpha"
10
+ output = json_06alpha(options)
11
11
  end
12
+ output[:version] = version unless options[:hide_version]
13
+ !options[:include_blank] ? output.delete_if_nil_value : output
12
14
  end
13
15
 
14
16
  private
15
17
 
16
18
  # As used by http://cosm.com/api/v2/FEED_ID/datastreams/DATASTREAM_ID.json
17
- def json_1_0_0(options={})
18
- template = Template.new(self, :json)
19
- template.id
20
- template.version {"1.0.0"}
21
- template.at {updated.iso8601(6)}
22
- template.current_value
23
- template.max_value {max_value.to_s}
24
- template.min_value {min_value.to_s}
25
- template.tags {parse_tag_string(tags)}
26
- template.unit {unit_hash(options)}
27
- template.datapoints_function
28
- template.datapoints do
29
- datapoints.collect do |datapoint|
30
- {
31
- :at => datapoint.at.iso8601(6),
32
- :value => datapoint.value
33
- }
34
- end
35
- end if datapoints.any?
36
- template.output! options
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
+ }
37
32
  end
38
33
 
39
34
  # As used by http://cosm.com/api/v1/FEED_ID/datastreams/DATASTREAM_ID.json
40
- def json_0_6_alpha(options={})
41
- template = Template.new(self, :json)
42
- template.id
43
- template.version {"0.6-alpha"}
44
- template.values {
45
- [{ :recorded_at => updated.iso8601,
35
+ def json_06alpha(options = {})
36
+ {
37
+ :id => self.id,
38
+ :values =>
39
+ [{
40
+ :recorded_at => updated.iso8601,
46
41
  :value => current_value,
47
42
  :max_value => max_value.to_s,
48
- :min_value => min_value.to_s }.delete_if_nil_value]
43
+ :min_value => min_value.to_s
44
+ }.delete_if_nil_value],
45
+ :tags => parse_tag_string(self.tags),
46
+ :unit => unit_hash(options),
49
47
  }
50
- template.tags {parse_tag_string(tags)}
51
- template.unit {unit_hash(options)}
52
- template.output! options
53
48
  end
54
49
 
55
50
  def unit_hash(options={})
56
51
  hash = { :type => unit_type,
57
52
  :symbol => unit_symbol,
58
53
  :label => unit_label }
59
- !options[:include_blank] ? hash.delete_if_nil_value : hash
54
+ !options[:include_blank] ? (hash.delete_if_nil_value if unit_type || unit_label || unit_symbol) : hash
60
55
  end
61
56
 
62
57
  end
@@ -6,96 +6,57 @@ module Cosm
6
6
  include Cosm::Helpers
7
7
 
8
8
  def generate_json(version, options = {})
9
- case version
10
- when "1.0.0"
11
- json_1_0_0 options
12
- when "0.6-alpha"
13
- json_0_6_alpha options
9
+ if version == "1.0.0"
10
+ output = json_100(options)
11
+ elsif version == "0.6-alpha"
12
+ output = json_06alpha(options)
14
13
  end
14
+ !options[:include_blank] ? output.delete_if_nil_value : output
15
15
  end
16
16
 
17
17
  private
18
18
 
19
19
  # As used by http://cosm.com/api/v2/FEED_ID.json
20
- def json_1_0_0(options = {})
21
- template = Template.new(self, :json)
22
- template.id
23
- template.title
24
- template.private {private.to_s}
25
- template.icon
26
- template.website
27
- template.tags {parse_tag_string(tags)} if tags
28
- template.description
29
- template.feed {feed.blank? ? "" : "#{feed}.json"}
30
- template.auto_feed_url
31
- template.status
32
- template.updated {updated.iso8601(6)}
33
- template.created {created.iso8601(6)}
34
- template.email
35
- template.creator
36
- if owner_login
37
- template.user do |user|
38
- {
39
- :login => owner_login
40
- }
41
- end
42
- end
43
- template.version {"1.0.0"}
44
- if datastreams
45
- template.datastreams do
46
- datastreams.collect do |ds|
47
- if ds.datapoints.any?
48
- datapoints = ds.datapoints.collect {|dp| {:value => dp.value, :at => dp.at.iso8601(6)}}
49
- end
50
- {
51
- :id => ds.id,
52
- :at => ds.updated.iso8601(6),
53
- :max_value => ds.max_value.to_s,
54
- :min_value => ds.min_value.to_s,
55
- :current_value => ds.current_value,
56
- :tags => parse_tag_string(ds.tags),
57
- :unit => unit_hash(ds, options),
58
- :datapoints => datapoints
59
- }.delete_if_nil_value
60
- end
61
- end
62
- end
63
- template.location {location_hash(options)}
64
- template.output! options
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
+ }
65
41
  end
66
42
 
43
+
67
44
  # As used by http://cosm.com/api/v1/FEED_ID.json
68
- def json_0_6_alpha(options={})
69
- template = Template.new(self, :json)
70
- template.id
71
- template.title
72
- template.icon
73
- template.website
74
- template.description
75
- template.feed {"#{feed}.json"}
76
- template.status
77
- template.updated {updated.iso8601(6)}
78
- template.email
79
- template.version {"0.6-alpha"}
80
- if datastreams
81
- template.datastreams do
82
- datastreams.collect do |ds|
83
- {
84
- :id => ds.id,
85
- :values => [{
86
- :max_value => ds.max_value.to_s,
87
- :min_value => ds.min_value.to_s,
88
- :value => ds.current_value,
89
- :recorded_at => ds.updated.iso8601
90
- }.delete_if_nil_value],
91
- :tags => parse_tag_string(ds.tags),
92
- :unit => unit_hash(ds, options)
93
- }.delete_if_nil_value
94
- end
95
- end
96
- end
97
- template.location {location_hash(options)}
98
- template.output! options
45
+ def json_06alpha(options = {})
46
+ {
47
+ :id => self.id,
48
+ :title => self.title,
49
+ :icon => icon,
50
+ :website => website,
51
+ :description => self.description,
52
+ :feed => (feed.blank? ? "" : "#{feed}.json"),
53
+ :status => status,
54
+ :updated => updated.iso8601(6),
55
+ :email => email,
56
+ :version => "0.6-alpha",
57
+ :datastreams => datastreams.map {|ds| ds.generate_json("0.6-alpha", options.merge({:hide_version => true}))},
58
+ :location => location_hash(options)
59
+ }
99
60
  end
100
61
 
101
62
  private
@@ -111,7 +72,7 @@ module Cosm
111
72
  hash[:waypoints] = format_location_waypoints if location_waypoints
112
73
  !options[:include_blank] ? (hash.delete_if_nil_value if location_disposition || location_name || location_exposure || location_domain || location_ele || location_lat || location_lon) : hash
113
74
  end
114
-
75
+
115
76
  def format_location_waypoints
116
77
  output = []
117
78
  location_waypoints.each{ |item|
@@ -120,13 +81,6 @@ module Cosm
120
81
  }
121
82
  output
122
83
  end
123
-
124
- def unit_hash(datastream, options={})
125
- hash = { :type => datastream.unit_type,
126
- :symbol => datastream.unit_symbol,
127
- :label => datastream.unit_label }
128
- !options[:include_blank] ? (hash.delete_if_nil_value if datastream.unit_type || datastream.unit_label || datastream.unit_symbol) : hash
129
- end
130
84
  end
131
85
  end
132
86
  end
@@ -3,26 +3,24 @@ module Cosm
3
3
  module JSON
4
4
  module KeyDefaults
5
5
  def generate_json(options={})
6
- template = Template.new(self, :json)
7
- template.key do |k|
8
- if self.permissions
9
- s = self.permissions.collect { |s|
10
- if s.resources
11
- 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
12
- }
13
- end
14
- {
15
- :referer => s.referer,
16
- :source_ip => s.source_ip,
17
- :label => s.label,
18
- :minimum_interval => s.minimum_interval,
19
- :access_methods => s.access_methods.collect { |a| a.to_s.downcase },
20
- :resources => res
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
21
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
22
19
  }
23
- end
24
-
25
- {
20
+ }
21
+ end
22
+ output = {
23
+ :key => {
26
24
  :id => id,
27
25
  :expires_at => expires_at.respond_to?(:iso8601) ? expires_at.iso8601(6) : expires_at,
28
26
  :api_key => key,
@@ -31,8 +29,8 @@ module Cosm
31
29
  :private_access => private_access,
32
30
  :permissions => s
33
31
  }
34
- end
35
- template.output! options
32
+ }
33
+ !options[:include_blank] ? output.delete_if_nil_value : output
36
34
  end
37
35
  end
38
36
  end
@@ -6,38 +6,33 @@ module Cosm
6
6
  include Helpers
7
7
 
8
8
  def generate_json(version)
9
- case version
10
- when "1.0.0"
11
- json_1_0_0
12
- when "0.6-alpha"
13
- json_0_6_alpha
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
14
13
  end
15
14
  end
16
15
 
17
16
  private
18
17
 
19
18
  # As used by http://cosm.com/api/v2/feeds.json
20
- def json_1_0_0
21
- template = Template.new(self, :json)
22
- template.totalResults
23
- template.startIndex
24
- template.itemsPerPage
25
- if results
26
- template.results {results.collect{|f| f.generate_json("1.0.0")}}
27
- end
28
- template.output!
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
+ }
29
26
  end
30
27
 
31
28
  # As used by http://cosm.com/api/v1/feeds.json
32
- def json_0_6_alpha
33
- template = Template.new(self, :json)
34
- template.totalResults
35
- template.startIndex
36
- template.itemsPerPage
37
- if results
38
- template.results {results.collect{|f| f.generate_json("0.6-alpha")}}
39
- end
40
- template.output!
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
+ }
41
36
  end
42
37
 
43
38
  end
@@ -3,16 +3,16 @@ module Cosm
3
3
  module JSON
4
4
  module TriggerDefaults
5
5
  def generate_json(version = nil)
6
- template = Template.new(self, :json)
7
- template.id
8
- template.threshold_value
9
- template.notified_at
10
- template.url
11
- template.trigger_type
12
- template.stream_id
13
- template.environment_id
14
- template.user
15
- template.output!
6
+ {
7
+ :id => self.id,
8
+ :threshold_value => self.threshold_value,
9
+ :notified_at => self.notified_at,
10
+ :url => self.url,
11
+ :trigger_type => self.trigger_type,
12
+ :stream_id => self.stream_id,
13
+ :environment_id => self.environment_id,
14
+ :user => self.user,
15
+ }.delete_if_nil_value
16
16
  end
17
17
  end
18
18
  end
@@ -1,3 +1,3 @@
1
1
  module Cosm #:nodoc:
2
- VERSION = '0.0.10'
2
+ VERSION = '0.1.00'
3
3
  end
@@ -102,9 +102,9 @@ describe Cosm::Datapoint do
102
102
  # Provided by Cosm::Templates::DatapointDefaults
103
103
  describe "#generate_json" do
104
104
  it "should take a version and generate the appropriate template" do
105
- datapoint = Cosm::Datapoint.new({})
106
- Cosm::Template.should_receive(:new).with(datapoint, :json)
107
- lambda {datapoint.generate_json}.should raise_error(NoMethodError)
105
+ now = Time.now
106
+ datapoint = Cosm::Datapoint.new({:at => now, :value => 123})
107
+ datapoint.generate_json.should == {:at => now.iso8601(6), :value => 123}
108
108
  end
109
109
  end
110
110
 
@@ -260,8 +260,7 @@ describe Cosm::Datastream do
260
260
  describe "#generate_json" do
261
261
  it "should take a version and generate the appropriate template" do
262
262
  datastream = Cosm::Datastream.new({})
263
- Cosm::Template.should_receive(:new).with(datastream, :json)
264
- lambda {datastream.generate_json("1.0.0")}.should raise_error(NoMethodError)
263
+ datastream.generate_json("1.0.0").should == {:version => "1.0.0"}
265
264
  end
266
265
  end
267
266
 
@@ -252,8 +252,7 @@ describe Cosm::Feed do
252
252
  describe "#generate_json" do
253
253
  it "should take a version and generate the appropriate template" do
254
254
  feed = Cosm::Feed.new({})
255
- Cosm::Template.should_receive(:new).with(feed, :json)
256
- lambda {feed.generate_json("1.0.0", {})}.should raise_error(NoMethodError)
255
+ feed.generate_json("1.0.0").should == {:version => "1.0.0"}
257
256
  end
258
257
  end
259
258
 
@@ -149,9 +149,9 @@ describe Cosm::SearchResult do
149
149
  @search_result = Cosm::SearchResult.new({})
150
150
  end
151
151
 
152
- it "should return nil if not an array" do
152
+ it "should return [] if not an array" do
153
153
  @search_result.results = "kittens"
154
- @search_result.results.should be_nil
154
+ @search_result.results.should be_empty
155
155
  end
156
156
 
157
157
  it "should accept an array of feeds and hashes and store an array of datastreams" do
@@ -187,9 +187,8 @@ describe Cosm::SearchResult do
187
187
  # Provided by Cosm::Templates::SearchResultDefaults
188
188
  describe "#generate_json" do
189
189
  it "should take a version and generate the appropriate template" do
190
- search_result = Cosm::SearchResult.new({})
191
- Cosm::Template.should_receive(:new).with(search_result, :json)
192
- lambda {search_result.generate_json("1.0.0")}.should raise_error(NoMethodError)
190
+ search_result = Cosm::SearchResult.new({"totalResults" => 100, "itemsPerPage" => 12, :results => []})
191
+ search_result.generate_json("1.0.0").should == {:totalResults => 100, :itemsPerPage => 12}
193
192
  end
194
193
  end
195
194
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cosm-rb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 10
10
- version: 0.0.10
10
+ version: 0.1.00
11
11
  platform: ruby
12
12
  authors:
13
13
  - Paul Bellamy
@@ -17,11 +17,13 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2012-08-07 00:00:00 +00:00
20
+ date: 2012-09-01 00:00:00 +00:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
24
- version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ name: yajl-ruby
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
25
27
  none: false
26
28
  requirements:
27
29
  - - ">="
@@ -32,12 +34,12 @@ dependencies:
32
34
  - 8
33
35
  - 1
34
36
  version: 0.8.1
35
- requirement: *id001
36
37
  type: :runtime
37
- name: yajl-ruby
38
- prerelease: false
38
+ version_requirements: *id001
39
39
  - !ruby/object:Gem::Dependency
40
- version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ name: nokogiri
41
+ prerelease: false
42
+ requirement: &id002 !ruby/object:Gem::Requirement
41
43
  none: false
42
44
  requirements:
43
45
  - - ">="
@@ -48,12 +50,12 @@ dependencies:
48
50
  - 4
49
51
  - 4
50
52
  version: 1.4.4
51
- requirement: *id002
52
53
  type: :runtime
53
- name: nokogiri
54
- prerelease: false
54
+ version_requirements: *id002
55
55
  - !ruby/object:Gem::Dependency
56
- version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ name: httparty
57
+ prerelease: false
58
+ requirement: &id003 !ruby/object:Gem::Requirement
57
59
  none: false
58
60
  requirements:
59
61
  - - ">="
@@ -64,12 +66,12 @@ dependencies:
64
66
  - 8
65
67
  - 3
66
68
  version: 0.8.3
67
- requirement: *id003
68
69
  type: :runtime
69
- name: httparty
70
- prerelease: false
70
+ version_requirements: *id003
71
71
  - !ruby/object:Gem::Dependency
72
- version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ name: fastercsv
73
+ prerelease: false
74
+ requirement: &id004 !ruby/object:Gem::Requirement
73
75
  none: false
74
76
  requirements:
75
77
  - - ">="
@@ -80,12 +82,12 @@ dependencies:
80
82
  - 5
81
83
  - x
82
84
  version: 1.5.x
83
- requirement: *id004
84
85
  type: :runtime
85
- name: fastercsv
86
- prerelease: false
86
+ version_requirements: *id004
87
87
  - !ruby/object:Gem::Dependency
88
- version_requirements: &id005 !ruby/object:Gem::Requirement
88
+ name: ruby-debug
89
+ prerelease: false
90
+ requirement: &id005 !ruby/object:Gem::Requirement
89
91
  none: false
90
92
  requirements:
91
93
  - - ">="
@@ -94,12 +96,12 @@ dependencies:
94
96
  segments:
95
97
  - 0
96
98
  version: "0"
97
- requirement: *id005
98
99
  type: :development
99
- name: ruby-debug
100
- prerelease: false
100
+ version_requirements: *id005
101
101
  - !ruby/object:Gem::Dependency
102
- version_requirements: &id006 !ruby/object:Gem::Requirement
102
+ name: rcov
103
+ prerelease: false
104
+ requirement: &id006 !ruby/object:Gem::Requirement
103
105
  none: false
104
106
  requirements:
105
107
  - - ">="
@@ -110,12 +112,12 @@ dependencies:
110
112
  - 9
111
113
  - 9
112
114
  version: 0.9.9
113
- requirement: *id006
114
115
  type: :development
115
- name: rcov
116
- prerelease: false
116
+ version_requirements: *id006
117
117
  - !ruby/object:Gem::Dependency
118
- version_requirements: &id007 !ruby/object:Gem::Requirement
118
+ name: rake
119
+ prerelease: false
120
+ requirement: &id007 !ruby/object:Gem::Requirement
119
121
  none: false
120
122
  requirements:
121
123
  - - "="
@@ -126,12 +128,12 @@ dependencies:
126
128
  - 8
127
129
  - 7
128
130
  version: 0.8.7
129
- requirement: *id007
130
131
  type: :development
131
- name: rake
132
- prerelease: false
132
+ version_requirements: *id007
133
133
  - !ruby/object:Gem::Dependency
134
- version_requirements: &id008 !ruby/object:Gem::Requirement
134
+ name: rspec
135
+ prerelease: false
136
+ requirement: &id008 !ruby/object:Gem::Requirement
135
137
  none: false
136
138
  requirements:
137
139
  - - "="
@@ -142,10 +144,8 @@ dependencies:
142
144
  - 6
143
145
  - 0
144
146
  version: 2.6.0
145
- requirement: *id008
146
147
  type: :development
147
- name: rspec
148
- prerelease: false
148
+ version_requirements: *id008
149
149
  description: A library for communicating with the Cosm REST API, parsing and rendering Cosm feed formats
150
150
  email:
151
151
  - paul.a.bellamy@gmail.com
@@ -205,7 +205,6 @@ files:
205
205
  - lib/cosm-rb/resource.rb
206
206
  - lib/cosm-rb/search_result.rb
207
207
  - lib/cosm-rb/string_extensions.rb
208
- - lib/cosm-rb/template.rb
209
208
  - lib/cosm-rb/templates/csv/datapoint_defaults.rb
210
209
  - lib/cosm-rb/templates/csv/datastream_defaults.rb
211
210
  - lib/cosm-rb/templates/csv/feed_defaults.rb
@@ -249,7 +248,6 @@ files:
249
248
  - spec/cosm-rb/parsers/xml/trigger_defaults_spec.rb
250
249
  - spec/cosm-rb/search_result_spec.rb
251
250
  - spec/cosm-rb/string_extensions_spec.rb
252
- - spec/cosm-rb/template_spec.rb
253
251
  - spec/cosm-rb/templates/csv/datapoint_defaults_spec.rb
254
252
  - spec/cosm-rb/templates/csv/datastream_defaults_spec.rb
255
253
  - spec/cosm-rb/templates/csv/feed_defaults_spec.rb
@@ -1,32 +0,0 @@
1
- module Cosm
2
- class Template
3
- attr_accessor :subject
4
- attr_accessor :presentation
5
- attr_accessor :output
6
-
7
- def initialize(subject, presentation, &block)
8
- @subject = subject
9
- @presentation = presentation
10
- @output = {}
11
- end
12
-
13
- def method_missing(sym, *args, &block)
14
- if block_given?
15
- @output[sym] = @subject.instance_eval &block
16
- else
17
- @output[sym] = @subject.send(sym)
18
- end
19
- rescue NameError, NoMethodError => e
20
- @output[sym] = nil
21
- end
22
-
23
- def id
24
- method_missing(:id)
25
- end
26
-
27
- def output!(options = {})
28
- !options[:include_blank] ? @output.reject {|k,v| v.nil? || v.blank?} : @output
29
- end
30
- end
31
- end
32
-
@@ -1,74 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Cosm::Template do
4
- describe "initialisation" do
5
- it "should accept an object and format" do
6
- template = Cosm::Template.new(@feed, :json)
7
- template.subject.should == @feed
8
- template.presentation.should == :json
9
- end
10
- end
11
-
12
- describe "presenting json" do
13
- before(:each) do
14
- @feed = Cosm::Feed.new(feed_as_(:hash))
15
- @template = Cosm::Template.new(@feed, :json)
16
- end
17
-
18
- it "should allow describing :title => @feed.title" do
19
- @template.title
20
- @template.output!.should == {:title => @feed.title}
21
- end
22
-
23
- it "should allow describing :name => @feed.title" do
24
- @template.name {title}
25
- @template.output!.should == {:name => @feed.title}
26
- end
27
-
28
- it "should allow describing :tags => @feed.tags.map(&:strip).sort" do
29
- @template.tags { [*tags].map(&:strip).sort }
30
- @template.output!.should == {:tags => [*@feed.tags].map(&:strip).sort}
31
- end
32
-
33
- it "should allow describing datastreams" do
34
- @template.title
35
- @template.datastreams do |f|
36
- f.datastreams.collect do |ds|
37
- {
38
- :id => ds.id,
39
- :current_value => ds.current_value
40
- }
41
- end
42
- end
43
- datastreams = @feed.datastreams.collect {|ds| {:id => ds.id, :current_value => ds.current_value}}
44
- @template.output!.should == {
45
- :title => @feed.title,
46
- :datastreams => datastreams
47
- }
48
- end
49
-
50
- it "should ignore nils" do
51
- @feed.title = nil
52
- @template.title
53
- @template.output!.should == {}
54
- end
55
-
56
- it "should return nils for NameErrors" do
57
- @template.title {litter}
58
- @template.output!.should == {}
59
- end
60
-
61
- it "should return nils for NoMethodErrors" do
62
- @feed.should_not respond_to(:rubbish)
63
- @template.rubbish.should == nil
64
- end
65
-
66
- it "should include blanks if we pass :include_blank" do
67
- @feed.title = ''
68
- @template.title
69
- @template.output!(:include_blank => true).should == {:title => ''}
70
- end
71
-
72
- end
73
- end
74
-