datacatalog 0.4.16 → 0.4.17

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ begin
5
5
  require 'jeweler'
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "datacatalog"
8
- gem.version = '0.4.16'
8
+ gem.version = '0.4.17'
9
9
  gem.rubyforge_project = "datacatalog"
10
10
  gem.summary = %Q{Client for the National Data Catalog API}
11
11
  gem.description = %Q{A Ruby client library for the National Data Catalog API}
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.4.16"
8
+ s.version = "0.4.17"
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{2010-08-10}
12
+ s.date = %q{2010-08-11}
13
13
  s.description = %q{A Ruby client library for the National Data Catalog API}
14
14
  s.email = %q{luigi@sunlightfoundation.com}
15
15
  s.extra_rdoc_files = [
@@ -46,6 +46,7 @@ Gem::Specification.new do |s|
46
46
  "spec/about_spec.rb",
47
47
  "spec/api_key_spec.rb",
48
48
  "spec/base_spec.rb",
49
+ "spec/broken_link_spec.rb",
49
50
  "spec/comment_spec.rb",
50
51
  "spec/datacatalog_spec.rb",
51
52
  "spec/document_spec.rb",
@@ -77,6 +78,7 @@ Gem::Specification.new do |s|
77
78
  "spec/about_spec.rb",
78
79
  "spec/api_key_spec.rb",
79
80
  "spec/base_spec.rb",
81
+ "spec/broken_link_spec.rb",
80
82
  "spec/comment_spec.rb",
81
83
  "spec/datacatalog_spec.rb",
82
84
  "spec/document_spec.rb",
@@ -14,6 +14,10 @@ module DataCatalog
14
14
  one(http_delete(uri(id)))
15
15
  end
16
16
 
17
+ def self.first(conditions={})
18
+ _first(http_get(uri, :query => query_hash(conditions)))
19
+ end
20
+
17
21
  def self.get(id)
18
22
  one(http_get(uri(id)))
19
23
  end
@@ -14,6 +14,10 @@ module DataCatalog
14
14
  one(http_delete(uri(id)))
15
15
  end
16
16
 
17
+ def self.first(conditions={})
18
+ _first(http_get(uri, :query => query_hash(conditions)))
19
+ end
20
+
17
21
  def self.get(id)
18
22
  one(http_get(uri(id)))
19
23
  end
data/spec/about_spec.rb CHANGED
@@ -19,7 +19,6 @@ describe DataCatalog do
19
19
  end
20
20
 
21
21
  describe ".version_at_least?" do
22
-
23
22
  it "should return false if below the actual version" do
24
23
  version_mock = mock(Object.new).version { '1.0.0' }
25
24
  mock(About).get { version_mock }
data/spec/base_spec.rb CHANGED
@@ -3,6 +3,13 @@ include DataCatalog
3
3
 
4
4
  describe Base do
5
5
 
6
+ def make_response(body, code)
7
+ response = Object.new
8
+ stub(response).code { code }
9
+ stub(response).body { body }
10
+ response
11
+ end
12
+
6
13
  before do
7
14
  setup_api
8
15
  end
@@ -22,43 +29,39 @@ describe Base do
22
29
  end
23
30
 
24
31
  describe ".check_status" do
25
- it "should return nil on 200 OK" do
26
- response = HTTParty::Response.new(nil, '{"foo":"bar"}', 200, 'OK', {})
27
- Base.check_status(response).should be_nil
32
+ it "should return response on 200 OK" do
33
+ response = make_response("{}", 200)
34
+ Base.check_status(response).should == response
28
35
  end
29
36
 
30
37
  it "should raise BadRequest on 400 Bad Request" do
31
- response = HTTParty::Response.new(nil, '[]', 400, 'Bad Request', {})
32
38
  executing do
33
- Base.check_status(response)
39
+ Base.check_status(make_response("{}", 400))
34
40
  end.should raise_error(BadRequest)
35
41
  end
36
42
 
37
43
  it "should raise Unauthorized on 401 Unauthorized" do
38
- response = HTTParty::Response.new(nil, '', 401, 'Unauthorized', {})
39
44
  executing do
40
- Base.check_status(response)
45
+ Base.check_status(make_response("{}", 401))
41
46
  end.should raise_error(Unauthorized)
42
47
  end
43
48
 
44
49
  it "should raise NotFound on 404 Not Found" do
45
- response = HTTParty::Response.new(nil, '[]', 404, 'Not Found', {})
46
50
  executing do
47
- Base.check_status(response)
51
+ Base.check_status(make_response("{}", 404))
48
52
  end.should raise_error(NotFound)
49
53
  end
50
54
 
51
55
  it "should raise InternalServerError on 500 Internal Server Error" do
52
- response = HTTParty::Response.new(nil, '', 500, 'Internal Server Error', {})
53
56
  executing do
54
- Base.check_status(response)
57
+ Base.check_status(make_response("{}", 500))
55
58
  end.should raise_error(InternalServerError)
56
59
  end
57
60
  end
58
-
61
+
59
62
  describe ".error" do
60
- it "should ... when body is blank" do
61
- response = HTTParty::Response.new(nil, '', 404, 'Not Found', {})
63
+ it "should be correct when body is blank and code is 404" do
64
+ response = make_response('', 404)
62
65
  begin
63
66
  e = Base.error(NotFound, response)
64
67
  rescue NotFound => e
@@ -69,7 +72,7 @@ describe Base do
69
72
  end
70
73
 
71
74
  it "should be correct when body is an empty hash" do
72
- response = HTTParty::Response.new(nil, '{}', 404, 'Not Found', {})
75
+ response = make_response('{}', 404)
73
76
  begin
74
77
  e = Base.error(NotFound, response).should == "Response was empty"
75
78
  rescue NotFound => e
@@ -80,7 +83,7 @@ describe Base do
80
83
  end
81
84
 
82
85
  it "should be correct when body is an empty array" do
83
- response = HTTParty::Response.new(nil, '[]', 404, 'Not Found', {})
86
+ response = make_response('[]', 404)
84
87
  begin
85
88
  e = Base.error(NotFound, response)
86
89
  rescue NotFound => e
@@ -91,8 +94,7 @@ describe Base do
91
94
  end
92
95
 
93
96
  it "should be correct when body has errors hash" do
94
- errors = '{"errors":["bad_error"]}'
95
- response = HTTParty::Response.new(nil, errors, 400, 'Bad Request', {})
97
+ response = make_response('{"errors":["bad_error"]}', 400)
96
98
  begin
97
99
  e = Base.error(BadRequest, response)
98
100
  rescue BadRequest => e
@@ -103,8 +105,7 @@ describe Base do
103
105
  end
104
106
 
105
107
  it "should be correct when body has hash" do
106
- errors = '{"foo":["bar"]}'
107
- response = HTTParty::Response.new(nil, errors, 400, 'Bad Request', {})
108
+ response = make_response('{"foo":["bar"]}', 400)
108
109
  begin
109
110
  e = Base.error(BadRequest, response)
110
111
  rescue BadRequest => e
@@ -0,0 +1,101 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ include DataCatalog
3
+
4
+ describe BrokenLink do
5
+
6
+ before do
7
+ setup_api
8
+ clean_slate
9
+ @user = User.create(
10
+ :name => "Q. Rater",
11
+ :email => "q_rater@email.com",
12
+ :curator => true
13
+ )
14
+ @source = Source.create(
15
+ :title => "Some FCC Data",
16
+ :url => "http://fcc.gov/somedata.csv",
17
+ :source_type => "dataset"
18
+ )
19
+ DataCatalog.with_key(@user.primary_api_key) do
20
+ @broken_link = BrokenLink.create({
21
+ :source_id => @source.id,
22
+ :field => "url",
23
+ :destination_url => "http://broken.gov/abx834",
24
+ :status => 404,
25
+ })
26
+ end
27
+ end
28
+
29
+ describe ".all" do
30
+ it "should return an enumeration of broken_links" do
31
+ broken_links = BrokenLink.all(:user_id => @user.id)
32
+ broken_links.each do |o|
33
+ o.should be_an_instance_of(BrokenLink)
34
+ end
35
+ end
36
+ end
37
+
38
+ describe ".create" do
39
+ # (successful create is exercised in before block)
40
+
41
+ it "should fail with invalid params" do
42
+ begin
43
+ BrokenLink.create(:mad => "hatter")
44
+ rescue BadRequest => e
45
+ e.errors.should == { "invalid_params" => ["mad"] }
46
+ end
47
+ end
48
+
49
+ it "should fail when missing params" do
50
+ begin
51
+ BrokenLink.create()
52
+ rescue BadRequest => e
53
+ e.errors.should == {
54
+ "destination_url" => ["can't be empty"],
55
+ "field" => ["can't be empty"],
56
+ "status" => ["can't be empty"],
57
+ "base" => ["source_id or organization_id needed"]
58
+ }
59
+ end
60
+ end
61
+ end
62
+
63
+ describe ".destroy" do
64
+ it "should destroy an existing broken_link as an admin" do
65
+ BrokenLink.destroy(@broken_link.id).should be_true
66
+ end
67
+
68
+ it "should destroy an existing broken_link as the user" do
69
+ DataCatalog.with_key(@user.primary_api_key) do
70
+ BrokenLink.destroy(@broken_link.id).should be_true
71
+ end
72
+ end
73
+
74
+ it "should raise NotFound when attempting to destroy non-existing broken_link" do
75
+ executing do
76
+ BrokenLink.destroy(mangle(@broken_link.id))
77
+ end.should raise_error(NotFound)
78
+ end
79
+ end
80
+
81
+ describe ".first" do
82
+ it "should return the first BrokenLink" do
83
+ BrokenLink.first.id.should == @broken_link.id
84
+ end
85
+ end
86
+
87
+ describe ".get" do
88
+ it "should return a broken_link" do
89
+ broken_link = BrokenLink.get(@broken_link.id)
90
+ broken_link.should be_an_instance_of(BrokenLink)
91
+ broken_link.source_id.should == @source.id
92
+ end
93
+
94
+ it "should raise NotFound if no broken_link exists" do
95
+ executing do
96
+ BrokenLink.get(mangle(@broken_link.id))
97
+ end.should raise_error(NotFound)
98
+ end
99
+ end
100
+
101
+ end
data/spec/comment_spec.rb CHANGED
@@ -15,39 +15,6 @@ describe Comment do
15
15
  :source_type => "dataset")
16
16
  end
17
17
 
18
- describe ".create" do
19
- it "should create a new comment when valid params are passed in" do
20
- DataCatalog.with_key(@user.primary_api_key) do
21
- @comment = Comment.create(:source_id => @source.id, :text => "The first comment.")
22
- end
23
- @comment.should be_an_instance_of(Comment)
24
-
25
- refreshed_source = Source.get(@source.id)
26
- refreshed_source.comments.first.text.should == "The first comment."
27
- refreshed_source.comments.first.user.name.should == "Ted Smith"
28
- end
29
- end
30
-
31
- describe ".get" do
32
- before do
33
- DataCatalog.with_key(@user.primary_api_key) do
34
- @comment = Comment.create(:source_id => @source.id, :text => "The first comment.")
35
- end
36
- end
37
-
38
- it "should return a comment" do
39
- comment = Comment.get(@comment.id)
40
- comment.should be_an_instance_of(Comment)
41
- comment.text.should == "The first comment."
42
- end
43
-
44
- it "should raise NotFound if no comment exists" do
45
- executing do
46
- Comment.get(mangle(@comment.id))
47
- end.should raise_error(NotFound)
48
- end
49
- end
50
-
51
18
  describe ".all" do
52
19
  before do
53
20
  @john = User.create(
@@ -72,20 +39,15 @@ describe Comment do
72
39
  end
73
40
  end
74
41
 
75
- describe ".update" do
76
- before do
42
+ describe ".create" do
43
+ it "should create a new comment when valid params are passed in" do
77
44
  DataCatalog.with_key(@user.primary_api_key) do
78
45
  @comment = Comment.create(:source_id => @source.id, :text => "The first comment.")
79
46
  end
80
- end
81
-
82
- it "should update an existing comment with valid params" do
83
- DataCatalog.with_key(@user.primary_api_key) do
84
- Comment.update(@comment.id, :text => "The first comment, updated.")
85
- end
47
+ @comment.should be_an_instance_of(Comment)
86
48
 
87
49
  refreshed_source = Source.get(@source.id)
88
- refreshed_source.comments.first.text.should == "The first comment, updated."
50
+ refreshed_source.comments.first.text.should == "The first comment."
89
51
  refreshed_source.comments.first.user.name.should == "Ted Smith"
90
52
  end
91
53
  end
@@ -114,4 +76,42 @@ describe Comment do
114
76
  end
115
77
  end
116
78
 
79
+ describe ".get" do
80
+ before do
81
+ DataCatalog.with_key(@user.primary_api_key) do
82
+ @comment = Comment.create(:source_id => @source.id, :text => "The first comment.")
83
+ end
84
+ end
85
+
86
+ it "should return a comment" do
87
+ comment = Comment.get(@comment.id)
88
+ comment.should be_an_instance_of(Comment)
89
+ comment.text.should == "The first comment."
90
+ end
91
+
92
+ it "should raise NotFound if no comment exists" do
93
+ executing do
94
+ Comment.get(mangle(@comment.id))
95
+ end.should raise_error(NotFound)
96
+ end
97
+ end
98
+
99
+ describe ".update" do
100
+ before do
101
+ DataCatalog.with_key(@user.primary_api_key) do
102
+ @comment = Comment.create(:source_id => @source.id, :text => "The first comment.")
103
+ end
104
+ end
105
+
106
+ it "should update an existing comment with valid params" do
107
+ DataCatalog.with_key(@user.primary_api_key) do
108
+ Comment.update(@comment.id, :text => "The first comment, updated.")
109
+ end
110
+
111
+ refreshed_source = Source.get(@source.id)
112
+ refreshed_source.comments.first.text.should == "The first comment, updated."
113
+ refreshed_source.comments.first.user.name.should == "Ted Smith"
114
+ end
115
+ end
116
+
117
117
  end
@@ -23,19 +23,43 @@ describe Document do
23
23
  end
24
24
  end
25
25
 
26
- describe ".create" do
26
+ describe ".all" do
27
+ it "should return an enumeration of documents" do
28
+ documents = Document.all(:source_id => @source.id)
29
+ documents.each do |o|
30
+ o.should be_an_instance_of(Document)
31
+ end
32
+ end
33
+ end
27
34
 
35
+ describe ".create" do
28
36
  it "should create a document when valid params are passed in" do
29
37
  DataCatalog.with_key(@user.primary_api_key) do
30
38
  refreshed_source = Source.get(@source.id)
31
39
  refreshed_source.documents.first.text.should == "This is community documentation."
32
40
  end
33
41
  end
42
+ end
34
43
 
35
- end # describe ".create"
44
+ describe ".destroy" do
45
+ it "should destroy an existing document as an admin" do
46
+ Document.destroy(@document.id).should be_true
47
+ end
36
48
 
37
- describe ".get" do
49
+ it "should not destroy an existing document as the user" do
50
+ DataCatalog.with_key(@user.primary_api_key) do
51
+ executing { Document.destroy(@document.id) }.should raise_error(Unauthorized)
52
+ end
53
+ end
54
+
55
+ it "should raise NotFound when attempting to destroy non-existing document" do
56
+ executing do
57
+ Document.destroy(mangle(@document.id))
58
+ end.should raise_error(NotFound)
59
+ end
60
+ end
38
61
 
62
+ describe ".get" do
39
63
  it "should return a document" do
40
64
  document = Document.get(@document.id)
41
65
  document.should be_an_instance_of(Document)
@@ -47,22 +71,9 @@ describe Document do
47
71
  Document.get(mangle(@document.id))
48
72
  end.should raise_error(NotFound)
49
73
  end
50
-
51
- end # describe ".get"
52
-
53
- describe ".all" do
54
-
55
- it "should return an enumeration of documents" do
56
- documents = Document.all(:source_id => @source.id)
57
- documents.each do |o|
58
- o.should be_an_instance_of(Document)
59
- end
60
- end
61
-
62
- end # describe ".all"
74
+ end
63
75
 
64
76
  describe ".update" do
65
-
66
77
  it "should update an existing document with valid params" do
67
78
  @document.previous_id.should be nil
68
79
  DataCatalog.with_key(@user.primary_api_key) do
@@ -73,27 +84,6 @@ describe Document do
73
84
  refreshed_document.text.should == "This is the updated document."
74
85
  refreshed_document.previous_id.should_not be nil
75
86
  end
87
+ end
76
88
 
77
- end # describe ".update"
78
-
79
- describe ".destroy" do
80
-
81
- it "should destroy an existing document as an admin" do
82
- Document.destroy(@document.id).should be_true
83
- end
84
-
85
- it "should not destroy an existing document as the user" do
86
- DataCatalog.with_key(@user.primary_api_key) do
87
- executing { Document.destroy(@document.id) }.should raise_error(Unauthorized)
88
- end
89
- end
90
-
91
- it "should raise NotFound when attempting to destroy non-existing document" do
92
- executing do
93
- Document.destroy(mangle(@document.id))
94
- end.should raise_error(NotFound)
95
- end
96
-
97
- end # describe ".destroy"
98
-
99
- end
89
+ end