sanity-ruby 0.2.0 → 0.4.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 +89 -5
- 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/groq/filter.rb +2 -0
- data/lib/sanity/groq/slice.rb +1 -1
- data/lib/sanity/helpers/type_helper.rb +12 -0
- data/lib/sanity/helpers.rb +3 -0
- data/lib/sanity/http/mutation.rb +20 -3
- data/lib/sanity/http/query.rb +19 -3
- data/lib/sanity/http/where.rb +3 -1
- data/lib/sanity/queryable.rb +10 -5
- data/lib/sanity/refinements/strings.rb +1 -1
- data/lib/sanity/resource.rb +5 -2
- data/lib/sanity/serializable.rb +62 -0
- data/lib/sanity/version.rb +1 -1
- data/lib/sanity.rb +5 -1
- data/sanity.gemspec +5 -3
- metadata +34 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e02c772808fc5112f526e0e8bd329a829dd4ba0e2395d82a83d34f8419a05e66
|
4
|
+
data.tar.gz: c2b80c8bee2242e68ec2b814fc18e11ad699687a2174889cdf4a7b34823405f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e265993399cfa3c24f821cac9625daaf763a0468353d18f0fac375231bc9e62e727f2a3314bbf55fc7aaa3b5d51f733822cc16409489bf6f3aebbff75ba5c72d
|
7
|
+
data.tar.gz: 5bf5fbfdb1fcf02d691fec0ad2226d2c24a9b993559f6e92eba16d6961eb0f54d97c70616e6642f6f42982666f3e3443b98bc8a9c53dc356b6fa956e99ae077b
|
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
@@ -1,7 +1,7 @@
|
|
1
1
|
# Sanity
|
2
2
|
|
3
|
-
![](https://github.com/
|
4
|
-
|
3
|
+
![](https://github.com/dvmonroe/sanity-ruby/actions/workflows/ci.yml/badge.svg)
|
4
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/24b96780b59c8a871e6e/maintainability)](https://codeclimate.com/github/dvmonroe/sanity-ruby/maintainability)
|
5
5
|
|
6
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
|
|
@@ -9,7 +9,7 @@ The library also provides other features, like:
|
|
9
9
|
|
10
10
|
- Easy configuration for fast setup and use.
|
11
11
|
- A pre-defined class to help make any PORO a "sanity resource"
|
12
|
-
- Extensibility in overriding the
|
12
|
+
- Extensibility in overriding the serializer for the API response results
|
13
13
|
- A small DSL around GROQ queries
|
14
14
|
|
15
15
|
## Contents
|
@@ -17,6 +17,7 @@ The library also provides other features, like:
|
|
17
17
|
- [Sanity](#sanity)
|
18
18
|
- [Contents](#contents)
|
19
19
|
- [Getting Started](#getting-started)
|
20
|
+
- [Serialization](#serialization)
|
20
21
|
- [Mutating](#mutating)
|
21
22
|
- [Querying](#querying)
|
22
23
|
- [Development](#development)
|
@@ -75,6 +76,20 @@ class User < Sanity::Resource
|
|
75
76
|
end
|
76
77
|
```
|
77
78
|
|
79
|
+
Since `Sanity::Resource` includes `ActiveModel::Model` and
|
80
|
+
`ActiveModel::Attributes`, you're able to define types on attributes and use
|
81
|
+
methods like `alias_attribute`.
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
class User < Sanity::Resource
|
85
|
+
...
|
86
|
+
attribute :name, :string, default: 'John Doe'
|
87
|
+
attribute :_createdAt, :datetime
|
88
|
+
alias_attribute :created_at, :_createdAt
|
89
|
+
...
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
78
93
|
To create a new document in Sanity:
|
79
94
|
|
80
95
|
```ruby
|
@@ -100,6 +115,63 @@ class User
|
|
100
115
|
end
|
101
116
|
```
|
102
117
|
|
118
|
+
## Serialization
|
119
|
+
|
120
|
+
When using a PORO, you can opt-in to automatically serialize your results. You
|
121
|
+
must define all attributes that should be serialized.
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
class User < Sanity::Resource
|
125
|
+
auto_serialize
|
126
|
+
...
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
Additionally, you can configure a custom serializer. See how to define a custom
|
131
|
+
serializer [below](#custom-serializer).
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
class User < Sanity::Resource
|
135
|
+
serializer UserSerializer
|
136
|
+
...
|
137
|
+
end
|
138
|
+
```
|
139
|
+
|
140
|
+
Finally, at query time you can also pass in a serializer. A serializer specified
|
141
|
+
at query time will take priority over any other configuration.
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
User.where(active: true, serializer: UserSerializer)
|
145
|
+
```
|
146
|
+
|
147
|
+
where `UserSerializer` might look like:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
class UserSerializer
|
151
|
+
class << self
|
152
|
+
def call(...)
|
153
|
+
new(...).call
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
attr_reader :results
|
158
|
+
|
159
|
+
def initialize(args)
|
160
|
+
@results = args["result"]
|
161
|
+
end
|
162
|
+
|
163
|
+
def call
|
164
|
+
results.map do |result|
|
165
|
+
User.new(
|
166
|
+
_id: result["_id"],
|
167
|
+
_type: result["_type"]
|
168
|
+
)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
```
|
173
|
+
|
174
|
+
|
103
175
|
## Mutating
|
104
176
|
|
105
177
|
To [create a document](https://www.sanity.io/docs/http-mutations#c732f27330a4):
|
@@ -137,7 +209,7 @@ Sanity::Document.patch(params: { _id: "1234-321", set: { first_name: "Carl" }})
|
|
137
209
|
To [find document(s) by id](https://www.sanity.io/docs/http-doc):
|
138
210
|
|
139
211
|
```ruby
|
140
|
-
Sanity::Document.find(
|
212
|
+
Sanity::Document.find(id: "1234-321")
|
141
213
|
```
|
142
214
|
|
143
215
|
To find documents based on certain fields:
|
@@ -218,15 +290,27 @@ GROQ
|
|
218
290
|
Sanity::Document.where(groq: groq_query, variables: {name: "Monsters, Inc."})
|
219
291
|
```
|
220
292
|
|
293
|
+
|
221
294
|
## Development
|
222
295
|
|
223
296
|
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.
|
224
297
|
|
225
298
|
To install this gem onto your local machine, run `bundle exec rake install`.
|
226
299
|
|
300
|
+
### Testing across all supported versions:
|
301
|
+
|
302
|
+
To run tests across all gem supported ruby versions (requires Docker):
|
303
|
+
```sh
|
304
|
+
bin/dev-test
|
305
|
+
```
|
306
|
+
To run lint across all gem supported ruby versions (requires Docker):
|
307
|
+
```sh
|
308
|
+
bin/dev-lint
|
309
|
+
```
|
310
|
+
|
227
311
|
## Contributing
|
228
312
|
|
229
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
313
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/dvmonroe/sanity-ruby.
|
230
314
|
|
231
315
|
## License
|
232
316
|
|
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
@@ -39,7 +39,7 @@ module Sanity
|
|
39
39
|
def initialize(**args)
|
40
40
|
self.class.default_attributes.merge(args).then do |attrs|
|
41
41
|
attrs.each do |key, val|
|
42
|
-
define_singleton_method("#{key}=") do |val|
|
42
|
+
define_singleton_method(:"#{key}=") do |val|
|
43
43
|
args[key] = val
|
44
44
|
attributes[key] = val
|
45
45
|
end
|
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" # rubocop:disable Lint/RedundantRequireStatement
|
3
4
|
using Sanity::Refinements::Strings
|
4
5
|
using Sanity::Refinements::Arrays
|
5
6
|
|
@@ -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,10 +65,15 @@ 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
|
@@ -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
@@ -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
|
@@ -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/queryable.rb
CHANGED
@@ -36,15 +36,20 @@ module Sanity
|
|
36
36
|
|
37
37
|
private
|
38
38
|
|
39
|
-
# @private
|
40
39
|
def queryable(**options)
|
41
40
|
options.fetch(:only, DEFAULT_KLASS_QUERIES).each do |query|
|
42
|
-
|
43
|
-
Module.const_get("Sanity::Http::#{query.to_s.classify}").call(**args.merge(resource_klass: self))
|
44
|
-
end
|
45
|
-
define_singleton_method("#{query}_api_endpoint") { QUERY_ENDPOINTS[query] }
|
41
|
+
define_query_method(query)
|
46
42
|
end
|
47
43
|
end
|
44
|
+
|
45
|
+
def define_query_method(query)
|
46
|
+
define_singleton_method(query) do |**args|
|
47
|
+
default_type = args.key?(:_type) ? args[:_type] : Sanity::TypeHelper.default_type(self)
|
48
|
+
default_args = {resource_klass: self, _type: default_type}.compact
|
49
|
+
Module.const_get("Sanity::Http::#{query.to_s.classify}").call(**default_args.merge(args))
|
50
|
+
end
|
51
|
+
define_singleton_method(:"#{query}_api_endpoint") { QUERY_ENDPOINTS[query] }
|
52
|
+
end
|
48
53
|
end
|
49
54
|
end
|
50
55
|
end
|
data/lib/sanity/resource.rb
CHANGED
@@ -5,9 +5,9 @@ module Sanity
|
|
5
5
|
# the sanity resources defined within this gem.
|
6
6
|
#
|
7
7
|
# Out of the box it includes the following mixins:
|
8
|
-
# Sanity::Attributable
|
9
8
|
# Sanity::Mutatable
|
10
9
|
# Sanity::Queryable
|
10
|
+
# Sanity::Serializable
|
11
11
|
#
|
12
12
|
# Sanity::Document and Sanity::Asset both inherit
|
13
13
|
# from Sanity::Resource
|
@@ -23,8 +23,11 @@ module Sanity
|
|
23
23
|
# end
|
24
24
|
#
|
25
25
|
class Resource
|
26
|
-
include
|
26
|
+
include ActiveModel::Model
|
27
|
+
include ActiveModel::Attributes
|
28
|
+
|
27
29
|
include Sanity::Mutatable
|
28
30
|
include Sanity::Queryable
|
31
|
+
include Sanity::Serializable
|
29
32
|
end
|
30
33
|
end
|
@@ -0,0 +1,62 @@
|
|
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
|
+
key = results.key?("result") ? "result" : "documents"
|
54
|
+
results[key].map do |result|
|
55
|
+
attributes = result.slice(*attribute_names)
|
56
|
+
new(**attributes.transform_keys(&:to_sym))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/sanity/version.rb
CHANGED
data/lib/sanity.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_model"
|
3
4
|
require "forwardable"
|
4
5
|
require "sanity/refinements"
|
5
6
|
|
@@ -8,14 +9,17 @@ require "sanity/configuration"
|
|
8
9
|
|
9
10
|
require "sanity/groqify"
|
10
11
|
require "sanity/http"
|
12
|
+
require "sanity/helpers"
|
11
13
|
|
12
|
-
require "sanity/attributable"
|
13
14
|
require "sanity/mutatable"
|
14
15
|
require "sanity/queryable"
|
16
|
+
require "sanity/serializable"
|
15
17
|
|
16
18
|
require "sanity/resource"
|
17
19
|
require "sanity/resources"
|
18
20
|
|
19
21
|
module Sanity
|
20
22
|
class Error < StandardError; end
|
23
|
+
|
24
|
+
RESULT_WRAPPER_DEPRECATION_WARNING = "DEPRECATION: `result_wrapper` is deprecated. Please use `serializer` instead."
|
21
25
|
end
|
data/sanity.gemspec
CHANGED
@@ -5,12 +5,12 @@ require_relative "lib/sanity/version"
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "sanity-ruby"
|
7
7
|
spec.version = Sanity::VERSION
|
8
|
-
spec.authors = "
|
9
|
-
spec.email = "
|
8
|
+
spec.authors = "Drew Monroe"
|
9
|
+
spec.email = "drew@pinecreeklabs.com"
|
10
10
|
|
11
11
|
spec.summary = "Ruby bindings for the Sanity API"
|
12
12
|
spec.description = ""
|
13
|
-
spec.homepage = "https://github.com/
|
13
|
+
spec.homepage = "https://github.com/dvmonroe/sanity-ruby"
|
14
14
|
spec.license = "MIT"
|
15
15
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
|
16
16
|
|
@@ -26,4 +26,6 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.bindir = "exe"
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
28
|
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "activemodel"
|
29
31
|
end
|
metadata
CHANGED
@@ -1,31 +1,52 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sanity-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Drew Monroe
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
11
|
+
date: 2024-02-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
description: ''
|
14
|
-
email:
|
28
|
+
email: drew@pinecreeklabs.com
|
15
29
|
executables: []
|
16
30
|
extensions: []
|
17
31
|
extra_rdoc_files: []
|
18
32
|
files:
|
33
|
+
- ".dockerignore"
|
19
34
|
- ".github/workflows/ci.yml"
|
20
35
|
- ".gitignore"
|
21
36
|
- ".standard.yml"
|
37
|
+
- Dockerfile-2.6
|
38
|
+
- Dockerfile-2.7
|
39
|
+
- Dockerfile-3.0
|
22
40
|
- Gemfile
|
23
41
|
- LICENSE.txt
|
24
42
|
- README.md
|
25
43
|
- Rakefile
|
26
44
|
- bin/console
|
45
|
+
- bin/dev-lint
|
46
|
+
- bin/dev-test
|
27
47
|
- bin/setup
|
28
48
|
- bin/standardrb
|
49
|
+
- docker-compose.yml
|
29
50
|
- lib/sanity-ruby.rb
|
30
51
|
- lib/sanity.rb
|
31
52
|
- lib/sanity/attributable.rb
|
@@ -35,6 +56,8 @@ files:
|
|
35
56
|
- lib/sanity/groq/select.rb
|
36
57
|
- lib/sanity/groq/slice.rb
|
37
58
|
- lib/sanity/groqify.rb
|
59
|
+
- lib/sanity/helpers.rb
|
60
|
+
- lib/sanity/helpers/type_helper.rb
|
38
61
|
- lib/sanity/http.rb
|
39
62
|
- lib/sanity/http/create.rb
|
40
63
|
- lib/sanity/http/create_if_not_exists.rb
|
@@ -56,15 +79,16 @@ files:
|
|
56
79
|
- lib/sanity/resources.rb
|
57
80
|
- lib/sanity/resources/asset.rb
|
58
81
|
- lib/sanity/resources/document.rb
|
82
|
+
- lib/sanity/serializable.rb
|
59
83
|
- lib/sanity/version.rb
|
60
84
|
- sanity.gemspec
|
61
|
-
homepage: https://github.com/
|
85
|
+
homepage: https://github.com/dvmonroe/sanity-ruby
|
62
86
|
licenses:
|
63
87
|
- MIT
|
64
88
|
metadata:
|
65
|
-
homepage_uri: https://github.com/
|
66
|
-
source_code_uri: https://github.com/
|
67
|
-
changelog_uri: https://github.com/
|
89
|
+
homepage_uri: https://github.com/dvmonroe/sanity-ruby
|
90
|
+
source_code_uri: https://github.com/dvmonroe/sanity-ruby
|
91
|
+
changelog_uri: https://github.com/dvmonroe/sanity-ruby
|
68
92
|
post_install_message:
|
69
93
|
rdoc_options: []
|
70
94
|
require_paths:
|
@@ -80,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
104
|
- !ruby/object:Gem::Version
|
81
105
|
version: '0'
|
82
106
|
requirements: []
|
83
|
-
rubygems_version: 3.
|
107
|
+
rubygems_version: 3.4.17
|
84
108
|
signing_key:
|
85
109
|
specification_version: 4
|
86
110
|
summary: Ruby bindings for the Sanity API
|