brighter_planet_metadata 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  s.add_dependency 'activesupport', '>=2.3.4'
23
23
  s.add_dependency 'i18n' # activesupport?
24
24
  s.add_dependency 'cache_method'
25
+ s.add_development_dependency 'brighter_planet_deploy'
25
26
  s.add_development_dependency 'fakeweb'
26
27
  s.add_development_dependency 'test-unit'
27
28
  s.add_development_dependency 'fakefs'
@@ -1,13 +1,14 @@
1
+ require 'brighter_planet_deploy'
1
2
  require 'singleton'
2
3
  module BrighterPlanet
3
4
  class Metadata
4
5
  class Cm1Authority
5
6
  include ::Singleton
6
- def authority?(universe, method_id)
7
- return unless universe == 'cm1_production'
7
+ def authority?(method_id)
8
+ return unless ::Rails.env.production? and ::BrighterPlanet.deploy.servers.me.service == 'EmissionEstimateService'
8
9
  method_id = method_id.to_s
9
10
  if method_id == 'certified_emitters'
10
- defined?(::Rails) and ::Rails.application.certified?
11
+ ::Rails.application.certified?
11
12
  else
12
13
  respond_to? method_id
13
14
  end
@@ -28,9 +29,6 @@ module BrighterPlanet
28
29
  memo
29
30
  end
30
31
  end
31
- def color
32
- (AuthoritativeDnsResolver.getaddress('carbon.brighterplanet.com') == '184.73.240.13') ? 'red' : 'blue'
33
- end
34
32
  end
35
33
  end
36
34
  end
@@ -1,10 +1,11 @@
1
+ require 'brighter_planet_deploy'
1
2
  require 'singleton'
2
3
  module BrighterPlanet
3
4
  class Metadata
4
5
  class Data1Authority
5
6
  include ::Singleton
6
- def authority?(universe, method_id)
7
- return unless universe == 'data1_production'
7
+ def authority?(method_id)
8
+ return unless ::Rails.env.production? and ::BrighterPlanet.deploy.servers.me.service == 'ReferenceDataService'
8
9
  respond_to? method_id
9
10
  end
10
11
  def resources
@@ -11,8 +11,6 @@ end if ::ActiveSupport::VERSION::MAJOR == 3
11
11
 
12
12
  module BrighterPlanet
13
13
  class Metadata
14
- autoload :AuthoritativeDnsResolver, 'brighter_planet_metadata/authoritative_dns_resolver'
15
-
16
14
  include ::Singleton
17
15
  LIVE_URL = {
18
16
  'datasets' => 'http://data.brighterplanet.com/datasets.json',
@@ -20,7 +18,6 @@ module BrighterPlanet
20
18
  'certified_emitters' => 'http://certified.carbon.brighterplanet.com/emitters.json',
21
19
  'resources' => 'http://data.brighterplanet.com/resources.json',
22
20
  'protocols' => 'http://carbon.brighterplanet.com/protocols.json',
23
- 'color' => 'http://carbon.brighterplanet.com/color.json'
24
21
  }.freeze
25
22
 
26
23
  # sabshere 2/4/11 obv these have to be updated with some regularity
@@ -28,9 +25,8 @@ module BrighterPlanet
28
25
  'datasets' => %w{ AutomobileIndustry FlightIndustry },
29
26
  'emitters' => %w{ Automobile AutomobileTrip BusTrip Computation Diet ElectricityUse Flight FuelPurchase Lodging Meeting Motorcycle Pet Purchase RailTrip Residence Shipment },
30
27
  'certified_emitters' => %w{ },
31
- 'resources' => %w{ AirConditionerUse Aircraft AircraftClass AircraftManufacturer Airline Airport AutomobileFuelType AutomobileMake AutomobileMakeFleetYear AutomobileMakeModel AutomobileMakeModelYear AutomobileMakeModelYearVariant AutomobileMakeYear AutomobileSizeClass AutomobileSizeClassYear AutomobileTypeFuelAge AutomobileTypeFuelControl AutomobileTypeFuelYear AutomobileTypeFuelYearControl AutomobileTypeYear Breed BreedGender BusClass Carrier CarrierMode CensusDivision CensusRegion ClimateDivision ClothesMachineUse ComputationPlatform Country DataCenterCompany DietClass DishwasherUse EgridRegion EgridSubregion FlightDistanceClass FlightFuelType FlightSeatClass FlightSegment FoodGroup FuelPrice FuelType FuelYear Gender GreenhouseGas Industry IndustryProduct IndustryProductLine IndustrySector LodgingClass Merchant MerchantCategory MerchantCategoryIndustry PetroleumAdministrationForDefenseDistrict ProductLine ProductLineIndustryProduct RailClass ResidenceAppliance ResidenceClass ResidenceFuelPrice ResidenceFuelType ResidentialEnergyConsumptionSurveyResponse Sector ServerType ServerTypeAlias ShipmentMode Species State Urbanity ZipCode },
28
+ 'resources' => %w{ AirConditionerUse Aircraft AircraftClass AircraftManufacturer Airline Airport AutomobileFuel AutomobileMake AutomobileMakeFleetYear AutomobileMakeModel AutomobileMakeModelYear AutomobileMakeModelYearVariant AutomobileMakeYear AutomobileSizeClass AutomobileSizeClassYear AutomobileTypeFuelAge AutomobileTypeFuelControl AutomobileTypeFuelYear AutomobileTypeFuelYearAge AutomobileTypeFuelYearControl AutomobileTypeYear Breed BreedGender BusClass BusFuel BusFuelControl BusFuelYearControl Carrier CarrierMode CensusDivision CensusRegion ClimateDivision ClothesMachineUse ComputationCarrier ComputationCarrierInstanceClass ComputationCarrierRegion Country DietClass DishwasherUse EgridRegion EgridSubregion FlightDistanceClass FlightSeatClass FlightSegment FoodGroup Fuel FuelPrice FuelType FuelYear Gender GreenhouseGas LodgingClass PetroleumAdministrationForDefenseDistrict RailClass ResidenceAppliance ResidenceClass ResidenceFuelPrice ResidenceFuelType ResidentialEnergyConsumptionSurveyResponse ShipmentMode Species State Urbanity ZipCode },
32
29
  'protocols' => { 'ghg_protocol_scope_3' => 'Greenhouse Gas Protocol Scope 3', 'iso' => 'ISO 14064-1', 'tcr' => 'The Climate Registry', 'ghg_protocol_scope_1' => 'Greenhouse Gas Protocol Scope 1' },
33
- 'color' => 'unknown'
34
30
  }.freeze
35
31
 
36
32
  # What resources are available.
@@ -58,11 +54,6 @@ module BrighterPlanet
58
54
  deep_copy_of_authoritative_value_or_fallback 'protocols'
59
55
  end
60
56
 
61
- # What 'color' the emission estimate service is today.
62
- def color
63
- deep_copy_of_authoritative_value_or_fallback 'color'
64
- end
65
-
66
57
  # Clear out any cached values
67
58
  def refresh
68
59
  # instance_variables.each { |ivar_name| instance_variable_set ivar_name, nil }
@@ -85,17 +76,6 @@ module BrighterPlanet
85
76
  [ cm1_authority, data1_authority ]
86
77
  end
87
78
 
88
- # A universe of operation, for example an EngineYard AppCloud "environment"
89
- def universe
90
- if ::ENV['BRIGHTER_PLANET_METADATA_FORCE_UNIVERSE'].present?
91
- ::ENV['BRIGHTER_PLANET_METADATA_FORCE_UNIVERSE']
92
- elsif ::File.readable? '/etc/brighterplanet/universe'
93
- ::File.read('/etc/brighterplanet/universe').chomp
94
- else
95
- 'unknown'
96
- end
97
- end
98
-
99
79
  def deep_copy_of_authoritative_value_or_fallback(k)
100
80
  v = authoritative_value_or_fallback k
101
81
  case v
@@ -113,7 +93,7 @@ module BrighterPlanet
113
93
  # Used internally to pull a live list of emitters/datasets/etc. or fall back to a static one.
114
94
  def authoritative_value_or_fallback(k)
115
95
  k = k.to_s
116
- if (authority = authorities.detect { |a| a.authority? universe, k })
96
+ if (authority = authorities.detect { |a| a.authority? k })
117
97
  authority.send k
118
98
  else
119
99
  begin
@@ -1,3 +1,3 @@
1
1
  module BrighterPlanetMetadata
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/test/helper.rb CHANGED
@@ -5,6 +5,7 @@ require 'test/unit'
5
5
  require 'fakeweb'
6
6
  require 'fakefs/safe'
7
7
  require 'fileutils'
8
+ require 'active_support/string_inquirer'
8
9
  $LOAD_PATH.unshift(File.dirname(__FILE__))
9
10
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
11
  require 'brighter_planet_metadata'
@@ -14,7 +15,8 @@ class Test::Unit::TestCase
14
15
  FakeWeb.clean_registry
15
16
  FakeWeb.allow_net_connect = false
16
17
  Rails.application.certified = false
17
- Rails.root = nil
18
+ Rails.root = '/var/www/myapp'
19
+ Rails.env = ActiveSupport::StringInquirer.new 'development'
18
20
  end
19
21
  def teardown
20
22
  FakeFS::FileSystem.clear
@@ -28,7 +30,7 @@ end
28
30
  require 'singleton'
29
31
  require 'active_support/core_ext/module'
30
32
  module Rails
31
- mattr_accessor :root
33
+ mattr_accessor :root, :env
32
34
  def self.application
33
35
  FakeApplication.instance
34
36
  end
@@ -12,23 +12,27 @@ class TestLiveCm1 < Test::Unit::TestCase
12
12
  def setup
13
13
  super
14
14
  FakeFS.activate!
15
- FileUtils.mkdir_p '/etc/brighterplanet'
16
- File.open('/etc/brighterplanet/universe', 'w') { |f| f.write 'cm1_production' }
15
+ Rails.env = ActiveSupport::StringInquirer.new 'production'
16
+ Rails.root = '/data/edge/current'
17
+ FileUtils.mkdir_p '/data/edge/current/config/brighter_planet_deploy'
18
+ FileUtils.mkdir_p '/data/edge/current/public/brighter_planet_deploy'
19
+ File.open('/data/edge/current/config/brighter_planet_deploy/service', 'w') { |f| f.write 'EmissionEstimateService' }
17
20
  end
18
21
 
19
- def test_universe
20
- assert_equal 'cm1_production', ::BrighterPlanet.metadata.send(:universe)
22
+ def test_self_awareness
23
+ assert ::Rails.env.production?
24
+ assert_equal 'EmissionEstimateService', ::BrighterPlanet.deploy.servers.me.service
21
25
  end
22
26
 
23
27
  def test_authority
24
- assert ::BrighterPlanet::Metadata::Cm1Authority.instance.authority?('cm1_production', 'emitters')
28
+ assert ::BrighterPlanet::Metadata::Cm1Authority.instance.authority?('emitters')
25
29
 
26
30
  # you don't have authority to say what's certified...
27
- assert !::BrighterPlanet::Metadata::Cm1Authority.instance.authority?('cm1_production', 'certified_emitters')
31
+ assert !::BrighterPlanet::Metadata::Cm1Authority.instance.authority?('certified_emitters')
28
32
 
29
33
  # now you do
30
34
  Rails.application.certified = true
31
- assert ::BrighterPlanet::Metadata::Cm1Authority.instance.authority?('cm1_production', 'certified_emitters')
35
+ assert ::BrighterPlanet::Metadata::Cm1Authority.instance.authority?('certified_emitters')
32
36
  ensure
33
37
  Rails.application.certified = false
34
38
  end
@@ -18,27 +18,28 @@ class TestLiveData1 < Test::Unit::TestCase
18
18
  fake_resource_path = File.join earth_gem_path, 'lib', 'earth', 'live_data1_resource.rb'
19
19
  File.open(fake_resource_path, 'w') { |f| f.write "class ::LiveData1Resource < ActiveRecord::Base; end"}
20
20
  eval File.read(fake_resource_path) unless defined?(::LiveData1Resource)
21
+
22
+ # faking deploy
23
+ Rails.env = ActiveSupport::StringInquirer.new 'production'
24
+ Rails.root = '/var/www/data1/current'
25
+ FileUtils.mkdir_p '/var/www/data1/current/config/brighter_planet_deploy'
26
+ FileUtils.mkdir_p '/var/www/data1/current/public/brighter_planet_deploy'
27
+ File.open('/var/www/data1/current/config/brighter_planet_deploy/service', 'w') { |f| f.write 'ReferenceDataService' }
21
28
 
22
29
  # FAKING DATASETS
23
- ::Rails.root = '/data/data1/current'
24
30
  eval "class ::Dataset; end"
25
-
26
- # fake what looks like a dataset
27
31
  fake_dataset_path = File.join ::Rails.root, 'app', 'models', 'live_data1_dataset.rb'
28
32
  File.open(fake_dataset_path, 'w') { |f| f.write "class ::LiveData1Dataset < Dataset; end"}
29
33
  eval File.read(fake_dataset_path) unless defined?(::LiveData1Dataset)
30
-
31
- # FAKING A UNIVERSE
32
- FileUtils.mkdir_p '/etc/brighterplanet'
33
- File.open('/etc/brighterplanet/universe', 'w') { |f| f.write 'data1_production' }
34
34
  end
35
-
36
- def test_universe
37
- assert_equal 'data1_production', ::BrighterPlanet.metadata.send(:universe)
35
+
36
+ def test_self_awareness
37
+ assert ::Rails.env.production?
38
+ assert_equal 'ReferenceDataService', ::BrighterPlanet.deploy.servers.me.service
38
39
  end
39
40
 
40
41
  def test_authority
41
- assert ::BrighterPlanet.metadata.send(:data1_authority).authority?('data1_production', 'resources')
42
+ assert ::BrighterPlanet.metadata.send(:data1_authority).authority?('resources')
42
43
  end
43
44
 
44
45
  def test_resources
@@ -14,7 +14,6 @@ class TestLiveRemote < Test::Unit::TestCase
14
14
  'http://certified.carbon.brighterplanet.com/emitters.json' => { 'emitters' => %w{ LiveRemoteCertifiedEmitter } },
15
15
  'http://data.brighterplanet.com/resources.json' => { 'resources' => %w{ LiveRemoteResource } },
16
16
  'http://carbon.brighterplanet.com/protocols.json' => { 'protocols' => { 'fooprotocol' => 'Foo Protocol' } },
17
- 'http://carbon.brighterplanet.com/color.json' => { 'color' => 'magenta'}
18
17
  }.each do |url, hsh|
19
18
  FakeWeb.register_uri :get,
20
19
  url,
@@ -50,8 +49,4 @@ class TestLiveRemote < Test::Unit::TestCase
50
49
  def test_protocols
51
50
  assert ::BrighterPlanet.metadata.protocols.values.include?('Foo Protocol')
52
51
  end
53
-
54
- def test_color
55
- assert_equal 'magenta', ::BrighterPlanet.metadata.color
56
- end
57
52
  end
data/test/test_real.rb CHANGED
@@ -30,8 +30,4 @@ class TestReal < Test::Unit::TestCase
30
30
  def test_protocols
31
31
  assert ::BrighterPlanet.metadata.protocols.values.include?('The Climate Registry')
32
32
  end
33
-
34
- def test_color
35
- assert %w{ red blue }.include?(::BrighterPlanet.metadata.color)
36
- end
37
33
  end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brighter_planet_metadata
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 4
10
- version: 0.0.4
5
+ version: 0.0.5
11
6
  platform: ruby
12
7
  authors:
13
8
  - Seamus Abshere
@@ -16,7 +11,7 @@ autorequire:
16
11
  bindir: bin
17
12
  cert_chain: []
18
13
 
19
- date: 2011-03-29 00:00:00 -05:00
14
+ date: 2011-06-02 00:00:00 -05:00
20
15
  default_executable:
21
16
  dependencies:
22
17
  - !ruby/object:Gem::Dependency
@@ -27,9 +22,6 @@ dependencies:
27
22
  requirements:
28
23
  - - ">="
29
24
  - !ruby/object:Gem::Version
30
- hash: 3
31
- segments:
32
- - 0
33
25
  version: "0"
34
26
  type: :runtime
35
27
  version_requirements: *id001
@@ -41,11 +33,6 @@ dependencies:
41
33
  requirements:
42
34
  - - ">="
43
35
  - !ruby/object:Gem::Version
44
- hash: 11
45
- segments:
46
- - 2
47
- - 3
48
- - 4
49
36
  version: 2.3.4
50
37
  type: :runtime
51
38
  version_requirements: *id002
@@ -57,9 +44,6 @@ dependencies:
57
44
  requirements:
58
45
  - - ">="
59
46
  - !ruby/object:Gem::Version
60
- hash: 3
61
- segments:
62
- - 0
63
47
  version: "0"
64
48
  type: :runtime
65
49
  version_requirements: *id003
@@ -71,79 +55,61 @@ dependencies:
71
55
  requirements:
72
56
  - - ">="
73
57
  - !ruby/object:Gem::Version
74
- hash: 3
75
- segments:
76
- - 0
77
58
  version: "0"
78
59
  type: :runtime
79
60
  version_requirements: *id004
80
61
  - !ruby/object:Gem::Dependency
81
- name: fakeweb
62
+ name: brighter_planet_deploy
82
63
  prerelease: false
83
64
  requirement: &id005 !ruby/object:Gem::Requirement
84
65
  none: false
85
66
  requirements:
86
67
  - - ">="
87
68
  - !ruby/object:Gem::Version
88
- hash: 3
89
- segments:
90
- - 0
91
69
  version: "0"
92
70
  type: :development
93
71
  version_requirements: *id005
94
72
  - !ruby/object:Gem::Dependency
95
- name: test-unit
73
+ name: fakeweb
96
74
  prerelease: false
97
75
  requirement: &id006 !ruby/object:Gem::Requirement
98
76
  none: false
99
77
  requirements:
100
78
  - - ">="
101
79
  - !ruby/object:Gem::Version
102
- hash: 3
103
- segments:
104
- - 0
105
80
  version: "0"
106
81
  type: :development
107
82
  version_requirements: *id006
108
83
  - !ruby/object:Gem::Dependency
109
- name: fakefs
84
+ name: test-unit
110
85
  prerelease: false
111
86
  requirement: &id007 !ruby/object:Gem::Requirement
112
87
  none: false
113
88
  requirements:
114
89
  - - ">="
115
90
  - !ruby/object:Gem::Version
116
- hash: 3
117
- segments:
118
- - 0
119
91
  version: "0"
120
92
  type: :development
121
93
  version_requirements: *id007
122
94
  - !ruby/object:Gem::Dependency
123
- name: earth
95
+ name: fakefs
124
96
  prerelease: false
125
97
  requirement: &id008 !ruby/object:Gem::Requirement
126
98
  none: false
127
99
  requirements:
128
100
  - - ">="
129
101
  - !ruby/object:Gem::Version
130
- hash: 3
131
- segments:
132
- - 0
133
102
  version: "0"
134
103
  type: :development
135
104
  version_requirements: *id008
136
105
  - !ruby/object:Gem::Dependency
137
- name: fastercsv
106
+ name: earth
138
107
  prerelease: false
139
108
  requirement: &id009 !ruby/object:Gem::Requirement
140
109
  none: false
141
110
  requirements:
142
111
  - - ">="
143
112
  - !ruby/object:Gem::Version
144
- hash: 3
145
- segments:
146
- - 0
147
113
  version: "0"
148
114
  type: :development
149
115
  version_requirements: *id009
@@ -164,13 +130,11 @@ files:
164
130
  - bin/brighter_planet_metadata
165
131
  - brighter_planet_metadata.gemspec
166
132
  - lib/brighter_planet_metadata.rb
167
- - lib/brighter_planet_metadata/authoritative_dns_resolver.rb
168
133
  - lib/brighter_planet_metadata/cm1_authority.rb
169
134
  - lib/brighter_planet_metadata/data1_authority.rb
170
135
  - lib/brighter_planet_metadata/metadata.rb
171
136
  - lib/brighter_planet_metadata/version.rb
172
137
  - test/helper.rb
173
- - test/test_developer_machine.rb
174
138
  - test/test_fallback.rb
175
139
  - test/test_live_cm1.rb
176
140
  - test/test_live_data1.rb
@@ -191,18 +155,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
191
155
  requirements:
192
156
  - - ">="
193
157
  - !ruby/object:Gem::Version
194
- hash: 3
195
- segments:
196
- - 0
197
158
  version: "0"
198
159
  required_rubygems_version: !ruby/object:Gem::Requirement
199
160
  none: false
200
161
  requirements:
201
162
  - - ">="
202
163
  - !ruby/object:Gem::Version
203
- hash: 3
204
- segments:
205
- - 0
206
164
  version: "0"
207
165
  requirements: []
208
166
 
@@ -213,7 +171,6 @@ specification_version: 3
213
171
  summary: Names of Brighter Planet things
214
172
  test_files:
215
173
  - test/helper.rb
216
- - test/test_developer_machine.rb
217
174
  - test/test_fallback.rb
218
175
  - test/test_live_cm1.rb
219
176
  - test/test_live_data1.rb
@@ -1,12 +0,0 @@
1
- require 'dnsruby'
2
- module BrighterPlanet
3
- class Metadata
4
- module AuthoritativeDnsResolver
5
- def self.getaddress(domain_name)
6
- r = ::Dnsruby::Resolver.new(:nameserver => %w{ ns1.easydns.com ns2.easydns.com ns3.easydns.org })
7
- q = r.query domain_name, ::Dnsruby::Types.A, ::Dnsruby::Classes.IN
8
- q.answer[0].address.to_s
9
- end
10
- end
11
- end
12
- end
@@ -1,17 +0,0 @@
1
- require 'helper'
2
-
3
- class TestDeveloperMachine < Test::Unit::TestCase
4
- def test_developing_cm1_locally
5
- ENV['BRIGHTER_PLANET_METADATA_FORCE_UNIVERSE'] = 'cm1_production'
6
- assert_equal 'cm1_production', BrighterPlanet.metadata.send(:universe)
7
- ensure
8
- ENV['BRIGHTER_PLANET_METADATA_FORCE_UNIVERSE'] = ''
9
- end
10
-
11
- def test_developing_data1_locally
12
- ENV['BRIGHTER_PLANET_METADATA_FORCE_UNIVERSE'] = 'data1_production'
13
- assert_equal 'data1_production', BrighterPlanet.metadata.send(:universe)
14
- ensure
15
- ENV['BRIGHTER_PLANET_METADATA_FORCE_UNIVERSE'] = ''
16
- end
17
- end