sanity-ruby 0.4.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/.github/workflows/ci.yml +1 -1
- data/Dockerfile-3.3 +30 -0
- data/README.md +64 -1
- data/bin/dev-lint +4 -1
- data/bin/dev-test +4 -1
- data/docker-compose.yml +13 -2
- data/lib/sanity/configuration.rb +29 -14
- data/lib/sanity/http/publication.rb +68 -0
- data/lib/sanity/http/publish.rb +9 -0
- data/lib/sanity/http/unpublish.rb +9 -0
- data/lib/sanity/http.rb +4 -0
- data/lib/sanity/mutatable.rb +3 -2
- data/lib/sanity/publishable.rb +50 -0
- data/lib/sanity/resource.rb +2 -0
- data/lib/sanity/resources/document.rb +1 -0
- data/lib/sanity/version.rb +1 -1
- data/lib/sanity.rb +1 -0
- metadata +11 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8cb8ebee529e3f7aa924f79ef9db8971f2e02ff727f307a0af4c2e01cd778afc
|
4
|
+
data.tar.gz: ac53d38a057a8e830ccfed73032d751bdf3f49f98e70b835b7d4b108a31ef2a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c613cd3d0dc68006d80f0a52b40a2884d654e77357e516b30e01ff78519360ca592fa8ea0c06962792daad9aac7d9841d83a7ee11c8c73d07c588e75730a87ae
|
7
|
+
data.tar.gz: 799a99a83a17e76526efa652207f1aad6756c01574a2dd63b786c9a3af75192ca5c6b10ca2ffaba602d275fdbf9ef7ee38996af258364406ca5b7a6818ef3855
|
data/.github/workflows/ci.yml
CHANGED
data/Dockerfile-3.3
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
FROM ruby:3.3-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/README.md
CHANGED
@@ -35,6 +35,52 @@ gem 'sanity-ruby'
|
|
35
35
|
Setup your configuration. If using in Rails, consider setting this in an initializer:
|
36
36
|
|
37
37
|
```ruby
|
38
|
+
# config/initializers/sanity.rb
|
39
|
+
Sanity.configure do |s|
|
40
|
+
s.token = "yoursupersecrettoken"
|
41
|
+
s.api_version = "v2021-03-25"
|
42
|
+
s.project_id = "1234"
|
43
|
+
s.dataset = "development"
|
44
|
+
s.use_cdn = false
|
45
|
+
end
|
46
|
+
|
47
|
+
# OR
|
48
|
+
|
49
|
+
# Sanity.configure do |s|
|
50
|
+
# s.token = ENV.fetch("SANITY_TOKEN", "")
|
51
|
+
# s.api_version = ENV.fetch("SANITY_API_VERSION", "")
|
52
|
+
# s.project_id = ENV.fetch("SANITY_PROJECT_ID", "")
|
53
|
+
# s.dataset = ENV.fetch("SANITY_DATASET", "")
|
54
|
+
# s.use_cdn = ENV.fetch("SANITY_USE_CDN", false)
|
55
|
+
# end
|
56
|
+
```
|
57
|
+
|
58
|
+
or you can set the following ENV variables at runtime without any initializer:
|
59
|
+
|
60
|
+
```bash
|
61
|
+
SANITY_TOKEN="yoursupersecrettoken"
|
62
|
+
SANITY_API_VERSION="v2021-03-25"
|
63
|
+
SANITY_PROJECT_ID="1234"
|
64
|
+
SANITY_DATASET="development"
|
65
|
+
SANITY_USE_CDN="false"
|
66
|
+
```
|
67
|
+
|
68
|
+
The configuration object is thread safe by default meaning you can connect to multiple different projects and/or API variations across any number of threads. A real world scenario when working with Sanity may require that you sometimes interact with the [CDN based API](https://www.sanity.io/docs/api-cdn) and sometimes the non-CDN based API. Using ENV variables combined with the thread safe configuration object gives you the ultimate flexibility.
|
69
|
+
|
70
|
+
If you're using this gem in a Rails application AND you're interacting with only ONE set of configuration you can make the gem use the global configuration by setting the `use_global_config` option to `true`.
|
71
|
+
|
72
|
+
Your initializer `config/initializers/sanity.rb` should look like:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
# `use_global_config` is NOT thread safe. DO NOT use if you intend on changing the
|
76
|
+
# config object at anytime within your application's lifecycle.
|
77
|
+
#
|
78
|
+
# Do not use `use_global_config` in your application if you're:
|
79
|
+
# - Interacting with various Sanity project ids/token
|
80
|
+
# - Interacting with multiple API versions
|
81
|
+
# - Interacting with calls that sometimes require the use of the CDN and sometimes don't
|
82
|
+
|
83
|
+
Sanity.use_global_config = true
|
38
84
|
Sanity.configure do |s|
|
39
85
|
s.token = "yoursupersecrettoken"
|
40
86
|
s.api_version = "v2021-03-25"
|
@@ -70,12 +116,15 @@ To make any PORO a sanity resource:
|
|
70
116
|
```ruby
|
71
117
|
class User < Sanity::Resource
|
72
118
|
attribute :_id, default: ""
|
73
|
-
attribute :_type
|
119
|
+
attribute :_type, default: ""
|
74
120
|
mutatable only: %i(create delete)
|
75
121
|
queryable
|
122
|
+
publishable
|
76
123
|
end
|
77
124
|
```
|
78
125
|
|
126
|
+
|
127
|
+
|
79
128
|
Since `Sanity::Resource` includes `ActiveModel::Model` and
|
80
129
|
`ActiveModel::Attributes`, you're able to define types on attributes and use
|
81
130
|
methods like `alias_attribute`.
|
@@ -204,6 +253,20 @@ To [patch a document](https://www.sanity.io/docs/http-mutations#2f480b2baca5):
|
|
204
253
|
Sanity::Document.patch(params: { _id: "1234-321", set: { first_name: "Carl" }})
|
205
254
|
```
|
206
255
|
|
256
|
+
## Publishing
|
257
|
+
|
258
|
+
To [publish a document](https://www.sanity.io/docs/scheduling-api#dcb47be520d0):
|
259
|
+
|
260
|
+
```ruby
|
261
|
+
Sanity::Document.publish(["1234-321"])
|
262
|
+
```
|
263
|
+
|
264
|
+
To [unpublish a document](https://www.sanity.io/docs/scheduling-api#64f3de350651):
|
265
|
+
|
266
|
+
```ruby
|
267
|
+
Sanity::Document.unpublish(["1234-321", "1432432-545"])
|
268
|
+
```
|
269
|
+
|
207
270
|
## Querying
|
208
271
|
|
209
272
|
To [find document(s) by id](https://www.sanity.io/docs/http-doc):
|
data/bin/dev-lint
CHANGED
@@ -1,3 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
system "docker-compose run --rm 2.6 bin/standardrb &&
|
3
|
+
system "docker-compose run --rm 2.6 bin/standardrb && \
|
4
|
+
docker-compose run --rm 2.7 bin/standardrb && \
|
5
|
+
docker-compose run --rm 3.0 bin/standardrb && \
|
6
|
+
docker-compose run --rm 3.3 bin/standardrb"
|
data/bin/dev-test
CHANGED
@@ -1,3 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
system "docker-compose run --rm 2.6 bundle exec rake test &&
|
3
|
+
system "docker-compose run --rm 2.6 bundle exec rake test && \
|
4
|
+
docker-compose run --rm 2.7 bundle exec rake test && \
|
5
|
+
docker-compose run --rm 3.0 bundle exec rake test && \
|
6
|
+
docker-compose run --rm 3.3 bundle exec rake test"
|
data/docker-compose.yml
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
version: '3.9'
|
2
|
-
|
3
1
|
services:
|
4
2
|
'2.6':
|
5
3
|
build:
|
@@ -40,6 +38,19 @@ services:
|
|
40
38
|
container_name: sanity-ruby-3.0
|
41
39
|
command: bash
|
42
40
|
|
41
|
+
'3.3':
|
42
|
+
build:
|
43
|
+
context: .
|
44
|
+
dockerfile: Dockerfile-3.3
|
45
|
+
tty: true
|
46
|
+
stdin_open: true
|
47
|
+
volumes:
|
48
|
+
- ./bin:/var/www/sanity-ruby/bin/
|
49
|
+
- ./lib:/var/www/sanity-ruby/lib/
|
50
|
+
- ./test:/var/www/sanity-ruby/test/
|
51
|
+
container_name: sanity-ruby-3.3
|
52
|
+
command: bash
|
53
|
+
|
43
54
|
volumes:
|
44
55
|
bin:
|
45
56
|
lib:
|
data/lib/sanity/configuration.rb
CHANGED
@@ -18,32 +18,47 @@ module Sanity
|
|
18
18
|
attr_accessor :use_cdn
|
19
19
|
|
20
20
|
def initialize
|
21
|
-
@project_id = ""
|
22
|
-
@dataset = ""
|
23
|
-
@api_version = ""
|
24
|
-
@token = ""
|
25
|
-
@use_cdn = false
|
21
|
+
@project_id = ENV.fetch("SANITY_PROJECT_ID", "")
|
22
|
+
@dataset = ENV.fetch("SANITY_DATASET", "")
|
23
|
+
@api_version = ENV.fetch("SANITY_API_VERSION", "")
|
24
|
+
@token = ENV.fetch("SANITY_TOKEN", "")
|
25
|
+
@use_cdn = ENV.fetch("SANITY_USE_CDN", false) == "true"
|
26
26
|
end
|
27
27
|
|
28
28
|
# @return [String] Api subdomain based on use_cdn flag
|
29
29
|
def api_subdomain
|
30
30
|
use_cdn ? "apicdn" : "api"
|
31
31
|
end
|
32
|
-
end
|
33
32
|
|
34
|
-
|
35
|
-
|
33
|
+
def to_h
|
34
|
+
instance_variables.each_with_object({}) do |var, obj|
|
35
|
+
obj[var.to_s.delete("@").to_sym] = instance_variable_get(var)
|
36
|
+
end
|
37
|
+
end
|
36
38
|
end
|
37
39
|
|
38
40
|
class << self
|
41
|
+
attr_accessor :use_global_config
|
42
|
+
|
43
|
+
def configuration
|
44
|
+
if use_global_config
|
45
|
+
@configuration ||= Configuration.new
|
46
|
+
else
|
47
|
+
Thread.current[:sanity_configuration] ||= Configuration.new
|
48
|
+
end
|
49
|
+
end
|
39
50
|
alias_method :config, :configuration
|
40
|
-
end
|
41
51
|
|
42
|
-
|
43
|
-
|
44
|
-
|
52
|
+
def configuration=(config)
|
53
|
+
if use_global_config
|
54
|
+
@configuration = config
|
55
|
+
else
|
56
|
+
Thread.current[:sanity_configuration] = config
|
57
|
+
end
|
58
|
+
end
|
45
59
|
|
46
|
-
|
47
|
-
|
60
|
+
def configure
|
61
|
+
yield configuration
|
62
|
+
end
|
48
63
|
end
|
49
64
|
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
using Sanity::Refinements::Strings
|
4
|
+
using Sanity::Refinements::Arrays
|
5
|
+
|
6
|
+
module Sanity
|
7
|
+
module Http
|
8
|
+
module Publication
|
9
|
+
class << self
|
10
|
+
def included(base)
|
11
|
+
base.extend(ClassMethods)
|
12
|
+
base.extend(Forwardable)
|
13
|
+
base.delegate(%i[project_id dataset token] => :"Sanity.config")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def call(args)
|
19
|
+
new(args).call
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_reader :args
|
24
|
+
|
25
|
+
def initialize(args)
|
26
|
+
unless args.is_a?(String) || args.is_a?(Array)
|
27
|
+
raise ArgumentError, "args must be a string or an array"
|
28
|
+
end
|
29
|
+
@args = Array.wrap(args)
|
30
|
+
end
|
31
|
+
|
32
|
+
def body_key
|
33
|
+
"documentId"
|
34
|
+
end
|
35
|
+
|
36
|
+
def call
|
37
|
+
Net::HTTP.post(uri, body.to_json, headers).then do |result|
|
38
|
+
block_given? ? yield(result) : result
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def publication_path
|
43
|
+
self.class.name.demodulize.underscore.camelize_lower
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def base_url
|
49
|
+
"https://api.sanity.io/v1/#{publication_path}/#{project_id}/#{dataset}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def body
|
53
|
+
Array.wrap(args.map { |arg| {"#{body_key}": arg} })
|
54
|
+
end
|
55
|
+
|
56
|
+
def headers
|
57
|
+
{
|
58
|
+
"Content-Type": "application/json",
|
59
|
+
Authorization: "Bearer #{token}"
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
def uri
|
64
|
+
URI(base_url)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/lib/sanity/http.rb
CHANGED
@@ -6,6 +6,7 @@ require "uri"
|
|
6
6
|
|
7
7
|
require "sanity/http/mutation"
|
8
8
|
require "sanity/http/query"
|
9
|
+
require "sanity/http/publication"
|
9
10
|
|
10
11
|
require "sanity/http/create"
|
11
12
|
require "sanity/http/create_if_not_exists"
|
@@ -13,6 +14,9 @@ require "sanity/http/create_or_replace"
|
|
13
14
|
require "sanity/http/delete"
|
14
15
|
require "sanity/http/patch"
|
15
16
|
|
17
|
+
require "sanity/http/publish"
|
18
|
+
require "sanity/http/unpublish"
|
19
|
+
|
16
20
|
require "sanity/http/find"
|
17
21
|
require "sanity/http/where"
|
18
22
|
|
data/lib/sanity/mutatable.rb
CHANGED
@@ -26,8 +26,9 @@ module Sanity
|
|
26
26
|
end
|
27
27
|
|
28
28
|
module ClassMethods
|
29
|
-
|
30
|
-
|
29
|
+
COMMON_MUTATIONS = %i[create create_or_replace create_if_not_exists delete].freeze
|
30
|
+
DEFAULT_KLASS_MUTATIONS = (COMMON_MUTATIONS + [:patch]).freeze
|
31
|
+
DEFAULT_INSTANCE_MUTATIONS = COMMON_MUTATIONS
|
31
32
|
ALL_MUTATIONS = DEFAULT_KLASS_MUTATIONS | DEFAULT_INSTANCE_MUTATIONS
|
32
33
|
|
33
34
|
private
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sanity
|
4
|
+
# Publishable is responsible for setting the appropriate class methods
|
5
|
+
# that invoke Sanity::Http's publishable actions
|
6
|
+
#
|
7
|
+
# The publishable macro can limit what actions are accessible to the
|
8
|
+
# publishable object
|
9
|
+
#
|
10
|
+
# @example provides default class methods
|
11
|
+
# publishable
|
12
|
+
#
|
13
|
+
# @example only add the `.publish` method
|
14
|
+
# publishable only: %i(publish)
|
15
|
+
#
|
16
|
+
# @example only add the `.unpublish`& `#unpublish` methods
|
17
|
+
# publishable only: %i(unpublish)
|
18
|
+
#
|
19
|
+
using Sanity::Refinements::Strings
|
20
|
+
|
21
|
+
module Publishable
|
22
|
+
class << self
|
23
|
+
def included(base)
|
24
|
+
base.extend(ClassMethods)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module ClassMethods
|
29
|
+
PUBLISHABLE_ACTIONS = %i[publish unpublish].freeze
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def publishable(**options)
|
34
|
+
options.fetch(:only, PUBLISHABLE_ACTIONS).each do |action|
|
35
|
+
if PUBLISHABLE_ACTIONS.include? action.to_sym
|
36
|
+
define_singleton_method(action) do |args|
|
37
|
+
Module.const_get("Sanity::Http::#{action.to_s.classify}").call(args)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
if PUBLISHABLE_ACTIONS.include? action.to_sym
|
42
|
+
define_method(action) do
|
43
|
+
Module.const_get("Sanity::Http::#{action.to_s.classify}").call(attributes["_id"])
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/sanity/resource.rb
CHANGED
data/lib/sanity/version.rb
CHANGED
data/lib/sanity.rb
CHANGED
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.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Drew Monroe
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- Dockerfile-2.6
|
38
38
|
- Dockerfile-2.7
|
39
39
|
- Dockerfile-3.0
|
40
|
+
- Dockerfile-3.3
|
40
41
|
- Gemfile
|
41
42
|
- LICENSE.txt
|
42
43
|
- README.md
|
@@ -66,10 +67,14 @@ files:
|
|
66
67
|
- lib/sanity/http/find.rb
|
67
68
|
- lib/sanity/http/mutation.rb
|
68
69
|
- lib/sanity/http/patch.rb
|
70
|
+
- lib/sanity/http/publication.rb
|
71
|
+
- lib/sanity/http/publish.rb
|
69
72
|
- lib/sanity/http/query.rb
|
70
73
|
- lib/sanity/http/results.rb
|
74
|
+
- lib/sanity/http/unpublish.rb
|
71
75
|
- lib/sanity/http/where.rb
|
72
76
|
- lib/sanity/mutatable.rb
|
77
|
+
- lib/sanity/publishable.rb
|
73
78
|
- lib/sanity/queryable.rb
|
74
79
|
- lib/sanity/refinements.rb
|
75
80
|
- lib/sanity/refinements/arrays.rb
|
@@ -89,7 +94,7 @@ metadata:
|
|
89
94
|
homepage_uri: https://github.com/dvmonroe/sanity-ruby
|
90
95
|
source_code_uri: https://github.com/dvmonroe/sanity-ruby
|
91
96
|
changelog_uri: https://github.com/dvmonroe/sanity-ruby
|
92
|
-
post_install_message:
|
97
|
+
post_install_message:
|
93
98
|
rdoc_options: []
|
94
99
|
require_paths:
|
95
100
|
- lib
|
@@ -104,8 +109,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
109
|
- !ruby/object:Gem::Version
|
105
110
|
version: '0'
|
106
111
|
requirements: []
|
107
|
-
rubygems_version: 3.
|
108
|
-
signing_key:
|
112
|
+
rubygems_version: 3.0.3.1
|
113
|
+
signing_key:
|
109
114
|
specification_version: 4
|
110
115
|
summary: Ruby bindings for the Sanity API
|
111
116
|
test_files: []
|