datacatalog 0.1.0 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,5 +1,5 @@
1
- .DS_Store
2
- coverage
3
- setup.rb
4
- datacatalog*.gem
1
+ .DS_Store
2
+ coverage
3
+ setup.rb
4
+ datacatalog*.gem
5
5
  sandbox_api.yml
data/CHANGES.md CHANGED
@@ -1,2 +1,2 @@
1
- ### 0.1.0 / 2009-08-15
2
- * Initial version
1
+ ### 0.1.0 / 2009-08-15
2
+ * Initial version
data/LICENSE.md CHANGED
@@ -1,11 +1,11 @@
1
- Copyright (c) 2009, Sunlight Foundation.
2
-
3
- All rights reserved.
4
-
5
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
-
7
- * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
- * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
- * Neither the name of Sunlight Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
-
11
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1
+ Copyright (c) 2009, Sunlight Foundation.
2
+
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
+ * Neither the name of Sunlight Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md CHANGED
@@ -1,31 +1,34 @@
1
- # Ruby Gem for the National Data Catalog API
2
-
3
- Under heavy development.
4
-
5
- ## Installation
6
-
7
- For now, the gem will not be packaged. Instead, install it manually:
8
-
9
- $ git clone git clone git://github.com/sunlightlabs/ruby-datacatalog.git
10
- $ cd ruby-datacatalog
11
- $ gem build datacatalog.gemspec
12
- $ gem install datacatalog-0.1.0.gem
13
-
14
- ## Usage
15
-
16
- require 'rubygems'
17
- require 'datacatalog'
18
-
19
- DataCatalog.api_key = 'c40505247a5e308a24d70a0118f76534b543795b'
20
-
21
- ## Running Specs
22
-
23
- We're not mocking out any of the web API calls in the specs. Instead, we expect developers who wish to run the specs to download and run a local sandbox instance of the [Data Catalog API](http://github.com/sunlightlabs/datacatalog-api), a Sinatra app:
24
-
25
- git clone git://github.com/sunlightlabs/datacatalog-api.git
26
-
27
- Get the app running like any normal Sinatra app, so you can choose to use thin or Passenger or new hotness like [Unicorn](http://unicorn.bogomips.org/). Some special considerations:
28
-
29
- 1. We recommend creating a `sandbox` entry in `datacatalog-api`'s `config.yml`.
30
- 2. Run `RACK_ENV=sandbox rake db:ensure_admin` in the `datacatalog-api` project to create a super admin for the API instance.
31
- 3. Back here in `ruby-datacatalog`, use the example file in `spec/` to create your own `spec/sandbox_api.yml` with the API key of the admin and your local URI.
1
+ # Ruby Gem for the National Data Catalog API
2
+
3
+ Under heavy development.
4
+
5
+ ## Installation
6
+
7
+ For now, the gem will not be packaged. Instead, install it manually:
8
+
9
+ $ git clone git clone git://github.com/sunlightlabs/ruby-datacatalog.git
10
+ $ cd ruby-datacatalog
11
+ $ rake check_dependencies
12
+ $ rake build
13
+ $ rake install # uses sudo
14
+ # Or, instead of the line above:
15
+ $ gem install pkg/datacatalog-0.1.0.gem
16
+
17
+ ## Usage
18
+
19
+ require 'rubygems'
20
+ require 'datacatalog'
21
+
22
+ DataCatalog.api_key = 'c40505247a5e308a24d70a0118f76534b543795b'
23
+
24
+ ## Running Specs
25
+
26
+ We're not mocking out any of the web API calls in the specs. Instead, we expect developers who wish to run the specs to download and run a local sandbox instance of the [Data Catalog API](http://github.com/sunlightlabs/datacatalog-api), a Sinatra app:
27
+
28
+ git clone git://github.com/sunlightlabs/datacatalog-api.git
29
+
30
+ Get the app running like any normal Sinatra app, so you can choose to use thin or Passenger or new hotness like [Unicorn](http://unicorn.bogomips.org/). Some special considerations:
31
+
32
+ 1. We recommend creating a `sandbox` entry in `datacatalog-api`'s `config.yml`.
33
+ 2. Run `RACK_ENV=sandbox rake db:ensure_admin` in the `datacatalog-api` project to create a super admin for the API instance.
34
+ 3. Back here in `ruby-datacatalog`, use the example file in `spec/` to create your own `spec/sandbox_api.yml` with the API key of the admin and your local URI.
data/Rakefile CHANGED
@@ -1,49 +1,51 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "datacatalog"
8
- gem.summary = %Q{Wrapper for the National Data Catalog API}
9
- gem.description = %Q{Ruby library that wraps the National Data Catalog API}
10
- gem.email = "luigi@sunlightfoundation.com"
11
- gem.homepage = "http://github.com/sunlightlabs/datacatalog"
12
- gem.authors = ["Luigi Montanez", "David James"]
13
- gem.add_development_dependency "rspec"
14
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
- end
16
- Jeweler::GemcutterTasks.new
17
- rescue LoadError
18
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
- end
20
-
21
- require 'spec/rake/spectask'
22
- Spec::Rake::SpecTask.new(:spec) do |spec|
23
- spec.libs << 'lib' << 'spec'
24
- spec.spec_files = FileList['spec/**/*_spec.rb']
25
- end
26
-
27
- Spec::Rake::SpecTask.new(:rcov) do |spec|
28
- spec.libs << 'lib' << 'spec'
29
- spec.pattern = 'spec/**/*_spec.rb'
30
- spec.rcov = true
31
- end
32
-
33
- task :spec => :check_dependencies
34
-
35
- task :default => :spec
36
-
37
- require 'rake/rdoctask'
38
- Rake::RDocTask.new do |rdoc|
39
- if File.exist?('VERSION')
40
- version = File.read('VERSION')
41
- else
42
- version = ""
43
- end
44
-
45
- rdoc.rdoc_dir = 'rdoc'
46
- rdoc.title = "datacatalog #{version}"
47
- rdoc.rdoc_files.include('README*')
48
- rdoc.rdoc_files.include('lib/**/*.rb')
49
- end
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "datacatalog"
8
+ gem.rubyforge_project = "datacatalog"
9
+ gem.summary = %Q{Wrapper for the National Data Catalog API}
10
+ gem.description = %Q{Ruby library that wraps the National Data Catalog API}
11
+ gem.email = "luigi@sunlightfoundation.com"
12
+ gem.homepage = "http://github.com/sunlightlabs/datacatalog"
13
+ gem.authors = ["Luigi Montanez", "David James"]
14
+ gem.add_dependency('activesupport', ">= 2.3.4")
15
+ gem.add_dependency('httparty', ">= 0.4.5")
16
+ gem.add_dependency('mash', ">= 0.0.3")
17
+ gem.add_development_dependency("jeweler", ">= 1.2.1")
18
+ gem.add_development_dependency("rspec", ">= 1.2.8")
19
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
20
+ end
21
+ Jeweler::GemcutterTasks.new
22
+ rescue LoadError
23
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
24
+ end
25
+
26
+ require 'spec/rake/spectask'
27
+ Spec::Rake::SpecTask.new(:spec) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.spec_opts = ['--color']
30
+ spec.spec_files = FileList['spec/**/*_spec.rb']
31
+ spec.rcov = true
32
+ spec.rcov_opts = ['--exclude', '^spec,/gems/']
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ if File.exist?('VERSION')
42
+ version = File.read('VERSION')
43
+ else
44
+ version = ""
45
+ end
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "datacatalog #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.3
data/datacatalog.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{datacatalog}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Luigi Montanez", "David James"]
12
- s.date = %q{2009-10-05}
12
+ s.date = %q{2009-10-09}
13
13
  s.description = %q{Ruby library that wraps the National Data Catalog API}
14
14
  s.email = %q{luigi@sunlightfoundation.com}
15
15
  s.extra_rdoc_files = [
@@ -24,17 +24,18 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "datacatalog.gemspec",
27
- "doc/api_key_spec_for_rest_api.txt",
28
- "doc/api_key_spec_for_ruby_api.rb",
29
- "doc/mocking_options.md",
27
+ "lib/base.rb",
30
28
  "lib/datacatalog.rb",
31
- "lib/datacatalog/api_key.rb",
32
- "lib/datacatalog/base.rb",
33
- "lib/datacatalog/source.rb",
34
- "lib/datacatalog/user.rb",
29
+ "lib/main.rb",
30
+ "lib/resources/about.rb",
31
+ "lib/resources/api_key.rb",
32
+ "lib/resources/source.rb",
33
+ "lib/resources/user.rb",
35
34
  "sandbox_api.yml.example",
35
+ "spec/about_spec.rb",
36
36
  "spec/api_key_spec.rb",
37
37
  "spec/base_spec.rb",
38
+ "spec/datacatalog_spec.rb",
38
39
  "spec/source_spec.rb",
39
40
  "spec/spec.opts",
40
41
  "spec/spec_helper.rb",
@@ -43,11 +44,14 @@ Gem::Specification.new do |s|
43
44
  s.homepage = %q{http://github.com/sunlightlabs/datacatalog}
44
45
  s.rdoc_options = ["--charset=UTF-8"]
45
46
  s.require_paths = ["lib"]
47
+ s.rubyforge_project = %q{datacatalog}
46
48
  s.rubygems_version = %q{1.3.5}
47
49
  s.summary = %q{Wrapper for the National Data Catalog API}
48
50
  s.test_files = [
49
- "spec/api_key_spec.rb",
51
+ "spec/about_spec.rb",
52
+ "spec/api_key_spec.rb",
50
53
  "spec/base_spec.rb",
54
+ "spec/datacatalog_spec.rb",
51
55
  "spec/source_spec.rb",
52
56
  "spec/spec_helper.rb",
53
57
  "spec/user_spec.rb"
@@ -58,11 +62,23 @@ Gem::Specification.new do |s|
58
62
  s.specification_version = 3
59
63
 
60
64
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
61
- s.add_development_dependency(%q<rspec>, [">= 0"])
65
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
66
+ s.add_runtime_dependency(%q<httparty>, [">= 0.4.5"])
67
+ s.add_runtime_dependency(%q<mash>, [">= 0.0.3"])
68
+ s.add_development_dependency(%q<jeweler>, [">= 1.2.1"])
69
+ s.add_development_dependency(%q<rspec>, [">= 1.2.8"])
62
70
  else
63
- s.add_dependency(%q<rspec>, [">= 0"])
71
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
72
+ s.add_dependency(%q<httparty>, [">= 0.4.5"])
73
+ s.add_dependency(%q<mash>, [">= 0.0.3"])
74
+ s.add_dependency(%q<jeweler>, [">= 1.2.1"])
75
+ s.add_dependency(%q<rspec>, [">= 1.2.8"])
64
76
  end
65
77
  else
66
- s.add_dependency(%q<rspec>, [">= 0"])
78
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
79
+ s.add_dependency(%q<httparty>, [">= 0.4.5"])
80
+ s.add_dependency(%q<mash>, [">= 0.0.3"])
81
+ s.add_dependency(%q<jeweler>, [">= 1.2.1"])
82
+ s.add_dependency(%q<rspec>, [">= 1.2.8"])
67
83
  end
68
84
  end
data/lib/base.rb ADDED
@@ -0,0 +1,77 @@
1
+ module DataCatalog
2
+
3
+ class Base < Mash
4
+
5
+ DEFAULT_BASE_URI = 'http://api.nationaldatacatalog.com'
6
+
7
+ include HTTParty
8
+
9
+ format :json
10
+ base_uri DEFAULT_BASE_URI
11
+
12
+ class << self
13
+ alias_method :_delete, :delete
14
+ alias_method :_get, :get
15
+ alias_method :_post, :post
16
+ alias_method :_put, :put
17
+
18
+ undef_method :delete
19
+ undef_method :get
20
+ undef_method :post
21
+ undef_method :put
22
+ end
23
+
24
+ def self.http_delete(path, options={})
25
+ check_status(_delete(path, options))
26
+ end
27
+
28
+ def self.http_get(path, options={})
29
+ check_status(_get(path, options))
30
+ end
31
+
32
+ def self.http_post(path, options={})
33
+ check_status(_post(path, options))
34
+ end
35
+
36
+ def self.http_put(path, options={})
37
+ check_status(_put(path, options))
38
+ end
39
+
40
+ # == protected
41
+
42
+ def self.check_status(response)
43
+ case response.code
44
+ when 400: raise BadRequest, error(response)
45
+ when 401: raise Unauthorized, error(response)
46
+ when 403: raise Forbidden, error(response)
47
+ when 404: raise NotFound, error(response)
48
+ when 409: raise Conflict, error(response)
49
+ when 500: raise InternalServerError, error(response)
50
+ end
51
+ response
52
+ end
53
+
54
+ def self.error(response)
55
+ parsed_body = JSON.parse(response.body)
56
+ if parsed_body.empty?
57
+ "Response was empty"
58
+ elsif parsed_body["errors"]
59
+ parsed_body["errors"].inspect
60
+ else
61
+ response.body
62
+ end
63
+ rescue JSON::ParserError
64
+ "Unable to parse: #{response.body.inspect}"
65
+ end
66
+
67
+ def self.many(response)
68
+ response.map { |item| new(item) }
69
+ end
70
+
71
+ def self.one(response)
72
+ response.blank? ? nil : new(response)
73
+ end
74
+
75
+ end
76
+
77
+ end
data/lib/datacatalog.rb CHANGED
@@ -1,28 +1,8 @@
1
- require 'rubygems'
2
- require 'activesupport'
3
- require 'httparty'
4
- require 'mash'
5
-
6
- module DataCatalog
7
- mattr_accessor :api_key, :base_uri
8
- class Error < RuntimeError; end
9
- class BadRequest < Error; end # 400
10
- class Unauthorized < Error; end # 401
11
- class Forbidden < Error; end # 403
12
- class NotFound < Error; end # 404
13
- class Conflict < Error; end # 409
14
- class InternalServerError < Error; end # 500
15
- class ApiKeyNotConfigured < Error; end
16
- class CannotDeletePrimaryApiKey < Error; end
17
-
18
- def self.with_key(temp_key)
19
- original_key = DataCatalog.api_key
20
- DataCatalog.api_key = temp_key
21
- yield
22
- DataCatalog.api_key = original_key
23
- end
24
-
25
- end
26
-
27
- require "#{File.dirname(__FILE__)}/datacatalog/base.rb"
28
- Dir["#{File.dirname(__FILE__)}/datacatalog/*.rb"].each { |source_file| require source_file }
1
+ require 'rubygems'
2
+ require 'activesupport'
3
+ require 'httparty'
4
+ require 'mash'
5
+
6
+ require File.dirname(__FILE__) + '/main'
7
+ require File.dirname(__FILE__) + '/base'
8
+ Dir.glob(File.dirname(__FILE__) + '/resources/*.rb').each { |f| require f }
data/lib/main.rb ADDED
@@ -0,0 +1,41 @@
1
+ module DataCatalog
2
+
3
+ # == Exceptions
4
+
5
+ class Error < RuntimeError; end
6
+ class BadRequest < Error; end # 400
7
+ class Unauthorized < Error; end # 401
8
+ class Forbidden < Error; end # 403
9
+ class NotFound < Error; end # 404
10
+ class Conflict < Error; end # 409
11
+ class InternalServerError < Error; end # 500
12
+ class ApiKeyNotConfigured < Error; end
13
+ class CannotDeletePrimaryApiKey < Error; end
14
+
15
+ # == Accessors
16
+
17
+ def self.api_key
18
+ Base.default_params[:api_key]
19
+ end
20
+
21
+ def self.api_key=(key)
22
+ Base.default_params :api_key => key
23
+ end
24
+
25
+ def self.base_uri
26
+ Base.base_uri
27
+ end
28
+
29
+ def self.base_uri=(uri)
30
+ Base.base_uri(uri.blank? ? Base::DEFAULT_BASE_URI : uri)
31
+ end
32
+
33
+ def self.with_key(temp_key)
34
+ original_key = DataCatalog.api_key
35
+ DataCatalog.api_key = temp_key
36
+ result = yield
37
+ DataCatalog.api_key = original_key
38
+ result
39
+ end
40
+
41
+ end