datacatalog 0.1.0 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ module DataCatalog
2
+
3
+ class About < Base
4
+
5
+ def self.get()
6
+ one(http_get("/"))
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,38 @@
1
+ module DataCatalog
2
+
3
+ class ApiKey < Base
4
+
5
+ def self.all(user_id, conditions={})
6
+ many(http_get(uri(user_id), :query => conditions))
7
+ end
8
+
9
+ def self.create(user_id, params={})
10
+ one(http_post(uri(user_id), :query => params))
11
+ end
12
+
13
+ def self.destroy(user_id, id)
14
+ one(http_delete(uri(user_id, id)))
15
+ end
16
+
17
+ def self.first(user_id, conditions={})
18
+ one(http_get(uri(user_id), :query => conditions).first)
19
+ end
20
+
21
+ def self.get(user_id, id)
22
+ one(http_get(uri(user_id, id)))
23
+ end
24
+
25
+ def self.update(user_id, id, params={})
26
+ one(http_put(uri(user_id, id), :query => params))
27
+ end
28
+
29
+ # == Helpers
30
+
31
+ def self.uri(user_id, id = nil)
32
+ raise Error, "user_id cannot be blank" if user_id.blank?
33
+ "/users/#{user_id}/keys/#{id}"
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,31 @@
1
+ module DataCatalog
2
+
3
+ class Source < Base
4
+
5
+ def self.all(conditions={})
6
+ many(http_get("/sources", :query => conditions))
7
+ end
8
+
9
+ def self.create(params={})
10
+ one(http_post("/sources", :query => params))
11
+ end
12
+
13
+ def self.destroy(source_id)
14
+ one(http_delete("/sources/#{source_id}"))
15
+ end
16
+
17
+ def self.first(conditions={})
18
+ one(http_get("/sources", :query => conditions).first)
19
+ end
20
+
21
+ def self.get(id)
22
+ one(http_get("/sources/#{id}"))
23
+ end
24
+
25
+ def self.update(source_id, params={})
26
+ one(http_put("/sources/#{source_id}", :query => params))
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,73 @@
1
+ module DataCatalog
2
+
3
+ class User < Base
4
+
5
+ def self.all(conditions={})
6
+ many(http_get("/users", :query => conditions))
7
+ end
8
+
9
+ def self.create(params={})
10
+ with_api_keys(one(http_post("/users", :query => params)))
11
+ end
12
+
13
+ def self.destroy(user_id)
14
+ one(http_delete("/users/#{user_id}"))
15
+ end
16
+
17
+ def self.first(conditions={})
18
+ one(http_get("/users", :query => conditions).first)
19
+ end
20
+
21
+ def self.get(id)
22
+ with_api_keys(one(http_get("/users/#{id}")))
23
+ end
24
+
25
+ def self.get_by_api_key(api_key)
26
+ DataCatalog.with_key(api_key) do
27
+ checkup = one(http_get("/checkup"))
28
+ raise NotFound unless checkup.valid_api_key
29
+ get(checkup.user.id)
30
+ end
31
+ end
32
+
33
+ def self.update(user_id, params)
34
+ one(http_put("/users/#{user_id}", :query => params))
35
+ end
36
+
37
+ # == Helpers
38
+
39
+ def self.with_api_keys(user)
40
+ user.api_keys = ApiKey.all(user.id) if user
41
+ user
42
+ end
43
+
44
+ # == Instance Methods
45
+
46
+ def delete_api_key!(api_key_id)
47
+ ApiKey.destroy(self.id, api_key_id)
48
+ update_api_keys
49
+ end
50
+
51
+ def generate_api_key!(params)
52
+ ApiKey.create(self.id, params)
53
+ update_api_keys
54
+ end
55
+
56
+ def update_api_key!(api_key_id, params)
57
+ ApiKey.update(self.id, api_key_id, params)
58
+ update_api_keys
59
+ end
60
+
61
+ protected
62
+
63
+ def update_api_keys
64
+ self.api_keys = ApiKey.all(self.id)
65
+ user = User.get(id)
66
+ self.application_api_keys = user.application_api_keys
67
+ self.valet_api_keys = user.valet_api_keys
68
+ true
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -1,2 +1,2 @@
1
- api_key: d193d1523d49e4caf46297ef94d7a2c995d3b2cb # generate with rake db:ensure_admin on datacatalog-api
2
- base_uri: sandbox.dc-api.local
1
+ api_key: d193d1523d49e4caf46297ef94d7a2c995d3b2cb # generate with rake db:ensure_admin on datacatalog-api
2
+ base_uri: sandbox.dc-api.local
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ include DataCatalog
3
+
4
+ describe DataCatalog do
5
+
6
+ describe ".about" do
7
+ before do
8
+ setup_api
9
+ end
10
+
11
+ it "should return information about the API" do
12
+ about = About.get
13
+ about.should be_an_instance_of(About)
14
+ about.name.should == "National Data Catalog API"
15
+ about.project_page.should == {
16
+ "href" => "http://sunlightlabs.com/projects/datacatalog/"
17
+ }
18
+ end
19
+ end
20
+
21
+ end
data/spec/api_key_spec.rb CHANGED
@@ -1 +1,145 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ include DataCatalog
3
+
4
+ module ApiKeyHelpers
5
+ def create_user
6
+ User.create({
7
+ :name => "Ted Smith",
8
+ :email => "ted@email.com"
9
+ })
10
+ end
11
+
12
+ def create_user_with_2_keys
13
+ user = create_user
14
+ ApiKey.create(user.id, {
15
+ :purpose => "Civic hacking",
16
+ :key_type => "application"
17
+ })
18
+ User.get(user.id)
19
+ end
20
+ end
21
+
22
+ describe ApiKey do
23
+ include ApiKeyHelpers
24
+
25
+ before do
26
+ setup_api
27
+ clean_slate
28
+ end
29
+
30
+ describe ".all" do
31
+ before do
32
+ @user = create_user_with_2_keys
33
+ @api_keys = ApiKey.all(@user.id)
34
+ end
35
+
36
+ it "should return an enumeration of API keys" do
37
+ @api_keys.each do |api_key|
38
+ api_key.should be_an_instance_of(ApiKey)
39
+ end
40
+ end
41
+
42
+ it "should return correct titles" do
43
+ @api_keys.map(&:key_type).should == %w(primary application)
44
+ end
45
+ end
46
+
47
+ describe ".all with conditions" do
48
+ before do
49
+ @user = create_user_with_2_keys
50
+ @api_keys = ApiKey.all(@user.id, :key_type => "application")
51
+ end
52
+
53
+ it "should return an enumeration of API keys" do
54
+ @api_keys.each do |api_key|
55
+ api_key.should be_an_instance_of(ApiKey)
56
+ end
57
+ end
58
+
59
+ it "should return correct titles" do
60
+ @api_keys.map(&:key_type).should == %w(application)
61
+ end
62
+ end
63
+
64
+ describe ".create" do
65
+ it "should create a new API key from basic params" do
66
+ user = create_user_with_2_keys
67
+ api_key = ApiKey.create(user.id, {
68
+ :purpose => "Data wrangling",
69
+ :key_type => "valet"
70
+ })
71
+ api_key.should be_an_instance_of(ApiKey)
72
+ api_key.key_type.should == "valet"
73
+ end
74
+ end
75
+
76
+ describe ".first" do
77
+ before do
78
+ @user = create_user_with_2_keys
79
+ end
80
+
81
+ it "should return an API key" do
82
+ api_key = ApiKey.first(@user.id, :purpose => "Civic hacking")
83
+ api_key.should be_an_instance_of(ApiKey)
84
+ api_key.purpose.should == "Civic hacking"
85
+ end
86
+
87
+ it "should return nil if nothing found" do
88
+ api_key = ApiKey.first(@user.id, :purpose => "Evil")
89
+ api_key.should be_nil
90
+ end
91
+ end
92
+
93
+ describe ".get" do
94
+ before do
95
+ @user = create_user_with_2_keys
96
+ @api_key = @user.api_keys[1]
97
+ end
98
+
99
+ it "should return an API key" do
100
+ api_key = ApiKey.get(@user.id, @api_key.id)
101
+ api_key.should be_an_instance_of(ApiKey)
102
+ api_key.purpose.should == "Civic hacking"
103
+ end
104
+
105
+ it "should raise NotFound if no API key exists" do
106
+ executing do
107
+ ApiKey.get(@user.id, mangle(@api_key.id))
108
+ end.should raise_error(NotFound)
109
+ end
110
+ end
111
+
112
+ describe ".update" do
113
+ before do
114
+ @user = create_user_with_2_keys
115
+ @api_key = @user.api_keys[1]
116
+ end
117
+
118
+ it "should update an existing source from valid params" do
119
+ api_key = ApiKey.update(@user.id, @api_key.id, {
120
+ :purpose => "Local Government Reporting"
121
+ })
122
+ api_key.should be_an_instance_of(ApiKey)
123
+ api_key.purpose.should == "Local Government Reporting"
124
+ end
125
+ end
126
+
127
+ describe ".destroy" do
128
+ before do
129
+ @user = create_user_with_2_keys
130
+ @api_key = @user.api_keys[1]
131
+ end
132
+
133
+ it "should destroy an existing source" do
134
+ result = ApiKey.destroy(@user.id, @api_key.id)
135
+ result.should be_true
136
+ end
137
+
138
+ it "should raise NotFound if API key does not exist" do
139
+ executing do
140
+ ApiKey.destroy(@user.id, mangle(@api_key.id))
141
+ end.should raise_error(NotFound)
142
+ end
143
+ end
144
+
145
+ end
data/spec/base_spec.rb CHANGED
@@ -1,184 +1,129 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
- describe DataCatalog do
4
-
5
- describe "module accessors" do
6
-
7
- it "should access the API Key" do
8
- DataCatalog.api_key = 'flurfeneugen'
9
- DataCatalog.api_key.should == 'flurfeneugen'
10
- end
11
-
12
- it "should access the base URI" do
13
- DataCatalog.base_uri = 'somehost.com'
14
- DataCatalog.base_uri.should == 'somehost.com'
15
- end
16
-
17
- end # describe "accessors"
18
-
19
- describe ".with_key" do
20
-
21
- it "should set the API key within the block" do
22
- DataCatalog.api_key = 'flurfeneugen'
23
- temp_value = 'not_temp_key'
24
-
25
- DataCatalog.with_key('temp_key') do
26
- temp_value = DataCatalog.api_key
27
- end
28
-
29
- temp_value.should == 'temp_key'
30
- DataCatalog.api_key.should == 'flurfeneugen'
31
- end
32
-
33
- end # describe ".with_key"
34
-
35
- end # describe DataCatalog
36
-
37
- describe DataCatalog::Base do
38
-
39
- before(:each) { setup_api }
40
-
41
- describe ".set_base_uri" do
42
-
43
- it "should set and normalize the base URI" do
44
- DataCatalog.base_uri = 'notherhost.com'
45
- DataCatalog::Base.set_base_uri
46
- DataCatalog::Base.default_options[:base_uri].should == 'http://notherhost.com'
47
- end
48
-
49
- it "should set the base URI to the default if it's not explicitly defined" do
50
- DataCatalog.base_uri = nil
51
- DataCatalog::Base.set_base_uri
52
- DataCatalog::Base.default_options[:base_uri].should == 'http://api.nationaldatacatalog.com'
53
- end
54
-
55
- end # describe ".set_base_uri"
56
-
57
- describe ".set_api_key" do
58
-
59
- it "should set the API key" do
60
- DataCatalog.api_key = 'flurfeneugen'
61
- DataCatalog::Base.set_api_key
62
- DataCatalog::Base.default_options[:default_params].should include(:api_key => 'flurfeneugen')
63
- end
64
-
65
- it "should raise exception when attempting to set the API key but none is set" do
66
- DataCatalog.api_key = nil
67
- executing do
68
- DataCatalog::Base.set_api_key
69
- end.should raise_error(DataCatalog::ApiKeyNotConfigured)
70
- end
71
-
72
- end # describe ".set_default_params"
73
-
74
- describe ".set_up!" do
75
-
76
- it "should set both the base URI and API key" do
77
- DataCatalog.base_uri = 'somehost.com'
78
- DataCatalog.api_key = 'flurfeneugen'
79
- DataCatalog::Base.set_up!
80
- DataCatalog::Base.default_options[:base_uri].should == 'http://somehost.com'
81
- DataCatalog::Base.default_options[:default_params].should include(:api_key => 'flurfeneugen')
82
- end
83
-
84
- end # describe ".set_up!"
85
-
86
- describe ".build_object" do
87
-
88
- it "should create an object when a filled hash is passed in" do
89
- base_object = DataCatalog::Base.build_object(:name => "John Smith", :email => "john@email.com")
90
- base_object.should be_an_instance_of(DataCatalog::Base)
91
- base_object.email.should == "john@email.com"
92
- end
93
-
94
- it "should return nil when an empty hash is passed in" do
95
- base_object = DataCatalog::Base.build_object({})
96
- base_object.should be_nil
97
- end
98
-
99
- it "should return nil when nil is passed in" do
100
- base_object = DataCatalog::Base.build_object(nil)
101
- base_object.should be_nil
102
- end
103
-
104
- end # describe ".build_object!"
105
-
106
- describe ".about" do
107
-
108
- it "should return information about the API" do
109
- base_object = DataCatalog::Base.about
110
- base_object.should be_an_instance_of(DataCatalog::Base)
111
- base_object.name.should == "National Data Catalog API"
112
- end
113
-
114
- end # describe ".about"
115
-
116
- describe ".check_status_code" do
117
-
118
- it "should return nil on 200 OK" do
119
- response = HTTParty::Response.new(nil, '{"foo":"bar"}', 200, 'OK', {})
120
- DataCatalog::Base.check_status_code(response).should be_nil
121
- end
122
-
123
- it "should raise BadRequest on 400 Bad Request" do
124
- response = HTTParty::Response.new(nil, '[]', 400, 'Bad Request', {})
125
- executing do
126
- DataCatalog::Base.check_status_code(response)
127
- end.should raise_error(DataCatalog::BadRequest)
128
- end
129
-
130
- it "should raise Unauthorized on 401 Unauthorized" do
131
- response = HTTParty::Response.new(nil, '', 401, 'Unauthorized', {})
132
- executing do
133
- DataCatalog::Base.check_status_code(response)
134
- end.should raise_error(DataCatalog::Unauthorized)
135
- end
136
-
137
- it "should raise NotFound on 404 Not Found" do
138
- response = HTTParty::Response.new(nil, '[]', 404, 'Not Found', {})
139
- executing do
140
- DataCatalog::Base.check_status_code(response)
141
- end.should raise_error(DataCatalog::NotFound)
142
- end
143
-
144
- it "should raise InternalServerError on 500 Internal Server Error" do
145
- response = HTTParty::Response.new(nil, '', 500, 'Internal Server Error', {})
146
- executing do
147
- DataCatalog::Base.check_status_code(response)
148
- end.should raise_error(DataCatalog::InternalServerError)
149
- end
150
-
151
- end # describe ".check_status_code"
152
-
153
- describe ".error_message" do
154
-
155
- it "should return an 'Unable to parse:' message when body is blank" do
156
- response = HTTParty::Response.new(nil, '', 404, 'Not Found', {})
157
- DataCatalog::Base.error_message(response).should == %(Unable to parse: "")
158
- end
159
-
160
- it "should return 'Response was empty' when body is an empty JSON object" do
161
- response = HTTParty::Response.new(nil, '{}', 404, 'Not Found', {})
162
- DataCatalog::Base.error_message(response).should == "Response was empty"
163
- end
164
-
165
- it "should return 'Response was empty' when body is an empty array" do
166
- response = HTTParty::Response.new(nil, '[]', 404, 'Not Found', {})
167
- DataCatalog::Base.error_message(response).should == "Response was empty"
168
- end
169
-
170
- it "should return the contents of the errors hash when it exists" do
171
- errors = '{"errors":["bad_error"]}'
172
- response = HTTParty::Response.new(nil, errors, 400, 'Bad Request', {})
173
- DataCatalog::Base.error_message(response).should == '["bad_error"]'
174
- end
175
-
176
- it "should return the contents of the response body when no errors hash exists" do
177
- errors = '{"foo":["bar"]}'
178
- response = HTTParty::Response.new(nil, errors, 400, 'Bad Request', {})
179
- DataCatalog::Base.error_message(response).should == '{"foo":["bar"]}'
180
- end
181
-
182
- end # describe ".error_message"
183
-
184
- end # describe DataCatalog::Base
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ include DataCatalog
3
+
4
+ describe Base do
5
+
6
+ before do
7
+ setup_api
8
+ end
9
+
10
+ describe ".base_uri=" do
11
+ it "should set and normalize the base URI" do
12
+ setup_api
13
+ DataCatalog.base_uri = 'host.com'
14
+ DataCatalog.base_uri.should == 'http://host.com'
15
+ end
16
+
17
+ it "should set the base URI to the default if it's not explicitly defined" do
18
+ DataCatalog.base_uri = ''
19
+ DataCatalog.base_uri.should == 'http://api.nationaldatacatalog.com'
20
+ Base.base_uri.should == 'http://api.nationaldatacatalog.com'
21
+ end
22
+ end
23
+
24
+ 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
28
+ end
29
+
30
+ it "should raise BadRequest on 400 Bad Request" do
31
+ response = HTTParty::Response.new(nil, '[]', 400, 'Bad Request', {})
32
+ executing do
33
+ Base.check_status(response)
34
+ end.should raise_error(BadRequest)
35
+ end
36
+
37
+ it "should raise Unauthorized on 401 Unauthorized" do
38
+ response = HTTParty::Response.new(nil, '', 401, 'Unauthorized', {})
39
+ executing do
40
+ Base.check_status(response)
41
+ end.should raise_error(Unauthorized)
42
+ end
43
+
44
+ it "should raise NotFound on 404 Not Found" do
45
+ response = HTTParty::Response.new(nil, '[]', 404, 'Not Found', {})
46
+ executing do
47
+ Base.check_status(response)
48
+ end.should raise_error(NotFound)
49
+ end
50
+
51
+ it "should raise InternalServerError on 500 Internal Server Error" do
52
+ response = HTTParty::Response.new(nil, '', 500, 'Internal Server Error', {})
53
+ executing do
54
+ Base.check_status(response)
55
+ end.should raise_error(InternalServerError)
56
+ end
57
+ end
58
+
59
+ describe ".error" do
60
+ it "should return an 'Unable to parse:' message when body is blank" do
61
+ response = HTTParty::Response.new(nil, '', 404, 'Not Found', {})
62
+ Base.error(response).should == %(Unable to parse: "")
63
+ end
64
+
65
+ it "should return 'Response was empty' when body is an empty JSON object" do
66
+ response = HTTParty::Response.new(nil, '{}', 404, 'Not Found', {})
67
+ Base.error(response).should == "Response was empty"
68
+ end
69
+
70
+ it "should return 'Response was empty' when body is an empty array" do
71
+ response = HTTParty::Response.new(nil, '[]', 404, 'Not Found', {})
72
+ Base.error(response).should == "Response was empty"
73
+ end
74
+
75
+ it "should return the contents of the errors hash when it exists" do
76
+ errors = '{"errors":["bad_error"]}'
77
+ response = HTTParty::Response.new(nil, errors, 400, 'Bad Request', {})
78
+ Base.error(response).should == '["bad_error"]'
79
+ end
80
+
81
+ it "should return the contents of the response body when no errors hash exists" do
82
+ errors = '{"foo":["bar"]}'
83
+ response = HTTParty::Response.new(nil, errors, 400, 'Bad Request', {})
84
+ Base.error(response).should == '{"foo":["bar"]}'
85
+ end
86
+ end
87
+
88
+ describe ".many" do
89
+ it "should create an object from a filled array" do
90
+ array = Base.many([
91
+ {
92
+ :name => "Carl Malamud",
93
+ :email => "no-spam-carl@media.org"
94
+ },
95
+ {
96
+ :name => "Ellen Miller",
97
+ :email => "no-spam-ellen@sunlightfoundation.com"
98
+ }
99
+ ])
100
+ array.map do |item|
101
+ item.should be_an_instance_of(Base)
102
+ end
103
+ array.map(&:name).should == ["Carl Malamud", "Ellen Miller"]
104
+ end
105
+ end
106
+
107
+ describe ".one" do
108
+ it "should create an object from a filled hash" do
109
+ hash = Base.one({
110
+ :name => "John Smith",
111
+ :email => "john@email.com"
112
+ })
113
+ hash.should be_an_instance_of(Base)
114
+ hash.name.should == "John Smith"
115
+ hash.email.should == "john@email.com"
116
+ end
117
+
118
+ it "should return nil from an empty hash" do
119
+ hash = Base.one({})
120
+ hash.should be_nil
121
+ end
122
+
123
+ it "should return nil from nil" do
124
+ hash = Base.one(nil)
125
+ hash.should be_nil
126
+ end
127
+ end
128
+
129
+ end