lazy_resource 0.1.0
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 +18 -0
- data/.rspec +3 -0
- data/.rvmrc +1 -0
- data/Gemfile +14 -0
- data/Guardfile +5 -0
- data/LICENSE +22 -0
- data/README.md +127 -0
- data/Rakefile +6 -0
- data/examples/github.rb +64 -0
- data/lazy_resource.gemspec +22 -0
- data/lib/lazy_resource.rb +39 -0
- data/lib/lazy_resource/attributes.rb +131 -0
- data/lib/lazy_resource/errors.rb +72 -0
- data/lib/lazy_resource/http_mock.rb +23 -0
- data/lib/lazy_resource/mapping.rb +73 -0
- data/lib/lazy_resource/relation.rb +132 -0
- data/lib/lazy_resource/request.rb +63 -0
- data/lib/lazy_resource/resource.rb +197 -0
- data/lib/lazy_resource/resource_queue.rb +40 -0
- data/lib/lazy_resource/types.rb +53 -0
- data/lib/lazy_resource/url_generation.rb +98 -0
- data/lib/lazy_resource/version.rb +3 -0
- data/spec/fixtures/comment.rb +6 -0
- data/spec/fixtures/post.rb +6 -0
- data/spec/fixtures/user.rb +9 -0
- data/spec/lazy_resource/attributes_spec.rb +157 -0
- data/spec/lazy_resource/errors_spec.rb +48 -0
- data/spec/lazy_resource/mapping_spec.rb +237 -0
- data/spec/lazy_resource/relation_spec.rb +169 -0
- data/spec/lazy_resource/request_spec.rb +143 -0
- data/spec/lazy_resource/resource_queue_spec.rb +66 -0
- data/spec/lazy_resource/resource_spec.rb +463 -0
- data/spec/lazy_resource/types_spec.rb +109 -0
- data/spec/lazy_resource/url_generation_spec.rb +155 -0
- data/spec/spec_helper.rb +25 -0
- metadata +165 -0
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
include LazyResource::Types
|
4
|
+
|
5
|
+
describe LazyResource::Types do
|
6
|
+
describe LazyResource::Types::Array do
|
7
|
+
it 'adds a parse method' do
|
8
|
+
LazyResource::Types::Array.should respond_to(:parse)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '.parse' do
|
12
|
+
it 'calls to_a on the passed object' do
|
13
|
+
array = []
|
14
|
+
array.should_receive(:to_a)
|
15
|
+
LazyResource::Types::Array.parse(array)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe LazyResource::Types::String do
|
21
|
+
it 'adds a parse method' do
|
22
|
+
LazyResource::Types::String.should respond_to(:parse)
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '.parse' do
|
26
|
+
it 'calls to_s on the passed object' do
|
27
|
+
string = ''
|
28
|
+
string.should_receive(:to_s)
|
29
|
+
LazyResource::Types::String.parse(string)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe LazyResource::Types::Hash do
|
35
|
+
it 'adds a parse method' do
|
36
|
+
LazyResource::Types::Hash.should respond_to(:parse)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'returns the passed object' do
|
40
|
+
hash = {}
|
41
|
+
LazyResource::Types::Hash.parse(hash).should == hash
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe LazyResource::Types::Boolean do
|
46
|
+
it 'adds a parse method' do
|
47
|
+
LazyResource::Types::Boolean.should respond_to(:parse)
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '.parse' do
|
51
|
+
it 'returns true if passed 1, true, \'true\'' do
|
52
|
+
['1', true, 'true'].each do |v|
|
53
|
+
LazyResource::Types::Boolean.parse(v).should == true
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'returns false otherwise' do
|
58
|
+
LazyResource::Types::Boolean.parse('false').should == false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe LazyResource::Types::Float do
|
64
|
+
it 'adds a parse method' do
|
65
|
+
LazyResource::Types::Float.should respond_to(:parse)
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '.parse' do
|
69
|
+
it 'calls to_f on the passed object' do
|
70
|
+
float = '1.0'
|
71
|
+
float.should_receive(:to_f)
|
72
|
+
LazyResource::Types::Float.parse(float)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe LazyResource::Types::Fixnum do
|
78
|
+
it 'adds a parse method' do
|
79
|
+
LazyResource::Types::Fixnum.should respond_to(:parse)
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '.parse' do
|
83
|
+
it 'calls to_i on the passed object' do
|
84
|
+
int = '1'
|
85
|
+
int.should_receive(:to_i)
|
86
|
+
LazyResource::Types::Fixnum.parse(int)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe LazyResource::Types::DateTime do
|
92
|
+
it 'adds a parse method' do
|
93
|
+
LazyResource::Types::DateTime.should respond_to(:parse)
|
94
|
+
end
|
95
|
+
|
96
|
+
describe '.parse' do
|
97
|
+
it 'returns the passed object if it is already a DateTime' do
|
98
|
+
date = DateTime.parse('01-01-2012 00:00:00 -0500')
|
99
|
+
LazyResource::Types::DateTime.parse(date).should == date
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'calls DateTime.parse otherwise' do
|
103
|
+
date_string = '01-01-2012 00:00:00 -0500'
|
104
|
+
::DateTime.should_receive(:parse).with(date_string)
|
105
|
+
LazyResource::Types::DateTime.parse(date_string)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Item
|
4
|
+
include LazyResource::UrlGeneration
|
5
|
+
|
6
|
+
attr_accessor :attributes
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def collection_name
|
10
|
+
'items'
|
11
|
+
end
|
12
|
+
|
13
|
+
def site
|
14
|
+
'http://example.com/'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
@attributes = {}
|
20
|
+
end
|
21
|
+
|
22
|
+
def primary_key
|
23
|
+
1
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe LazyResource::UrlGeneration do
|
28
|
+
describe '#element_path' do
|
29
|
+
it 'calls the class element_path' do
|
30
|
+
item = Item.new
|
31
|
+
Item.should_receive(:element_path).with(1, nil)
|
32
|
+
item.element_path
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#new_element_path' do
|
37
|
+
it 'calls the class element_path' do
|
38
|
+
item = Item.new
|
39
|
+
Item.should_receive(:new_element_path)
|
40
|
+
item.new_element_path
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#collection_path' do
|
45
|
+
it 'calls the class element_path' do
|
46
|
+
item = Item.new
|
47
|
+
Item.should_receive(:collection_path)
|
48
|
+
item.collection_path
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe '#element_url' do
|
53
|
+
it 'builds a full URL for the element' do
|
54
|
+
item = Item.new
|
55
|
+
item.element_url.should == 'http://example.com/items/1'
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#collection_url' do
|
60
|
+
it 'builds a full URL for the collection' do
|
61
|
+
item = Item.new
|
62
|
+
item.collection_url.should == 'http://example.com/items'
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe '#split_options' do
|
67
|
+
it 'calls the class element_path' do
|
68
|
+
item = Item.new
|
69
|
+
Item.should_receive(:split_options).with({})
|
70
|
+
item.split_options({})
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe '.prefix' do
|
75
|
+
it 'returns / if no options are passed' do
|
76
|
+
Item.prefix({}).should == '/'
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'generates nested routes' do
|
80
|
+
Item.prefix({ :user_id => 1, :post_id => 2 }).should == '/users/1/posts/2/'
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '.element_path' do
|
85
|
+
it 'generates a single resource\'s path' do
|
86
|
+
Item.element_path(1).should == '/items/1'
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'prepends any parent resources' do
|
90
|
+
Item.element_path(1, { :comment_id => 1, :post_id => 2 }).should == '/comments/1/posts/2/items/1'
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'splits the prefix options (parent resources) from the parameters' do
|
94
|
+
Item.element_path(1, { :comment_id => 1, :name => 'Andrew' }).should == '/comments/1/items/1?name=Andrew'
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'puts parent resources in the query params if passed in the query_options hash' do
|
98
|
+
Item.element_path(1, {}, { :comment_id => 1 }).should == '/items/1?comment_id=1'
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'uses the "from" parameter over the collection name if passed' do
|
102
|
+
Item.element_path(1, {}, {}, 'comments').should == '/comments/1'
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe '.new_element_path' do
|
107
|
+
it 'generates a path for a new element' do
|
108
|
+
Item.new_element_path.should == '/items/new'
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'prepends any parent resources' do
|
112
|
+
Item.new_element_path(:comment_id => 1, :post_id => 2).should == '/comments/1/posts/2/items/new'
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'uses from over the collection name if passed' do
|
116
|
+
Item.new_element_path({}, 'comments').should == '/comments/new'
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe '.collection_path' do
|
121
|
+
it 'generates a collection\'s path' do
|
122
|
+
Item.collection_path.should == '/items'
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'prepends any parent resources' do
|
126
|
+
Item.collection_path(:comment_id => 1, :post_id => 2).should == '/comments/1/posts/2/items'
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'splits the prefix options (parent resources) from the parameters' do
|
130
|
+
Item.collection_path(:comment_id => 1, :name => 'Andrew').should == '/comments/1/items?name=Andrew'
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'puts parent resources in the params if passed in the query_options hash' do
|
134
|
+
Item.collection_path({}, { :comment_id => 1 }).should == '/items?comment_id=1'
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'uses the "from" parameter over the collection name if passed' do
|
138
|
+
Item.collection_path({}, {}, 'comments').should == '/comments'
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe '.query_string' do
|
143
|
+
it 'creates a query string' do
|
144
|
+
Item.query_string({ :name => 'Andrew', :comment_id => 1, :query => 'this is a query' }).should == '?comment_id=1&name=Andrew&query=this+is+a+query'
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
describe '.split_options' do
|
149
|
+
it 'splits prefix options from query options' do
|
150
|
+
prefix_options, query_options = Item.split_options(:name => 'Andrew', :comment_id => 1)
|
151
|
+
prefix_options.should == { :comment_id => 1 }
|
152
|
+
query_options.should == { :name => 'Andrew' }
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'simplecov'
|
5
|
+
SimpleCov.start do
|
6
|
+
add_filter '/spec/'
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'lazy_resource'
|
10
|
+
|
11
|
+
require 'fixtures/comment'
|
12
|
+
require 'fixtures/post'
|
13
|
+
require 'fixtures/user'
|
14
|
+
|
15
|
+
module LazyResource
|
16
|
+
autoload :HttpMock
|
17
|
+
end
|
18
|
+
|
19
|
+
LazyResource.configure do |config|
|
20
|
+
config.site = "http://example.com"
|
21
|
+
end
|
22
|
+
|
23
|
+
RSpec.configure do |config|
|
24
|
+
config.mock_with nil
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lazy_resource
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrew Latimer
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.1.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.1.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.1.0
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.1.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: json
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.5.2
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.5.2
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: typhoeus
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.2.4
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 0.2.4
|
78
|
+
description: ActiveResource with it's feet up. The write less, do more consumer of
|
79
|
+
delicious APIs.
|
80
|
+
email:
|
81
|
+
- andrew@elpasoera.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- .rspec
|
88
|
+
- .rvmrc
|
89
|
+
- Gemfile
|
90
|
+
- Guardfile
|
91
|
+
- LICENSE
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- examples/github.rb
|
95
|
+
- lazy_resource.gemspec
|
96
|
+
- lib/lazy_resource.rb
|
97
|
+
- lib/lazy_resource/attributes.rb
|
98
|
+
- lib/lazy_resource/errors.rb
|
99
|
+
- lib/lazy_resource/http_mock.rb
|
100
|
+
- lib/lazy_resource/mapping.rb
|
101
|
+
- lib/lazy_resource/relation.rb
|
102
|
+
- lib/lazy_resource/request.rb
|
103
|
+
- lib/lazy_resource/resource.rb
|
104
|
+
- lib/lazy_resource/resource_queue.rb
|
105
|
+
- lib/lazy_resource/types.rb
|
106
|
+
- lib/lazy_resource/url_generation.rb
|
107
|
+
- lib/lazy_resource/version.rb
|
108
|
+
- spec/fixtures/comment.rb
|
109
|
+
- spec/fixtures/post.rb
|
110
|
+
- spec/fixtures/user.rb
|
111
|
+
- spec/lazy_resource/attributes_spec.rb
|
112
|
+
- spec/lazy_resource/errors_spec.rb
|
113
|
+
- spec/lazy_resource/mapping_spec.rb
|
114
|
+
- spec/lazy_resource/relation_spec.rb
|
115
|
+
- spec/lazy_resource/request_spec.rb
|
116
|
+
- spec/lazy_resource/resource_queue_spec.rb
|
117
|
+
- spec/lazy_resource/resource_spec.rb
|
118
|
+
- spec/lazy_resource/types_spec.rb
|
119
|
+
- spec/lazy_resource/url_generation_spec.rb
|
120
|
+
- spec/spec_helper.rb
|
121
|
+
homepage: http://github.com/ahlatimer/lazy_resource
|
122
|
+
licenses: []
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ! '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
hash: -143928251561470554
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
segments:
|
143
|
+
- 0
|
144
|
+
hash: -143928251561470554
|
145
|
+
requirements: []
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 1.8.24
|
148
|
+
signing_key:
|
149
|
+
specification_version: 3
|
150
|
+
summary: ActiveResource with it's feet up. The write less, do more consumer of delicious
|
151
|
+
APIs.
|
152
|
+
test_files:
|
153
|
+
- spec/fixtures/comment.rb
|
154
|
+
- spec/fixtures/post.rb
|
155
|
+
- spec/fixtures/user.rb
|
156
|
+
- spec/lazy_resource/attributes_spec.rb
|
157
|
+
- spec/lazy_resource/errors_spec.rb
|
158
|
+
- spec/lazy_resource/mapping_spec.rb
|
159
|
+
- spec/lazy_resource/relation_spec.rb
|
160
|
+
- spec/lazy_resource/request_spec.rb
|
161
|
+
- spec/lazy_resource/resource_queue_spec.rb
|
162
|
+
- spec/lazy_resource/resource_spec.rb
|
163
|
+
- spec/lazy_resource/types_spec.rb
|
164
|
+
- spec/lazy_resource/url_generation_spec.rb
|
165
|
+
- spec/spec_helper.rb
|