Permalink
Browse files

quick and massive updates

  • Loading branch information...
1 parent 20672a3 commit eada852716592e6133f67fc3ee3f04bed09be573 @seamusabshere seamusabshere committed Mar 1, 2012
View
@@ -1,4 +1,8 @@
-source "http://rubygems.org"
+source :rubygems
-# Specify your gem's dependencies in brighter_planet_metadata.gemspec
gemspec
+
+# development dependencies
+gem 'webmock'
+gem 'test-unit'
+gem 'rake'
View
@@ -10,16 +10,3 @@ Rake::TestTask.new(:test) do |test|
end
task :default => :test
-
-begin
- require 'rake/rdoctask'
- Rake::RDocTask.new do |rdoc|
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = 'remote_table'
- rdoc.options << '--line-numbers' << '--inline-source'
- rdoc.rdoc_files.include('README*')
- rdoc.rdoc_files.include('lib/**/*.rb')
- end
-rescue LoadError
- puts "Rdoc is not available"
-end
@@ -5,7 +5,6 @@ require "brighter_planet_metadata/version"
Gem::Specification.new do |s|
s.name = "brighter_planet_metadata"
s.version = BrighterPlanetMetadata::VERSION
- s.platform = Gem::Platform::RUBY
s.authors = ["Seamus Abshere", "Andy Rossmeissl"]
s.email = ["seamus@abshere.net"]
s.homepage = "https://github.com/brighterplanet/brighter_planet_metadata"
@@ -22,13 +21,5 @@ Gem::Specification.new do |s|
s.add_dependency 'activesupport', '>=2.3.4'
s.add_dependency 'i18n' # activesupport?
s.add_dependency 'cache_method'
- s.add_development_dependency 'brighter_planet_deploy'
- s.add_development_dependency 'fakeweb'
- s.add_development_dependency 'test-unit'
- s.add_development_dependency 'fakefs'
- s.add_development_dependency 'earth'
- s.add_development_dependency 'rake'
- unless RUBY_VERSION > '1.9'
- s.add_development_dependency 'fastercsv' # earth
- end
+ s.add_dependency 'multi_json'
end
@@ -1,7 +1,7 @@
require 'cache_method'
+require 'brighter_planet_metadata/metadata'
module BrighterPlanet
- autoload :Metadata, 'brighter_planet_metadata/metadata'
def self.metadata
Metadata.instance
end
@@ -1,40 +0,0 @@
-require 'singleton'
-module BrighterPlanet
- class Metadata
- class Cm1Authority
- include ::Singleton
- def authority?(method_id)
- return unless defined?(::Rails) and ::Rails.env.production? and brighter_planet_server?
- method_id = method_id.to_s
- if method_id == 'certified_emitters'
- ::Rails.application.certified?
- else
- respond_to? method_id
- end
- end
- def emitters
- ary = []
- ::ObjectSpace.each_object(::BrighterPlanet::Emitter) do |obj|
- ary.push obj.name.demodulize
- end
- ary
- end
- def certified_emitters
- emitters
- end
- def protocols
- emitters.map(&:constantize).map(&:protocols).flatten.uniq.inject({}) do |memo, p|
- memo[p] = ::File.read(::File.join(::Rails.root, 'app', 'views', 'protocols', 'names', "_#{p}.html.erb")).strip
- memo
- end
- end
- private
- def brighter_planet_server?
- require 'brighter_planet_deploy'
- ::BrighterPlanet.deploy.servers.me.service == 'Cm1'
- rescue ::Exception
- false
- end
- end
- end
-end
@@ -1,25 +0,0 @@
-require 'singleton'
-module BrighterPlanet
- class Metadata
- class Data1Authority
- include ::Singleton
- def authority?(method_id)
- return unless defined?(::Rails) and ::Rails.env.production? and brighter_planet_server?
- respond_to? method_id
- end
- def resources
- ::Earth.resources.sort
- end
- def datasets
- ::Dir[::File.expand_path(::File.join(::Rails.root, 'app', 'models', '**', '*.rb'))].select { |f| ::File.read(f) =~ /class [\:A-Za-z0-9]+ < Dataset/ }.map { |path| ::File.basename(path, '.rb').camelcase }.sort
- end
- private
- def brighter_planet_server?
- require 'brighter_planet_deploy'
- ::BrighterPlanet.deploy.servers.me.service == 'Data1'
- rescue ::Exception
- false
- end
- end
- end
-end
@@ -1,14 +1,12 @@
require 'singleton'
require 'eat'
+require 'multi_json'
require 'active_support'
require 'active_support/version'
-%w{
- active_support/json
- active_support/core_ext/object/blank
- active_support/inflector/inflections
-}.each do |active_support_3_requirement|
- require active_support_3_requirement
-end if ::ActiveSupport::VERSION::MAJOR == 3
+if ::ActiveSupport::VERSION::MAJOR >= 3
+ require 'active_support/core_ext'
+ require 'active_support/inflector/inflections'
+end
::ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable %w{ aircraft bts_aircraft }
@@ -23,10 +21,10 @@ class Metadata
include ::Singleton
LIVE_URL = {
'datasets' => 'http://data.brighterplanet.com/datasets.json',
- 'emitters' => 'http://carbon.brighterplanet.com/emitters.json',
- 'certified_emitters' => 'http://certified.carbon.brighterplanet.com/emitters.json',
+ 'emitters' => 'http://impact.brighterplanet.com/emitters.json',
+ 'certified_emitters' => 'http://certified.impact.brighterplanet.com/emitters.json',
'resources' => 'http://data.brighterplanet.com/resources.json',
- 'protocols' => 'http://carbon.brighterplanet.com/protocols.json',
+ 'protocols' => 'http://impact.brighterplanet.com/protocols.json',
}.freeze
# sabshere 2/4/11 obv these have to be updated with some regularity
@@ -66,56 +64,29 @@ def protocols
# Clear out any cached values
def refresh
# instance_variables.each { |ivar_name| instance_variable_set ivar_name, nil }
- clear_method_cache :authoritative_value_or_fallback
+ cache_method_clear :authoritative_value_or_fallback
end
# for cache_method
- HASH = 'BrighterPlanet::Metadata.instance'.hash
- def method_cache_hash
- HASH
+ def as_cache_key
+ 'BrighterPlanet::Metadata.instance'
end
private
-
- autoload :Cm1Authority, 'brighter_planet_metadata/cm1_authority'
- def cm1_authority
- Cm1Authority.instance
- end
-
- autoload :Data1Authority, 'brighter_planet_metadata/data1_authority'
- def data1_authority
- Data1Authority.instance
- end
-
- def authorities
- [ cm1_authority, data1_authority ]
- end
-
+
def deep_copy_of_authoritative_value_or_fallback(k)
- v = authoritative_value_or_fallback k
- case v
- when ::Hash
- ::Hash[(v.map { |k, vv| [ k.to_s.dup, vv.to_s.dup] })]
- when ::Array
- v.map { |vv| vv.to_s.dup }
- when ::String, ::Symbol
- v.to_s.dup
- else
- raise "i only handle arrays of strings, hashes of strings, and strings"
- end
+ authoritative_value_or_fallback(k).clone
end
# Used internally to pull a live list of emitters/datasets/etc. or fall back to a static one.
def authoritative_value_or_fallback(k)
k = k.to_s
- if (authority = authorities.detect { |a| a.authority? k })
- authority.send k
- elsif ::ENV['BRIGHTER_PLANET_METADATA_FALLBACKS_ONLY'] == 'true'
+ if ::ENV['BRIGHTER_PLANET_METADATA_FALLBACKS_ONLY'] == 'true'
$stderr.puts %{ENV['BRIGHTER_PLANET_METADATA_FALLBACKS_ONLY'] == 'true', so using fallback value for '#{k}'}
FALLBACK[k]
else
begin
- hsh = ::ActiveSupport::JSON.decode eat(LIVE_URL[k])
+ hsh = ::MultiJson.decode eat(LIVE_URL[k])
kk = (k == 'certified_emitters') ? 'emitters' : k # the live certified response will contain an 'emitters' key
raise unless hsh.has_key? kk
hsh[kk]
@@ -1,3 +1,3 @@
module BrighterPlanetMetadata
- VERSION = "0.0.16"
+ VERSION = "0.1.0"
end
View
@@ -2,44 +2,21 @@
require 'bundler'
Bundler.setup
require 'test/unit'
-require 'fakeweb'
-require 'fakefs/safe'
+require 'webmock/test_unit'
require 'fileutils'
-require 'active_support/string_inquirer'
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'brighter_planet_metadata'
+
class Test::Unit::TestCase
def setup
BrighterPlanet.metadata.refresh
- FakeWeb.clean_registry
- FakeWeb.allow_net_connect = false
- Rails.application.certified = false
- Rails.root = '/var/www/myapp'
- Rails.env = ActiveSupport::StringInquirer.new 'development'
+ WebMock.enable!
+ WebMock.disable_net_connect!
end
def teardown
- FakeFS::FileSystem.clear
- FakeFS.deactivate!
- FakeWeb.allow_net_connect = true
- Rails.application.certified = false
- Rails.root = nil
- end
-end
-
-require 'singleton'
-require 'active_support/core_ext/module'
-module Rails
- mattr_accessor :root, :env
- def self.application
- FakeApplication.instance
- end
- class FakeApplication
- include ::Singleton
- attr_writer :certified
- def certified?
- @certified == true
- end
+ WebMock.reset!
+ WebMock.disable!
end
end
View
@@ -3,19 +3,14 @@
class TestFallback < Test::Unit::TestCase
def setup
super
- FakeWeb.clean_registry
- FakeWeb.allow_net_connect = false
[
'http://data.brighterplanet.com/datasets.json',
- 'http://carbon.brighterplanet.com/emitters.json',
- 'http://certified.carbon.brighterplanet.com/emitters.json',
+ 'http://impact.brighterplanet.com/emitters.json',
+ 'http://certified.impact.brighterplanet.com/emitters.json',
'http://data.brighterplanet.com/resources.json',
- 'http://carbon.brighterplanet.com/protocols.json',
+ 'http://impact.brighterplanet.com/protocols.json',
].each do |url|
- FakeWeb.register_uri :get,
- url,
- :status => ["500", "Urg"],
- :body => nil
+ WebMock.stub_request(:get, url).to_return(:status => 500)
end
end
View
@@ -1,60 +0,0 @@
-require 'helper'
-
-module BrighterPlanet
- module Emitter
- end
- module LiveCm1Emitter
- extend BrighterPlanet::Emitter
- end
-end
-
-class TestLiveCm1 < Test::Unit::TestCase
- def setup
- super
- FakeFS.activate!
- Rails.env = ActiveSupport::StringInquirer.new 'production'
- Rails.root = '/data/edge/current'
- FileUtils.mkdir_p '/data/edge/current/config/brighter_planet_deploy'
- FileUtils.mkdir_p '/data/edge/current/public/brighter_planet_deploy'
- File.open('/data/edge/current/config/brighter_planet_deploy/service', 'w') { |f| f.write 'Cm1' }
- end
-
- def test_self_awareness
- assert ::Rails.env.production?
- assert_equal 'Cm1', ::BrighterPlanet.deploy.servers.me.service
- end
-
- def test_authority
- assert ::BrighterPlanet::Metadata::Cm1Authority.instance.authority?('emitters')
-
- # you don't have authority to say what's certified...
- assert !::BrighterPlanet::Metadata::Cm1Authority.instance.authority?('certified_emitters')
-
- # now you do
- Rails.application.certified = true
- assert ::BrighterPlanet::Metadata::Cm1Authority.instance.authority?('certified_emitters')
- ensure
- Rails.application.certified = false
- end
-
- def test_emitters
- assert_equal %w{LiveCm1Emitter}, ::BrighterPlanet.metadata.emitters
- end
-
- def test_what_must_come_from_other_sources
- assert_equal ::BrighterPlanet::Metadata::FALLBACK['resources'], ::BrighterPlanet.metadata.resources
- end
-
- # note: you still get a list of certified emitters! the point is that you, as the edge server, don't decide which ones they are
- # in other words, nothing is ever certified unless Rails.application.certified?
- def test_certified_emitters_as_if_on_edge
- assert_equal ::BrighterPlanet::Metadata::FALLBACK['certified_emitters'], ::BrighterPlanet.metadata.certified_emitters
- end
-
- def test_certified_emitters_as_if_on_certified
- Rails.application.certified = true
- assert_equal %w{LiveCm1Emitter}, ::BrighterPlanet.metadata.certified_emitters
- ensure
- Rails.application.certified = false
- end
-end
Oops, something went wrong.

0 comments on commit eada852

Please sign in to comment.