sanity-ruby 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.dockerignore +10 -0
- data/.gitignore +1 -0
- data/Dockerfile-2.6 +30 -0
- data/Dockerfile-2.7 +31 -0
- data/Dockerfile-3.0 +30 -0
- data/Gemfile +1 -1
- data/README.md +121 -21
- data/bin/dev-lint +3 -0
- data/bin/dev-test +3 -0
- data/docker-compose.yml +46 -0
- data/lib/sanity/attributable.rb +1 -1
- data/lib/sanity/configuration.rb +5 -0
- data/lib/sanity/groq/filter.rb +2 -0
- data/lib/sanity/groq/slice.rb +1 -1
- data/lib/sanity/http/mutation.rb +22 -5
- data/lib/sanity/http/query.rb +21 -5
- data/lib/sanity/http/where.rb +3 -1
- data/lib/sanity/mutatable.rb +1 -1
- data/lib/sanity/refinements/strings.rb +1 -1
- data/lib/sanity/resource.rb +2 -0
- data/lib/sanity/serializable.rb +61 -0
- data/lib/sanity/version.rb +1 -1
- data/lib/sanity-ruby.rb +3 -0
- data/lib/sanity.rb +3 -0
- metadata +15 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6992c7a949150c3c6d2fc4f927e74f18b2b00d7158cb26905e195eb7d91da627
|
4
|
+
data.tar.gz: 0c2e2088e770fe98cff7a1522c6a39076a8efc43410ea9eb241311e2611cd1fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2fff7714db16558133549a6cc8ed496cc2e157b17b533a44501ad125a0f7636d1575c9766939989711c7f957de7e6167cbd285963bbf8aac19bcdb7c2cde9377
|
7
|
+
data.tar.gz: 360d11148a57b4880b7cc661e4f7b7a51b8e2be937f991a4f1a6546fb21be348bd11123bba3d46eb71178a8c6aa628bcc107e7cee55ebeb613f4a80b4df5cdbe
|
data/.dockerignore
ADDED
data/.gitignore
CHANGED
data/Dockerfile-2.6
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
FROM ruby:2.6-alpine as base
|
2
|
+
|
3
|
+
RUN apk add --update --no-cache \
|
4
|
+
build-base \
|
5
|
+
cmake \
|
6
|
+
tzdata \
|
7
|
+
bash \
|
8
|
+
git
|
9
|
+
|
10
|
+
ENV APP_PATH /var/www/sanity-ruby
|
11
|
+
RUN mkdir -p $APP_PATH
|
12
|
+
|
13
|
+
# Build intermediate
|
14
|
+
FROM base as intermediate
|
15
|
+
|
16
|
+
WORKDIR $APP_PATH
|
17
|
+
|
18
|
+
RUN rm -rf /var/cache/apk/*
|
19
|
+
|
20
|
+
FROM base as development
|
21
|
+
|
22
|
+
COPY --from=intermediate $APP_PATH $APP_PATH
|
23
|
+
|
24
|
+
WORKDIR $APP_PATH
|
25
|
+
|
26
|
+
ENV GEM_HOME $APP_PATH/vendor/bundle
|
27
|
+
ENV BUNDLE_PATH vendor/bundle
|
28
|
+
|
29
|
+
COPY . ./
|
30
|
+
RUN bundle check || bundle install
|
data/Dockerfile-2.7
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
FROM ruby:2.7-alpine as base
|
2
|
+
|
3
|
+
RUN apk add --update --no-cache \
|
4
|
+
build-base \
|
5
|
+
cmake \
|
6
|
+
tzdata \
|
7
|
+
bash \
|
8
|
+
git
|
9
|
+
|
10
|
+
ENV APP_PATH /var/www/sanity-ruby
|
11
|
+
RUN mkdir -p $APP_PATH
|
12
|
+
|
13
|
+
# Build intermediate
|
14
|
+
FROM base as intermediate
|
15
|
+
|
16
|
+
WORKDIR $APP_PATH
|
17
|
+
|
18
|
+
RUN rm -rf /var/cache/apk/*
|
19
|
+
|
20
|
+
FROM base as development
|
21
|
+
|
22
|
+
COPY --from=intermediate $APP_PATH $APP_PATH
|
23
|
+
|
24
|
+
WORKDIR $APP_PATH
|
25
|
+
|
26
|
+
ENV GEM_HOME $APP_PATH/vendor/bundle
|
27
|
+
ENV BUNDLE_PATH vendor/bundle
|
28
|
+
|
29
|
+
COPY . ./
|
30
|
+
RUN bundle check || bundle install
|
31
|
+
|
data/Dockerfile-3.0
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
FROM ruby:3.0-alpine as base
|
2
|
+
|
3
|
+
RUN apk add --update --no-cache \
|
4
|
+
build-base \
|
5
|
+
cmake \
|
6
|
+
tzdata \
|
7
|
+
bash \
|
8
|
+
git
|
9
|
+
|
10
|
+
ENV APP_PATH /var/www/sanity-ruby
|
11
|
+
RUN mkdir -p $APP_PATH
|
12
|
+
|
13
|
+
# Build intermediate
|
14
|
+
FROM base as intermediate
|
15
|
+
|
16
|
+
WORKDIR $APP_PATH
|
17
|
+
|
18
|
+
RUN rm -rf /var/cache/apk/*
|
19
|
+
|
20
|
+
FROM base as development
|
21
|
+
|
22
|
+
COPY --from=intermediate $APP_PATH $APP_PATH
|
23
|
+
|
24
|
+
WORKDIR $APP_PATH
|
25
|
+
|
26
|
+
ENV GEM_HOME $APP_PATH/vendor/bundle
|
27
|
+
ENV BUNDLE_PATH vendor/bundle
|
28
|
+
|
29
|
+
COPY . ./
|
30
|
+
RUN bundle check || bundle install
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -3,23 +3,26 @@
|
|
3
3
|
![](https://github.com/morning-brew/sanity-ruby/actions/workflows/ci.yml/badge.svg)
|
4
4
|
<a href="https://codeclimate.com/github/morning-brew/sanity-ruby/maintainability"><img src="https://api.codeclimate.com/v1/badges/1984ee6eb0bce46a2469/maintainability" /></a>
|
5
5
|
|
6
|
-
The Sanity Ruby library provides convenient access to the Sanity API from applications written in Ruby. It includes a pre-defined set of classes for API resources
|
6
|
+
The Sanity Ruby library provides convenient access to the Sanity API from applications written in Ruby. It includes a pre-defined set of classes for API resources.
|
7
7
|
|
8
|
-
The library also provides other features
|
8
|
+
The library also provides other features, like:
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
- Easy configuration for fast setup and use.
|
11
|
+
- A pre-defined class to help make any PORO a "sanity resource"
|
12
|
+
- Extensibility in overriding the serializer for the API response results
|
13
|
+
- A small DSL around GROQ queries
|
14
14
|
|
15
15
|
## Contents
|
16
16
|
|
17
|
-
- [
|
18
|
-
- [
|
19
|
-
- [
|
20
|
-
- [
|
21
|
-
- [
|
22
|
-
- [
|
17
|
+
- [Sanity](#sanity)
|
18
|
+
- [Contents](#contents)
|
19
|
+
- [Getting Started](#getting-started)
|
20
|
+
- [Serialization](#serialization)
|
21
|
+
- [Mutating](#mutating)
|
22
|
+
- [Querying](#querying)
|
23
|
+
- [Development](#development)
|
24
|
+
- [Contributing](#contributing)
|
25
|
+
- [License](#license)
|
23
26
|
|
24
27
|
## Getting Started
|
25
28
|
|
@@ -29,12 +32,33 @@ Add this line to your application's Gemfile:
|
|
29
32
|
gem 'sanity-ruby'
|
30
33
|
```
|
31
34
|
|
35
|
+
Setup your configuration. If using in Rails, consider setting this in an initializer:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
Sanity.configure do |s|
|
39
|
+
s.token = "yoursupersecrettoken"
|
40
|
+
s.api_version = "v2021-03-25"
|
41
|
+
s.project_id = "1234"
|
42
|
+
s.dataset = "development"
|
43
|
+
s.use_cdn = false
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
32
47
|
To create a new document:
|
33
48
|
|
34
49
|
```ruby
|
35
50
|
Sanity::Document.create(params: {_type: "user", first_name: "Carl", last_name: "Sagan"})
|
36
51
|
```
|
37
52
|
|
53
|
+
You can also return the created document ID.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
res = Sanity::Document.create(params: {_type: "user", first_name: "Carl", last_name: "Sagan"}, options: {return_ids: true})
|
57
|
+
|
58
|
+
# JSON.parse(res.body)["results"]
|
59
|
+
# > [{"id"=>"1fc471c6434fdc654ba447", "operation"=>"create"}]
|
60
|
+
```
|
61
|
+
|
38
62
|
To create a new asset:
|
39
63
|
|
40
64
|
```ruby
|
@@ -77,6 +101,63 @@ class User
|
|
77
101
|
end
|
78
102
|
```
|
79
103
|
|
104
|
+
## Serialization
|
105
|
+
|
106
|
+
When using a PORO, you can opt-in to automatically serialize your results. You
|
107
|
+
must define all attributes that should be serialized.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
class User < Sanity::Resource
|
111
|
+
auto_serialize
|
112
|
+
...
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
Additionally, you can configure a custom serializer. See how to define a custom
|
117
|
+
serializer [below](#custom-serializer).
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
class User < Sanity::Resource
|
121
|
+
serializer UserSerializer
|
122
|
+
...
|
123
|
+
end
|
124
|
+
```
|
125
|
+
|
126
|
+
Finally, at query time you can also pass in a serializer. A serializer specified
|
127
|
+
at query time will take priority over any other configuration.
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
User.where(active: true, serializer: UserSerializer)
|
131
|
+
```
|
132
|
+
|
133
|
+
where `UserSerializer` might look like:
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
class UserSerializer
|
137
|
+
class << self
|
138
|
+
def call(...)
|
139
|
+
new(...).call
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
attr_reader :results
|
144
|
+
|
145
|
+
def initialize(args)
|
146
|
+
@results = args["result"]
|
147
|
+
end
|
148
|
+
|
149
|
+
def call
|
150
|
+
results.map do |result|
|
151
|
+
User.new(
|
152
|
+
_id: result["_id"],
|
153
|
+
_type: result["_type"]
|
154
|
+
)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
```
|
159
|
+
|
160
|
+
|
80
161
|
## Mutating
|
81
162
|
|
82
163
|
To [create a document](https://www.sanity.io/docs/http-mutations#c732f27330a4):
|
@@ -114,15 +195,11 @@ Sanity::Document.patch(params: { _id: "1234-321", set: { first_name: "Carl" }})
|
|
114
195
|
To [find document(s) by id](https://www.sanity.io/docs/http-doc):
|
115
196
|
|
116
197
|
```ruby
|
117
|
-
Sanity::Document.find(
|
198
|
+
Sanity::Document.find(id: "1234-321")
|
118
199
|
```
|
119
200
|
|
120
201
|
To find documents based on certain fields:
|
121
202
|
|
122
|
-
```ruby
|
123
|
-
Sanity::Document.where(_id: "1234-321", slug: "foobar")
|
124
|
-
```
|
125
|
-
|
126
203
|
[Where](https://www.sanity.io/docs/query-cheat-sheet#3949cadc7524)
|
127
204
|
|
128
205
|
_majority supported_
|
@@ -178,26 +255,49 @@ select: [:_id, :slug, :title, :name]
|
|
178
255
|
```ruby
|
179
256
|
Sanity::Document.where(_type: "user", select: %i[first_name last_name])
|
180
257
|
```
|
181
|
-
|
258
|
+
|
182
259
|
Should you need more advanced querying that isn't handled in this gem's DSL you can pass a raw groq query
|
183
260
|
|
184
261
|
[Query Cheat Sheet](https://www.sanity.io/docs/query-cheat-sheet)
|
185
262
|
|
186
263
|
```ruby
|
187
|
-
|
264
|
+
groq_query = <<-GROQ
|
265
|
+
*[ _type =='movie' && name == $name] {
|
266
|
+
title,
|
267
|
+
poster {
|
268
|
+
asset-> {
|
269
|
+
path,
|
270
|
+
url
|
271
|
+
}
|
272
|
+
}
|
273
|
+
}
|
274
|
+
GROQ
|
275
|
+
|
276
|
+
Sanity::Document.where(groq: groq_query, variables: {name: "Monsters, Inc."})
|
188
277
|
```
|
189
278
|
|
279
|
+
|
190
280
|
## Development
|
191
281
|
|
192
282
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
193
283
|
|
194
|
-
To install this gem onto your local machine, run `bundle exec rake install`.
|
284
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
285
|
+
|
286
|
+
### Testing across all supported versions:
|
287
|
+
|
288
|
+
To run tests across all gem supported ruby versions (requires Docker):
|
289
|
+
```sh
|
290
|
+
bin/dev-test
|
291
|
+
```
|
292
|
+
To run lint across all gem supported ruby versions (requires Docker):
|
293
|
+
```sh
|
294
|
+
bin/dev-lint
|
295
|
+
```
|
195
296
|
|
196
297
|
## Contributing
|
197
298
|
|
198
299
|
Bug reports and pull requests are welcome on GitHub at https://github.com/morning-brew/sanity-ruby.
|
199
300
|
|
200
|
-
|
201
301
|
## License
|
202
302
|
|
203
303
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/bin/dev-lint
ADDED
data/bin/dev-test
ADDED
data/docker-compose.yml
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
version: '3.9'
|
2
|
+
|
3
|
+
services:
|
4
|
+
'2.6':
|
5
|
+
build:
|
6
|
+
context: .
|
7
|
+
dockerfile: Dockerfile-2.6
|
8
|
+
tty: true
|
9
|
+
stdin_open: true
|
10
|
+
volumes:
|
11
|
+
- ./bin:/var/www/sanity-ruby/bin/
|
12
|
+
- ./lib:/var/www/sanity-ruby/lib/
|
13
|
+
- ./test:/var/www/sanity-ruby/test/
|
14
|
+
container_name: sanity-ruby-2.6
|
15
|
+
command: bash
|
16
|
+
|
17
|
+
'2.7':
|
18
|
+
build:
|
19
|
+
context: .
|
20
|
+
dockerfile: Dockerfile-2.7
|
21
|
+
tty: true
|
22
|
+
stdin_open: true
|
23
|
+
volumes:
|
24
|
+
- ./bin:/var/www/sanity-ruby/bin/
|
25
|
+
- ./lib:/var/www/sanity-ruby/lib/
|
26
|
+
- ./test:/var/www/sanity-ruby/test/
|
27
|
+
container_name: sanity-ruby-2.7
|
28
|
+
command: bash
|
29
|
+
|
30
|
+
'3.0':
|
31
|
+
build:
|
32
|
+
context: .
|
33
|
+
dockerfile: Dockerfile-3.0
|
34
|
+
tty: true
|
35
|
+
stdin_open: true
|
36
|
+
volumes:
|
37
|
+
- ./bin:/var/www/sanity-ruby/bin/
|
38
|
+
- ./lib:/var/www/sanity-ruby/lib/
|
39
|
+
- ./test:/var/www/sanity-ruby/test/
|
40
|
+
container_name: sanity-ruby-3.0
|
41
|
+
command: bash
|
42
|
+
|
43
|
+
volumes:
|
44
|
+
bin:
|
45
|
+
lib:
|
46
|
+
test:
|
data/lib/sanity/attributable.rb
CHANGED
data/lib/sanity/configuration.rb
CHANGED
data/lib/sanity/groq/filter.rb
CHANGED
@@ -68,6 +68,8 @@ module Sanity
|
|
68
68
|
filter_value << "#{filter(key: nested_key)} #{key} #{equal} #{cast_value(val)}"
|
69
69
|
elsif val.is_a?(Array) && !val[0].is_a?(Hash)
|
70
70
|
filter_value << "#{key} in #{val.map(&:to_s)}"
|
71
|
+
elsif [true, false].include?(val)
|
72
|
+
filter_value << "#{filter(key: nested_key)} #{key} #{equal} #{val}"
|
71
73
|
elsif LOGICAL_OPERATORS.key?(key)
|
72
74
|
if val.is_a?(Array)
|
73
75
|
val.each { |hsh| iterate(hsh, nested_key: key) }
|
data/lib/sanity/groq/slice.rb
CHANGED
data/lib/sanity/http/mutation.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "set"
|
3
4
|
using Sanity::Refinements::Strings
|
4
5
|
using Sanity::Refinements::Arrays
|
5
6
|
|
@@ -10,7 +11,7 @@ module Sanity
|
|
10
11
|
def included(base)
|
11
12
|
base.extend(ClassMethods)
|
12
13
|
base.extend(Forwardable)
|
13
|
-
base.delegate(%i[project_id api_version dataset token] => :"Sanity.config")
|
14
|
+
base.delegate(%i[project_id api_version dataset token api_subdomain] => :"Sanity.config")
|
14
15
|
base.delegate(mutatable_api_endpoint: :resource_klass)
|
15
16
|
end
|
16
17
|
end
|
@@ -34,13 +35,18 @@ module Sanity
|
|
34
35
|
visibility: :sync
|
35
36
|
}.freeze
|
36
37
|
|
37
|
-
attr_reader :options, :params, :resource_klass, :query_set, :
|
38
|
+
attr_reader :options, :params, :resource_klass, :query_set, :serializer
|
38
39
|
|
39
40
|
def initialize(**args)
|
40
41
|
@resource_klass = args.delete(:resource_klass)
|
41
42
|
@params = args.delete(:params)
|
42
43
|
@query_set = Set.new
|
43
|
-
|
44
|
+
|
45
|
+
warn RESULT_WRAPPER_DEPRECATION_WARNING if args[:result_wrapper]
|
46
|
+
@serializer = args.delete(:serializer) ||
|
47
|
+
args.delete(:result_wrapper) || # kept for backwards compatibility
|
48
|
+
klass_serializer ||
|
49
|
+
Sanity::Http::Results
|
44
50
|
|
45
51
|
raise ArgumentError, "resource_klass must be defined" unless resource_klass
|
46
52
|
raise ArgumentError, "params argument is missing" unless params
|
@@ -59,14 +65,19 @@ module Sanity
|
|
59
65
|
|
60
66
|
def call
|
61
67
|
Net::HTTP.post(uri, {"#{REQUEST_KEY}": body}.to_json, headers).then do |result|
|
62
|
-
block_given? ? yield(
|
68
|
+
block_given? ? yield(serializer.call(result)) : serializer.call(result)
|
63
69
|
end
|
64
70
|
end
|
65
71
|
|
72
|
+
def result_wrapper
|
73
|
+
warn RESULT_WRAPPER_DEPRECATION_WARNING
|
74
|
+
serializer
|
75
|
+
end
|
76
|
+
|
66
77
|
private
|
67
78
|
|
68
79
|
def base_url
|
69
|
-
"https://#{project_id}.
|
80
|
+
"https://#{project_id}.#{api_subdomain}.sanity.io/#{api_version}/#{mutatable_api_endpoint}/#{dataset}"
|
70
81
|
end
|
71
82
|
|
72
83
|
def body
|
@@ -88,6 +99,12 @@ module Sanity
|
|
88
99
|
}
|
89
100
|
end
|
90
101
|
|
102
|
+
def klass_serializer
|
103
|
+
return unless @resource_klass.respond_to?(:default_serializer)
|
104
|
+
|
105
|
+
@resource_klass.default_serializer
|
106
|
+
end
|
107
|
+
|
91
108
|
def query_params
|
92
109
|
camelize_query_set.map do |key, val|
|
93
110
|
"#{key}=#{val}"
|
data/lib/sanity/http/query.rb
CHANGED
@@ -11,7 +11,7 @@ module Sanity
|
|
11
11
|
def included(base)
|
12
12
|
base.extend(ClassMethods)
|
13
13
|
base.extend(Forwardable)
|
14
|
-
base.delegate(%i[project_id api_version dataset token] => :"Sanity.config")
|
14
|
+
base.delegate(%i[project_id api_version dataset token api_subdomain] => :"Sanity.config")
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -21,12 +21,17 @@ module Sanity
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
attr_reader :resource_klass, :
|
24
|
+
attr_reader :resource_klass, :serializer
|
25
25
|
|
26
26
|
# @todo Add query support
|
27
27
|
def initialize(**args)
|
28
28
|
@resource_klass = args.delete(:resource_klass)
|
29
|
-
|
29
|
+
|
30
|
+
warn RESULT_WRAPPER_DEPRECATION_WARNING if args[:result_wrapper]
|
31
|
+
@serializer = args.delete(:serializer) ||
|
32
|
+
args.delete(:result_wrapper) || # kept for backwards compatibility
|
33
|
+
klass_serializer ||
|
34
|
+
Sanity::Http::Results
|
30
35
|
end
|
31
36
|
|
32
37
|
# @todo Add query support
|
@@ -42,10 +47,15 @@ module Sanity
|
|
42
47
|
http.request(request).then do |result|
|
43
48
|
data = JSON.parse(result.body)
|
44
49
|
|
45
|
-
block_given? ? yield(
|
50
|
+
block_given? ? yield(serializer.call(data)) : serializer.call(data)
|
46
51
|
end
|
47
52
|
end
|
48
53
|
|
54
|
+
def result_wrapper
|
55
|
+
warn RESULT_WRAPPER_DEPRECATION_WARNING
|
56
|
+
serializer
|
57
|
+
end
|
58
|
+
|
49
59
|
private
|
50
60
|
|
51
61
|
def request_body
|
@@ -56,7 +66,7 @@ module Sanity
|
|
56
66
|
end
|
57
67
|
|
58
68
|
def base_url
|
59
|
-
"https://#{project_id}.
|
69
|
+
"https://#{project_id}.#{api_subdomain}.sanity.io/#{api_version}/#{api_endpoint}/#{dataset}"
|
60
70
|
end
|
61
71
|
|
62
72
|
def headers
|
@@ -66,6 +76,12 @@ module Sanity
|
|
66
76
|
}
|
67
77
|
end
|
68
78
|
|
79
|
+
def klass_serializer
|
80
|
+
return unless @resource_klass.respond_to?(:default_serializer)
|
81
|
+
|
82
|
+
@resource_klass.default_serializer
|
83
|
+
end
|
84
|
+
|
69
85
|
def uri
|
70
86
|
URI(base_url)
|
71
87
|
end
|
data/lib/sanity/http/where.rb
CHANGED
@@ -18,7 +18,9 @@ module Sanity
|
|
18
18
|
@variables = args.delete(:variables) || {}
|
19
19
|
@use_post = args.delete(:use_post) || false
|
20
20
|
|
21
|
-
@groq_attributes = args.except(
|
21
|
+
@groq_attributes = args.except(
|
22
|
+
:groq, :use_post, :resource_klass, :serializer, :result_wrapper
|
23
|
+
)
|
22
24
|
end
|
23
25
|
|
24
26
|
private
|
data/lib/sanity/mutatable.rb
CHANGED
data/lib/sanity/resource.rb
CHANGED
@@ -8,6 +8,7 @@ module Sanity
|
|
8
8
|
# Sanity::Attributable
|
9
9
|
# Sanity::Mutatable
|
10
10
|
# Sanity::Queryable
|
11
|
+
# Sanity::Serializable
|
11
12
|
#
|
12
13
|
# Sanity::Document and Sanity::Asset both inherit
|
13
14
|
# from Sanity::Resource
|
@@ -26,5 +27,6 @@ module Sanity
|
|
26
27
|
include Sanity::Attributable
|
27
28
|
include Sanity::Mutatable
|
28
29
|
include Sanity::Queryable
|
30
|
+
include Sanity::Serializable
|
29
31
|
end
|
30
32
|
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sanity
|
4
|
+
# Serializable is responsible for configuring auto serialization or a default
|
5
|
+
# serializer. It also defines the default class serializer used when auto
|
6
|
+
# serialization is enabled.
|
7
|
+
#
|
8
|
+
# The auto_serialize macro is used to enable auto serialization.
|
9
|
+
#
|
10
|
+
# @example enables auto serialization
|
11
|
+
# auto_serialize
|
12
|
+
#
|
13
|
+
# The serializer marco is used to define the default serializer.
|
14
|
+
#
|
15
|
+
# @example default to using a custom defined UserSerializer
|
16
|
+
# serializer UserSerializer
|
17
|
+
#
|
18
|
+
module Serializable
|
19
|
+
class << self
|
20
|
+
def included(base)
|
21
|
+
base.extend(ClassMethods)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module ClassMethods
|
26
|
+
def default_serializer
|
27
|
+
@default_serializer ||=
|
28
|
+
if auto_serialize?
|
29
|
+
class_serializer
|
30
|
+
elsif defined?(@serializer)
|
31
|
+
@serializer
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def auto_serialize?
|
36
|
+
return @auto_serialize if defined?(@auto_serialize)
|
37
|
+
|
38
|
+
superclass.respond_to?(:auto_serialize?) && superclass.auto_serialize?
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def auto_serialize
|
44
|
+
@auto_serialize = true
|
45
|
+
end
|
46
|
+
|
47
|
+
def serializer(serializer)
|
48
|
+
@serializer = serializer
|
49
|
+
end
|
50
|
+
|
51
|
+
def class_serializer
|
52
|
+
@class_serializer ||= proc do |results|
|
53
|
+
results["result"].map do |result|
|
54
|
+
attributes = result.slice(*self.attributes.map(&:to_s))
|
55
|
+
new(**attributes.transform_keys(&:to_sym))
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/sanity/version.rb
CHANGED
data/lib/sanity-ruby.rb
ADDED
data/lib/sanity.rb
CHANGED
@@ -12,10 +12,13 @@ require "sanity/http"
|
|
12
12
|
require "sanity/attributable"
|
13
13
|
require "sanity/mutatable"
|
14
14
|
require "sanity/queryable"
|
15
|
+
require "sanity/serializable"
|
15
16
|
|
16
17
|
require "sanity/resource"
|
17
18
|
require "sanity/resources"
|
18
19
|
|
19
20
|
module Sanity
|
20
21
|
class Error < StandardError; end
|
22
|
+
|
23
|
+
RESULT_WRAPPER_DEPRECATION_WARNING = "DEPRECATION: `result_wrapper` is deprecated. Please use `serializer` instead."
|
21
24
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sanity-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Morning Brew
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ''
|
14
14
|
email: tech@morningbrew.com
|
@@ -16,16 +16,24 @@ executables: []
|
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
+
- ".dockerignore"
|
19
20
|
- ".github/workflows/ci.yml"
|
20
21
|
- ".gitignore"
|
21
22
|
- ".standard.yml"
|
23
|
+
- Dockerfile-2.6
|
24
|
+
- Dockerfile-2.7
|
25
|
+
- Dockerfile-3.0
|
22
26
|
- Gemfile
|
23
27
|
- LICENSE.txt
|
24
28
|
- README.md
|
25
29
|
- Rakefile
|
26
30
|
- bin/console
|
31
|
+
- bin/dev-lint
|
32
|
+
- bin/dev-test
|
27
33
|
- bin/setup
|
28
34
|
- bin/standardrb
|
35
|
+
- docker-compose.yml
|
36
|
+
- lib/sanity-ruby.rb
|
29
37
|
- lib/sanity.rb
|
30
38
|
- lib/sanity/attributable.rb
|
31
39
|
- lib/sanity/configuration.rb
|
@@ -55,6 +63,7 @@ files:
|
|
55
63
|
- lib/sanity/resources.rb
|
56
64
|
- lib/sanity/resources/asset.rb
|
57
65
|
- lib/sanity/resources/document.rb
|
66
|
+
- lib/sanity/serializable.rb
|
58
67
|
- lib/sanity/version.rb
|
59
68
|
- sanity.gemspec
|
60
69
|
homepage: https://github.com/morning-brew/sanity-ruby
|
@@ -64,7 +73,7 @@ metadata:
|
|
64
73
|
homepage_uri: https://github.com/morning-brew/sanity-ruby
|
65
74
|
source_code_uri: https://github.com/morning-brew/sanity-ruby
|
66
75
|
changelog_uri: https://github.com/morning-brew/sanity-ruby
|
67
|
-
post_install_message:
|
76
|
+
post_install_message:
|
68
77
|
rdoc_options: []
|
69
78
|
require_paths:
|
70
79
|
- lib
|
@@ -79,8 +88,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
88
|
- !ruby/object:Gem::Version
|
80
89
|
version: '0'
|
81
90
|
requirements: []
|
82
|
-
rubygems_version: 3.
|
83
|
-
signing_key:
|
91
|
+
rubygems_version: 3.4.17
|
92
|
+
signing_key:
|
84
93
|
specification_version: 4
|
85
94
|
summary: Ruby bindings for the Sanity API
|
86
95
|
test_files: []
|