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.
- data/.gitignore +4 -4
- data/CHANGES.md +2 -2
- data/LICENSE.md +11 -11
- data/README.md +34 -31
- data/Rakefile +51 -49
- data/VERSION +1 -1
- data/datacatalog.gemspec +29 -13
- data/lib/base.rb +77 -0
- data/lib/datacatalog.rb +8 -28
- data/lib/main.rb +41 -0
- data/lib/resources/about.rb +11 -0
- data/lib/resources/api_key.rb +38 -0
- data/lib/resources/source.rb +31 -0
- data/lib/resources/user.rb +73 -0
- data/sandbox_api.yml.example +2 -2
- data/spec/about_spec.rb +21 -0
- data/spec/api_key_spec.rb +145 -1
- data/spec/base_spec.rb +129 -184
- data/spec/datacatalog_spec.rb +36 -0
- data/spec/source_spec.rb +162 -101
- data/spec/spec.opts +4 -4
- data/spec/spec_helper.rb +48 -48
- data/spec/user_spec.rb +278 -208
- metadata +54 -11
- data/doc/api_key_spec_for_rest_api.txt +0 -54
- data/doc/api_key_spec_for_ruby_api.rb +0 -26
- data/doc/mocking_options.md +0 -20
- data/lib/datacatalog/api_key.rb +0 -7
- data/lib/datacatalog/base.rb +0 -68
- data/lib/datacatalog/source.rb +0 -30
- data/lib/datacatalog/user.rb +0 -94
@@ -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
|
data/sandbox_api.yml.example
CHANGED
@@ -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
|
data/spec/about_spec.rb
ADDED
@@ -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
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
DataCatalog.base_uri = '
|
14
|
-
DataCatalog.base_uri.should == '
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
it "should
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
it "should return nil
|
119
|
-
|
120
|
-
|
121
|
-
end
|
122
|
-
|
123
|
-
it "should
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
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
|