jsonapi-consumer 0.1.0.pre.3 → 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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +61 -1
- data/lib/jsonapi/consumer/resource.rb +5 -1
- data/lib/jsonapi/consumer/resource/connection_concern.rb +6 -2
- data/lib/jsonapi/consumer/version.rb +1 -1
- data/spec/fixtures/resources.rb +8 -0
- data/spec/jsonapi/consumer/connection_spec.rb +21 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ebd6ed496bf4e33e58103e89a8ea1f8ec65e49c
|
4
|
+
data.tar.gz: 4de1a846349a7e3606f45573fa70dd8c2b60bbdd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f90b5cf975914fdc9dced9176b48e8705f98c3ee8b423c653629aa76513e11b3615ed7e771f4bd54c58416198ad5073198b955a9bd8e379672c5fde8b5fa9ec6
|
7
|
+
data.tar.gz: 25da9ee3afd9b759a001834fb4989aeb73c1e6a8c0cfc59cecd51bd09fa1a17ac33350618db264089108a9fa7cacd68aef81f2a6f5c79fa0e602aab0cd5bb32b
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -22,7 +22,67 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
It's suggested to create a base resource for the whole API that you can re-use.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class Base
|
29
|
+
include JSONAPI::Consumer::Resource
|
30
|
+
|
31
|
+
self.host = 'http://localhost:3000/api/'
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Then inherit from that Base class for each resource defined in your API.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
module Blog
|
39
|
+
|
40
|
+
class Author < Base
|
41
|
+
has_many :posts, class_name: 'Blog::Post'
|
42
|
+
end
|
43
|
+
|
44
|
+
class Post < Base
|
45
|
+
has_one :user, class_name: 'Blog::User'
|
46
|
+
has_many :comments, class_name: 'Blog::Comment'
|
47
|
+
end
|
48
|
+
|
49
|
+
class User < Base
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
class Comment < Base
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
#### Additional Features
|
61
|
+
|
62
|
+
##### Dynamic Objects
|
63
|
+
|
64
|
+
By default calling `.new` or `.build` on a resource will give you an empty
|
65
|
+
object with no attributes defined. This is less than ideal when building forms
|
66
|
+
with something like Rails' FormBuilder.
|
67
|
+
|
68
|
+
We suggest setting up your model to do a `GET /{resource_name}/new` if your
|
69
|
+
server supports it and using `.build` instead of `.new`. This will populate the
|
70
|
+
object with defaults set by the server response.
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class User
|
74
|
+
include JSONAPI::Consumer::Resource
|
75
|
+
|
76
|
+
self.request_new_object_on_build = true
|
77
|
+
|
78
|
+
# .build will now call GET /users/new
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
#### Testing
|
83
|
+
|
84
|
+
We suggest [Webmock](https://github.com/bblimke/webmock) at this stage in
|
85
|
+
development. We plan to add test helpers before the first major release.
|
26
86
|
|
27
87
|
## Contributing
|
28
88
|
|
@@ -6,7 +6,7 @@ module JSONAPI::Consumer
|
|
6
6
|
extend ActiveModel::Naming
|
7
7
|
|
8
8
|
attr_reader :errors
|
9
|
-
class_attribute :host
|
9
|
+
class_attribute :host, :connection, :ssl
|
10
10
|
end
|
11
11
|
|
12
12
|
include ObjectBuildConcern
|
@@ -17,6 +17,10 @@ module JSONAPI::Consumer
|
|
17
17
|
include ConnectionConcern
|
18
18
|
|
19
19
|
module ClassMethods
|
20
|
+
def middleware(&block)
|
21
|
+
_connection(true, &block)
|
22
|
+
end
|
23
|
+
|
20
24
|
def json_key
|
21
25
|
self.name.demodulize.pluralize.underscore
|
22
26
|
end
|
@@ -23,18 +23,22 @@ module JSONAPI::Consumer
|
|
23
23
|
end
|
24
24
|
|
25
25
|
# :nodoc:
|
26
|
-
def _connection
|
27
|
-
|
26
|
+
def _connection(reload=false)
|
27
|
+
self.connection = nil if !!reload
|
28
|
+
self.connection ||= begin
|
28
29
|
Faraday.new(url: self.host, ssl: self.ssl) do |conn|
|
29
30
|
conn.request :json
|
30
31
|
conn.request :request_headers, accept: "application/json"
|
31
32
|
|
33
|
+
yield(conn) if block_given?
|
34
|
+
|
32
35
|
conn.use Middleware::RequestTimeout
|
33
36
|
conn.use Middleware::ParseJson
|
34
37
|
|
35
38
|
conn.use Middleware::RaiseError
|
36
39
|
conn.adapter Faraday.default_adapter
|
37
40
|
end
|
41
|
+
|
38
42
|
end
|
39
43
|
end
|
40
44
|
end
|
data/spec/fixtures/resources.rb
CHANGED
@@ -10,6 +10,27 @@ RSpec.describe 'Connection' do
|
|
10
10
|
|
11
11
|
let(:obj) { test_class.new(name: 'jsonapi.example') }
|
12
12
|
|
13
|
+
describe 'custom connection middleware' do
|
14
|
+
|
15
|
+
it 'handles custom middleware' do
|
16
|
+
stub_request(:get, "http://localhost:3000/api/basic_resources")
|
17
|
+
.with(headers: {accept: 'application/json'})
|
18
|
+
.to_return(headers: {content_type: "application/json"}, body: {
|
19
|
+
basic_resources: [
|
20
|
+
{id: '1'}
|
21
|
+
]
|
22
|
+
}.to_json)
|
23
|
+
|
24
|
+
expect { BasicResource.all }.to_not raise_error
|
25
|
+
|
26
|
+
BasicResource.middleware do |conn|
|
27
|
+
conn.use NotCalled
|
28
|
+
end
|
29
|
+
|
30
|
+
expect { BasicResource.all }.to raise_error(NotCalledError)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
13
34
|
describe '.all' do
|
14
35
|
it 'returns all results as objects' do
|
15
36
|
stub_request(:get, "http://localhost:3000/api/records")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-consumer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Smestad
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -217,9 +217,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
217
217
|
version: '0'
|
218
218
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
219
219
|
requirements:
|
220
|
-
- - "
|
220
|
+
- - ">="
|
221
221
|
- !ruby/object:Gem::Version
|
222
|
-
version:
|
222
|
+
version: '0'
|
223
223
|
requirements: []
|
224
224
|
rubyforge_project:
|
225
225
|
rubygems_version: 2.2.2
|