jeckle 0.5.0 → 0.6.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/README.md +53 -13
- data/lib/jeckle/attribute_aliasing.rb +17 -5
- data/lib/jeckle/http.rb +7 -20
- data/lib/jeckle/model.rb +5 -5
- data/lib/jeckle/resource.rb +5 -9
- data/lib/jeckle/rest_actions.rb +6 -5
- data/lib/jeckle/types.rb +7 -0
- data/lib/jeckle/version.rb +1 -1
- data/lib/jeckle.rb +3 -2
- metadata +24 -9
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4761ead36774dc1acab1c41b0365ff229c412677a75292aa1473acd381fd06cd
|
|
4
|
+
data.tar.gz: 64f4f522c235470e25be1660a414bebf5aabe81e4b60e934b1fcbd13ed08056b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 94176cba14e8fa3ad4aac3006191f34dffc050ca6739a05e991715108f8a9a8b4e6abb9e937028e3230357b6a39146f015a8484fc29a8a987e5dab8d43ae0508
|
|
7
|
+
data.tar.gz: 719608cb95326167d652fd67fa76ceac3929ba4610a1705e4ad948c769d97177f3eaa161907e7396ade56cc32368fe0b625e9be043ae6884529a15adc200b9b3
|
data/README.md
CHANGED
|
@@ -52,21 +52,19 @@ end
|
|
|
52
52
|
Following the previous example, Dribbble.com consists of pieces of web designers work called "Shots". Each shot has the attributes `id`, `name`, `url` and `image_url`. A Jeckle resource representing Dribbble's shots would be something like this:
|
|
53
53
|
|
|
54
54
|
```ruby
|
|
55
|
-
class Shot
|
|
56
|
-
include Jeckle::Resource
|
|
57
|
-
|
|
55
|
+
class Shot < Jeckle::Resource
|
|
58
56
|
api :dribbble
|
|
59
57
|
|
|
60
|
-
attribute :id, Integer
|
|
61
|
-
attribute :name, String
|
|
62
|
-
attribute :url, String
|
|
63
|
-
attribute :image_url, String
|
|
58
|
+
attribute :id, Jeckle::Types::Integer
|
|
59
|
+
attribute :name, Jeckle::Types::String
|
|
60
|
+
attribute :url, Jeckle::Types::String
|
|
61
|
+
attribute :image_url, Jeckle::Types::String
|
|
64
62
|
end
|
|
65
63
|
```
|
|
66
64
|
|
|
67
65
|
### Fetching data
|
|
68
66
|
|
|
69
|
-
The resource class allows us to
|
|
67
|
+
The resource class allows us to list shots through HTTP requests to the API, based on the provided information. For example, we can find a specific shot by providing its id to the `find` method:
|
|
70
68
|
|
|
71
69
|
```ruby
|
|
72
70
|
# GET http://api.dribbble.com/shots/1600459
|
|
@@ -86,11 +84,11 @@ shot.image_url
|
|
|
86
84
|
=> "https://d13yacurqjgara.cloudfront.net/users/85699/screenshots/1600459/daryl_heckle_and_jeckle_oates-dribble.jpg"
|
|
87
85
|
```
|
|
88
86
|
|
|
89
|
-
You can also look for many shots matching one or more attributes, by using the `
|
|
87
|
+
You can also look for many shots matching one or more attributes, by using the `list` method:
|
|
90
88
|
|
|
91
89
|
```ruby
|
|
92
90
|
# GET http://api.dribbble.com/shots?name=avengers
|
|
93
|
-
shots = Shot.
|
|
91
|
+
shots = Shot.list name: 'avengers'
|
|
94
92
|
```
|
|
95
93
|
|
|
96
94
|
### Attribute Aliasing
|
|
@@ -98,7 +96,7 @@ shots = Shot.search name: 'avengers'
|
|
|
98
96
|
Sometimes you want to call the API's attributes something else, either because their names aren't very concise or because they're out of you app's convention. If that's the case, you can add an `as` option:
|
|
99
97
|
|
|
100
98
|
```ruby
|
|
101
|
-
attribute :thumbnailSize, String, as: :thumbnail_size
|
|
99
|
+
attribute :thumbnailSize, Jeckle::Types::String, as: :thumbnail_size
|
|
102
100
|
```
|
|
103
101
|
|
|
104
102
|
Both mapping will work:
|
|
@@ -111,8 +109,6 @@ shot.thumbnail_size
|
|
|
111
109
|
=> "50x50"
|
|
112
110
|
```
|
|
113
111
|
|
|
114
|
-
We're all set! Now we can expand the mapping of our API, e.g to add ability to search Dribbble Designer directory by adding Designer class, or we can expand the original mapping of Shot class to include more attributes, such as tags or comments.
|
|
115
|
-
|
|
116
112
|
### Error Handling
|
|
117
113
|
|
|
118
114
|
Jeckle provides a built-in Faraday middleware that automatically raises typed errors for HTTP error responses. Enable it in your API configuration:
|
|
@@ -156,6 +152,50 @@ The error hierarchy:
|
|
|
156
152
|
- `Jeckle::ServerError` — 5xx errors
|
|
157
153
|
- `InternalServerError` (500), `ServiceUnavailableError` (503)
|
|
158
154
|
|
|
155
|
+
We're all set! Now we can expand the mapping of our API, e.g to add ability to search Dribbble Designer directory by adding Designer class, or we can expand the original mapping of Shot class to include more attributes, such as tags or comments.
|
|
156
|
+
|
|
157
|
+
## Migration from 0.4.x
|
|
158
|
+
|
|
159
|
+
### Resource definition
|
|
160
|
+
|
|
161
|
+
Resources now use class inheritance instead of module inclusion:
|
|
162
|
+
|
|
163
|
+
```ruby
|
|
164
|
+
# Before (0.4.x)
|
|
165
|
+
class Shot
|
|
166
|
+
include Jeckle::Resource
|
|
167
|
+
attribute :id, Integer
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# After (0.6.0+)
|
|
171
|
+
class Shot < Jeckle::Resource
|
|
172
|
+
attribute :id, Jeckle::Types::Integer
|
|
173
|
+
end
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Attribute types
|
|
177
|
+
|
|
178
|
+
Use `Jeckle::Types::*` instead of Ruby constants:
|
|
179
|
+
|
|
180
|
+
| Before | After |
|
|
181
|
+
|--------|-------|
|
|
182
|
+
| `Integer` | `Jeckle::Types::Integer` |
|
|
183
|
+
| `String` | `Jeckle::Types::String` |
|
|
184
|
+
| `Float` | `Jeckle::Types::Float` |
|
|
185
|
+
| `Boolean` | `Jeckle::Types::Bool` |
|
|
186
|
+
|
|
187
|
+
### Collection method
|
|
188
|
+
|
|
189
|
+
The `search` method has been renamed to `list`:
|
|
190
|
+
|
|
191
|
+
```ruby
|
|
192
|
+
# Before
|
|
193
|
+
Shot.search name: 'avengers'
|
|
194
|
+
|
|
195
|
+
# After
|
|
196
|
+
Shot.list name: 'avengers'
|
|
197
|
+
```
|
|
198
|
+
|
|
159
199
|
## Examples
|
|
160
200
|
|
|
161
201
|
You can see more examples here: [https://github.com/tomas-stefano/jeckle/tree/master/examples](https://github.com/tomas-stefano/jeckle/tree/master/examples)
|
|
@@ -2,14 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
module Jeckle
|
|
4
4
|
module AttributeAliasing
|
|
5
|
-
def attribute(name,
|
|
5
|
+
def attribute(name, type = nil, **options)
|
|
6
6
|
if (custom_name = options.delete(:as))
|
|
7
|
-
|
|
7
|
+
original_name = name
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
if type
|
|
10
|
+
super(custom_name, type)
|
|
11
|
+
else
|
|
12
|
+
super(custom_name)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
define_method(original_name) { public_send(custom_name) }
|
|
16
|
+
|
|
17
|
+
transform_keys do |key|
|
|
18
|
+
key = key.to_sym
|
|
19
|
+
key == original_name ? custom_name : key
|
|
20
|
+
end
|
|
21
|
+
elsif type
|
|
22
|
+
super(name, type)
|
|
11
23
|
else
|
|
12
|
-
super
|
|
24
|
+
super(name)
|
|
13
25
|
end
|
|
14
26
|
end
|
|
15
27
|
end
|
data/lib/jeckle/http.rb
CHANGED
|
@@ -2,38 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
module Jeckle
|
|
4
4
|
module HTTP
|
|
5
|
-
def self.included(base)
|
|
6
|
-
base.extend Jeckle::HTTP::APIMapping
|
|
7
|
-
end
|
|
8
|
-
|
|
9
5
|
module APIMapping
|
|
10
6
|
def inherited(base)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
end
|
|
7
|
+
super
|
|
8
|
+
base.instance_variable_set(:@api_mapping, api_mapping.dup)
|
|
14
9
|
end
|
|
15
10
|
|
|
16
11
|
# The name of the resource that Jeckle uses to make the request
|
|
17
12
|
#
|
|
18
13
|
# @example
|
|
19
14
|
#
|
|
20
|
-
#
|
|
21
|
-
# class Shot
|
|
22
|
-
# include Jeckle::Resource
|
|
23
|
-
# end
|
|
15
|
+
# class Shot < Jeckle::Resource
|
|
24
16
|
# end
|
|
25
17
|
#
|
|
26
18
|
# Shot.resource_name # => Will request for '/shots' resource
|
|
27
19
|
#
|
|
28
20
|
# To overwrite this behaviour, rewrite the resource name method in the class:
|
|
29
21
|
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
-
# def self.resource_name
|
|
35
|
-
# '/project'
|
|
36
|
-
# end
|
|
22
|
+
# class Project < Jeckle::Resource
|
|
23
|
+
# def self.resource_name
|
|
24
|
+
# '/project'
|
|
37
25
|
# end
|
|
38
26
|
# end
|
|
39
27
|
#
|
|
@@ -51,8 +39,7 @@ module Jeckle
|
|
|
51
39
|
# end
|
|
52
40
|
# end
|
|
53
41
|
#
|
|
54
|
-
# class Shot
|
|
55
|
-
# include Jeckle::Resource
|
|
42
|
+
# class Shot < Jeckle::Resource
|
|
56
43
|
# api :dribbble
|
|
57
44
|
# end
|
|
58
45
|
#
|
data/lib/jeckle/model.rb
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Jeckle
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
class Model < Dry::Struct
|
|
5
|
+
transform_keys(&:to_sym)
|
|
6
|
+
transform_types(&:omittable)
|
|
7
|
+
|
|
8
|
+
include ActiveModel::Validations
|
|
9
9
|
end
|
|
10
10
|
end
|
data/lib/jeckle/resource.rb
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Jeckle
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
base.include ActiveModel::Naming
|
|
4
|
+
class Resource < Jeckle::Model
|
|
5
|
+
include ActiveModel::Naming
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
base.extend Jeckle::AttributeAliasing
|
|
13
|
-
end
|
|
7
|
+
extend Jeckle::HTTP::APIMapping
|
|
8
|
+
extend Jeckle::RESTActions::Collection
|
|
9
|
+
extend Jeckle::AttributeAliasing
|
|
14
10
|
end
|
|
15
11
|
end
|
data/lib/jeckle/rest_actions.rb
CHANGED
|
@@ -2,10 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
module Jeckle
|
|
4
4
|
module RESTActions
|
|
5
|
-
def self.included(base)
|
|
6
|
-
base.extend Jeckle::RESTActions::Collection
|
|
7
|
-
end
|
|
8
|
-
|
|
9
5
|
module Collection
|
|
10
6
|
def find(id)
|
|
11
7
|
endpoint = "#{resource_name}/#{id}"
|
|
@@ -14,7 +10,7 @@ module Jeckle
|
|
|
14
10
|
new attributes
|
|
15
11
|
end
|
|
16
12
|
|
|
17
|
-
def
|
|
13
|
+
def list(params = {})
|
|
18
14
|
custom_resource_name = params.delete(:resource_name) if params.is_a?(Hash)
|
|
19
15
|
|
|
20
16
|
response = run_request(custom_resource_name || resource_name, params: params).response.body || []
|
|
@@ -22,6 +18,11 @@ module Jeckle
|
|
|
22
18
|
|
|
23
19
|
Array(collection).collect { |attrs| new attrs }
|
|
24
20
|
end
|
|
21
|
+
|
|
22
|
+
def search(params = {})
|
|
23
|
+
warn '[DEPRECATION] `search` is deprecated. Please use `list` instead.'
|
|
24
|
+
list(params)
|
|
25
|
+
end
|
|
25
26
|
end
|
|
26
27
|
end
|
|
27
28
|
end
|
data/lib/jeckle/types.rb
ADDED
data/lib/jeckle/version.rb
CHANGED
data/lib/jeckle.rb
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'active_model'
|
|
4
|
+
require 'dry-struct'
|
|
5
|
+
require 'dry/types'
|
|
4
6
|
require 'faraday'
|
|
5
|
-
require 'virtus'
|
|
6
7
|
|
|
7
8
|
require 'jeckle/version'
|
|
8
9
|
|
|
9
10
|
%w[
|
|
10
|
-
setup api model request http rest_actions resource errors
|
|
11
|
+
types setup api model request http rest_actions attribute_aliasing resource errors
|
|
11
12
|
middleware/raise_error
|
|
12
13
|
].each do |file_name|
|
|
13
14
|
require "jeckle/#{file_name}"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jeckle
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tomas D'Stefano
|
|
@@ -26,33 +26,47 @@ dependencies:
|
|
|
26
26
|
- !ruby/object:Gem::Version
|
|
27
27
|
version: '6.0'
|
|
28
28
|
- !ruby/object:Gem::Dependency
|
|
29
|
-
name:
|
|
29
|
+
name: dry-struct
|
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
|
31
31
|
requirements:
|
|
32
32
|
- - "~>"
|
|
33
33
|
- !ruby/object:Gem::Version
|
|
34
|
-
version: '
|
|
34
|
+
version: '1.0'
|
|
35
35
|
type: :runtime
|
|
36
36
|
prerelease: false
|
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
|
38
38
|
requirements:
|
|
39
39
|
- - "~>"
|
|
40
40
|
- !ruby/object:Gem::Version
|
|
41
|
-
version: '
|
|
41
|
+
version: '1.0'
|
|
42
42
|
- !ruby/object:Gem::Dependency
|
|
43
|
-
name:
|
|
43
|
+
name: dry-types
|
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
|
45
45
|
requirements:
|
|
46
|
-
- - "
|
|
46
|
+
- - "~>"
|
|
47
47
|
- !ruby/object:Gem::Version
|
|
48
|
-
version: '0'
|
|
48
|
+
version: '1.0'
|
|
49
49
|
type: :runtime
|
|
50
50
|
prerelease: false
|
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
|
52
52
|
requirements:
|
|
53
|
-
- - "
|
|
53
|
+
- - "~>"
|
|
54
54
|
- !ruby/object:Gem::Version
|
|
55
|
-
version: '0'
|
|
55
|
+
version: '1.0'
|
|
56
|
+
- !ruby/object:Gem::Dependency
|
|
57
|
+
name: faraday
|
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - "~>"
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '2.0'
|
|
63
|
+
type: :runtime
|
|
64
|
+
prerelease: false
|
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - "~>"
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '2.0'
|
|
56
70
|
- !ruby/object:Gem::Dependency
|
|
57
71
|
name: bundler
|
|
58
72
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -172,6 +186,7 @@ files:
|
|
|
172
186
|
- lib/jeckle/resource.rb
|
|
173
187
|
- lib/jeckle/rest_actions.rb
|
|
174
188
|
- lib/jeckle/setup.rb
|
|
189
|
+
- lib/jeckle/types.rb
|
|
175
190
|
- lib/jeckle/version.rb
|
|
176
191
|
homepage: https://github.com/tomas-stefano/jeckle
|
|
177
192
|
licenses:
|