Permalink
Browse files

Merge remote branch 'upstream/master'

  • Loading branch information...
2 parents d188d71 + 4bd5a95 commit 262a985733aa42979784065c3243aa8c5451d9fa @yonkeltron committed Aug 22, 2011
View
@@ -0,0 +1,63 @@
+ENV["WATCHR"] = "1"
+system 'clear'
+
+def growl(title, message)
+ growlnotify = `which growlnotify`.chomp
+ puts message
+ image = message.match(/\s0\s(errors|failures)/) ? "~/.watchr_images/passed.png" : "~/.watchr_images/failed.png"
+ options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'"
+ system %(#{growlnotify} #{options} &)
+end
+
+def run(cmd)
+ puts(cmd)
+ `#{cmd}`
+end
+
+def run_spec_file(file)
+ system('clear')
+ result = run(%Q(rspec #{file}))
+ growl( "Watchr results for '#{file}'", result.split("\n").last ) rescue nil
+ puts result
+end
+
+def run_all_specs
+ system('clear')
+ result = run "rspec spec"
+ growl( "Watchr results for all specs", result.split("\n").last ) rescue nil
+ puts result
+end
+
+def related_spec_files(path)
+ Dir['spec/**/*.rb'].select { |file| file =~ /#{File.basename(path).split(".").first}_spec.rb/ }
+end
+
+def run_suite
+ run_all_specs
+end
+
+watch('spec/spec_helper\.rb') { run_all_specs }
+watch('spec/.*_spec\.rb') { |m| run_spec_file(m[0]) }
+watch('lib/.*/.*\.rb') { |m| related_spec_files(m[0]).map {|tf| run_spec_file(tf) } }
+
+# Ctrl-\
+Signal.trap 'QUIT' do
+ puts " --- Running all specs ---\n\n"
+ run_all_specs
+end
+
+@interrupted = false
+
+# Ctrl-C
+Signal.trap 'INT' do
+ if @interrupted then
+ @wants_to_quit = true
+ abort("\n")
+ else
+ puts "Interrupt a second time to quit"
+ @interrupted = true
+ Kernel.sleep 1.5
+ # raise Interrupt, nil # let the run loop catch it
+ run_suite
+ end
+end
View
@@ -1 +1 @@
-0.2.0
+0.3.0
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{federal_register}
- s.version = "0.2.0"
+ s.version = "0.3.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Andrew Carpenter"]
- s.date = %q{2011-08-17}
+ s.date = %q{2011-08-22}
s.description = %q{Ruby API Client for FederalRegister.gov that handles searching articles and getting information about agencies}
s.email = %q{andrew@criticaljuncture.org}
s.extra_rdoc_files = [
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
]
s.files = [
".document",
+ ".watchr",
"Gemfile",
"Gemfile.lock",
"LICENSE.txt",
@@ -33,15 +34,14 @@ Gem::Specification.new do |s|
"lib/federal_register/result_set.rb",
"spec/agency_spec.rb",
"spec/article_spec.rb",
+ "spec/base_spec.rb",
"spec/result_set_spec.rb",
- "spec/spec_helper.rb",
- "test/helper.rb",
- "test/test_federal_register.rb"
+ "spec/spec_helper.rb"
]
s.homepage = %q{http://github.com/criticaljuncture/federal_register}
s.licenses = ["MIT"]
s.require_paths = ["lib"]
- s.rubygems_version = %q{1.5.2}
+ s.rubygems_version = %q{1.7.2}
s.summary = %q{Ruby API Client for FederalRegister.gov}
if s.respond_to? :specification_version then
@@ -1,15 +1,12 @@
class FederalRegister::Agency < FederalRegister::Base
- ATTRIBUTES = [
- :id,
- :name,
- :short_name,
- :url,
- :description,
- :url,
- :recent_articles_url,
- :logo,
- :json_url
- ]
+ add_attribute :description,
+ :id,
+ :json_url,
+ :logo,
+ :name,
+ :recent_articles_url,
+ :short_name,
+ :url
def self.all
response = get('/agencies.json')
@@ -27,4 +24,4 @@ def logo_url(size)
attributes["logo"]["#{size}_url"] || raise("size '#{size}' not a valid image size")
end
end
-end
+end
@@ -1,30 +1,30 @@
class FederalRegister::Article < FederalRegister::Base
- ATTRIBUTES = [
- :title,
- :type,
- :abstract,
- :document_number,
- :html_url,
- :pdf_url,
- :publication_date,
- :agencies,
- :full_text_xml_url,
- :abstract_html_url,
- :body_html_url,
- :mods_url,
- :action,
- :dates,
- :effective_on,
- :comments_close_on,
- :start_page,
- :end_page,
- :volume,
- :docket_id,
- :regulation_id_numbers,
- :cfr_references,
- :json_url
- ]
-
+ add_attribute :abstract,
+ :abstract_html_url,
+ :action,
+ :agencies,
+ :body_html_url,
+ :cfr_references,
+ :dates,
+ :docket_id,
+ :document_number,
+ :end_page,
+ :full_text_xml_url,
+ :html_url,
+ :json_url,
+ :mods_url,
+ :pdf_url,
+ :regulation_id_numbers,
+ :start_page,
+ :title,
+ :type,
+ :volume
+
+ add_attribute :comments_close_on,
+ :effective_on,
+ :publication_date,
+ :type => :date
+
def self.search(args)
FederalRegister::ResultSet.fetch("/articles.json", :query => args, :result_class => self)
end
@@ -46,7 +46,7 @@ def agencies
%w(full_text_xml abstract_html body_html mods).each do |file_type|
define_method file_type do
- self.class.get(attributes["#{file_type}_url"]).body
+ self.class.get(send("#{file_type}_url")).body
end
end
end
@@ -1,5 +1,29 @@
class FederalRegister::Base < FederalRegister::Client
attr_reader :attributes
+
+ def self.add_attribute(*attributes)
+ options = {}
+
+ if attributes.last.is_a?(Hash)
+ options = attributes.pop
+ end
+
+ attributes.each do |attr|
+ define_method attr do
+ val = @attributes[attr.to_s]
+ if val && options[:type] == :date
+ val = Date.strptime(val.to_s)
+ end
+
+ if ! val && ! full? && respond_to?(:json_url) && @attributes['json_url']
+ fetch_full
+ val = send(attr)
+ end
+ val
+ end
+ end
+ end
+
def initialize(attributes = {}, options = {})
@attributes = attributes
@full = options[:full] || false
@@ -14,23 +38,14 @@ def fetch_full
@full = true
self
end
-
+
+ def self.override_base_uri(uri)
+ [FederalRegister::Agency, FederalRegister::Article, FederalRegister::Base, FederalRegister::Client, FederalRegister:: ResultSet].each do |klass|
+ klass.base_uri(uri)
+ end
+ end
+
private
attr_reader :attributes
-
- def method_missing(name, *args)
- if attributes.has_key?(name.to_s)
- attributes[name.to_s]
- elsif self.class::ATTRIBUTES.include?(name.to_sym)
- if ! full? && @attributes['json_url']
- fetch_full
- method_missing(name,*args)
- else
- nil
- end
- else
- super
- end
- end
-end
+end
@@ -4,7 +4,7 @@ class FederalRegister::Client
class RecordNotFound < HTTParty::ResponseError; end
class ServerError < HTTParty::ResponseError; end
- base_uri 'http://www.federalregister.gov/api/v1'
+ base_uri 'http://api.federalregister.gov/v1'
def self.get(url, *options)
response = super
@@ -20,4 +20,4 @@ def self.get(url, *options)
raise HTTParty::ResponseError.new(response)
end
end
-end
+end
View
@@ -53,4 +53,25 @@
FederalRegister::Article.search(:conditions => {:term => "Fish"}).should be_an_instance_of(FederalRegister::ResultSet)
end
end
+
+ describe "#full_text_xml" do
+ it "fetches the full_text_xml from the full_text_xml_url" do
+ url = "http://example.com/full_text"
+ article = FederalRegister::Article.new("full_text_xml_url" => url)
+ FakeWeb.register_uri(
+ :get,
+ url,
+ :content_type =>"text/xml",
+ :body => "hello, world!"
+ )
+ article.full_text_xml.should == 'hello, world!'
+ end
+ end
+
+ describe "#publication_date" do
+ it "returns a Date object" do
+ article = FederalRegister::Article.new("publication_date" => "2011-07-22")
+ article.publication_date.should == Date.strptime("2011-07-22")
+ end
+ end
end
View
@@ -0,0 +1,37 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe FederalRegister::Base do
+ describe '.add_attribute' do
+ it 'creates a getter method of the same name' do
+ klass = Class.new(FederalRegister::Base)
+ klass.add_attribute(:foo)
+ instance = klass.new("foo" => "bar")
+ instance.foo.should == "bar"
+ end
+
+ it 'creates a getter method that lazy loads the full data' do
+ klass = Class.new(FederalRegister::Base)
+ klass.add_attribute(:foo, :json_url)
+ instance = klass.new('json_url' => 'http://example.com/details')
+ FakeWeb.register_uri(
+ :get,
+ "http://example.com/details",
+ :content_type =>"text/json",
+ :body => {:foo => "bar"}.to_json
+ )
+ instance.foo.should == 'bar'
+ end
+ end
+
+ describe '.override_base_uri' do
+ before(:each) do
+ FederalRegister::Base.override_base_uri('http://fr2.local/api/v1')
+ end
+
+ [FederalRegister::Agency, FederalRegister::Article, FederalRegister::Base, FederalRegister::Client, FederalRegister::ResultSet].each do |klass|
+ it "should set default_options[:base_uri] for #{klass}" do
+ klass.default_options[:base_uri].should == 'http://fr2.local/api/v1'
+ end
+ end
+ end
+end
View
@@ -1,18 +0,0 @@
-require 'rubygems'
-require 'bundler'
-begin
- Bundler.setup(:default, :development)
-rescue Bundler::BundlerError => e
- $stderr.puts e.message
- $stderr.puts "Run `bundle install` to install missing gems"
- exit e.status_code
-end
-require 'test/unit'
-require 'shoulda'
-
-$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
-$LOAD_PATH.unshift(File.dirname(__FILE__))
-require 'federal_register'
-
-class Test::Unit::TestCase
-end
@@ -1,7 +0,0 @@
-require 'helper'
-
-class TestFederalRegister < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
- end
-end

0 comments on commit 262a985

Please sign in to comment.