sanity-ruby 0.5.0 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a6bf214154655cc746cb50c817499ddef2dc104915633814fb4bbb60dbcf18d6
4
- data.tar.gz: 0e906831bd32ae8b98431adb107a1aca54ea0b6baa30327807cb0764f0a35c25
3
+ metadata.gz: 8eac71da998761b24763c53a7d5751006738582374052c9a306240d3e06c8a90
4
+ data.tar.gz: 7f00d6937d416db65496fc130219f07e4663809acdf9c98f3de73a53b4ae60cb
5
5
  SHA512:
6
- metadata.gz: 99db43870e1f4ac67f63e46b142545c5588657d93eaee2eb007352a33646f36f5d7b31d1992b8dbca12eb98f92b9bab27ae8c0122637b3d0a3815f3eb1543b02
7
- data.tar.gz: 9c09be7fec031eda815290b3e6dab1c2e080a7cb053eaf0274989181661d8e9f84c264ef798c7d17ee9b828a1fd2abae9fe17547f137d1f6dcb167768d5425eb
6
+ metadata.gz: 7e1b8494784ef0076d78e0317c9627e4ff9eba2d132a3d8cb263ca48e410d978594b8186133fb2ba4e0f3431ee986d6ec13554ffb0ca15c5811c2e34a04e79ee
7
+ data.tar.gz: 876b992ee2bb568d79a10588bf8ee443d301b2d9a386a12de9c2373e0f4d5dda211f9b4639a2445d59409c035b86c7ee3cef8dac38056dd0a233cbde768b8b36
@@ -6,7 +6,7 @@ jobs:
6
6
  strategy:
7
7
  matrix:
8
8
  # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
9
- ruby: [2.6, 2.7, "3.0"]
9
+ ruby: [2.6, 2.7, "3.0", "3.3"]
10
10
  steps:
11
11
  - uses: actions/checkout@v2
12
12
  - uses: ruby/setup-ruby@v1
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/Gemfile CHANGED
@@ -14,3 +14,9 @@ gem "guard-minitest"
14
14
  gem "yard"
15
15
 
16
16
  gem "standard"
17
+
18
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0")
19
+ gem "ffi", "~> 1.16.3"
20
+ else
21
+ gem "ffi"
22
+ end
data/README.md CHANGED
@@ -12,6 +12,12 @@ The library also provides other features, like:
12
12
  - Extensibility in overriding the serializer for the API response results
13
13
  - A small DSL around GROQ queries
14
14
 
15
+ > [!NOTE]
16
+ > This gem was originally developed in early 2021 to facilitate Morning Brew's content migration from Rails to Sanity. It was subsequently used to enable interaction between Morning Brew's Rails-based Advertising CMS and their Sanity-based Editorial CMS for another ~year. The gem is no longer actively used in production as the Rails applications have since been deprecated, but it remains available as an open-source solution for Ruby - Sanity integrations.
17
+
18
+ > [!WARNING]
19
+ > If you're looking for a way to embed Sanity Studio within a Rails application, this gem is not the solution.
20
+
15
21
  ## Contents
16
22
 
17
23
  - [Sanity](#sanity)
@@ -35,6 +41,7 @@ gem 'sanity-ruby'
35
41
  Setup your configuration. If using in Rails, consider setting this in an initializer:
36
42
 
37
43
  ```ruby
44
+ # config/initializers/sanity.rb
38
45
  Sanity.configure do |s|
39
46
  s.token = "yoursupersecrettoken"
40
47
  s.api_version = "v2021-03-25"
@@ -42,9 +49,19 @@ Sanity.configure do |s|
42
49
  s.dataset = "development"
43
50
  s.use_cdn = false
44
51
  end
52
+
53
+ # OR
54
+
55
+ # Sanity.configure do |s|
56
+ # s.token = ENV.fetch("SANITY_TOKEN", "")
57
+ # s.api_version = ENV.fetch("SANITY_API_VERSION", "")
58
+ # s.project_id = ENV.fetch("SANITY_PROJECT_ID", "")
59
+ # s.dataset = ENV.fetch("SANITY_DATASET", "")
60
+ # s.use_cdn = ENV.fetch("SANITY_USE_CDN", false)
61
+ # end
45
62
  ```
46
63
 
47
- or you can set the following ENV variables at runtime:
64
+ or you can set the following ENV variables at runtime without any initializer:
48
65
 
49
66
  ```bash
50
67
  SANITY_TOKEN="yoursupersecrettoken"
@@ -54,7 +71,30 @@ SANITY_DATASET="development"
54
71
  SANITY_USE_CDN="false"
55
72
  ```
56
73
 
57
- The configuration object is thread safe meaning you can connect to multiple different projects across multiple threads. This may be useful if your application is interacting with multiple different Sanity projects.
74
+ 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.
75
+
76
+ 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`.
77
+
78
+ Your initializer `config/initializers/sanity.rb` should look like:
79
+
80
+ ```ruby
81
+ # `use_global_config` is NOT thread safe. DO NOT use if you intend on changing the
82
+ # config object at anytime within your application's lifecycle.
83
+ #
84
+ # Do not use `use_global_config` in your application if you're:
85
+ # - Interacting with various Sanity project ids/token
86
+ # - Interacting with multiple API versions
87
+ # - Interacting with calls that sometimes require the use of the CDN and sometimes don't
88
+
89
+ Sanity.use_global_config = true
90
+ Sanity.configure do |s|
91
+ s.token = "yoursupersecrettoken"
92
+ s.api_version = "v2021-03-25"
93
+ s.project_id = "1234"
94
+ s.dataset = "development"
95
+ s.use_cdn = false
96
+ end
97
+ ```
58
98
 
59
99
  To create a new document:
60
100
 
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 && docker-compose run --rm 2.7 bin/standardrb && docker-compose run --rm 3.0 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 && docker-compose run --rm 2.7 bundle exec rake test && docker-compose run --rm 3.0 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:
@@ -29,21 +29,36 @@ module Sanity
29
29
  def api_subdomain
30
30
  use_cdn ? "apicdn" : "api"
31
31
  end
32
- end
33
32
 
34
- def self.configuration
35
- Thread.current[:sanity_configuration] ||= Configuration.new
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
- def self.configuration=(config)
43
- Thread.current[:sanity_configuration] = config
44
- end
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
- def self.configure
47
- yield configuration
60
+ def configure
61
+ yield configuration
62
+ end
48
63
  end
49
64
  end
@@ -41,7 +41,7 @@ module Sanity
41
41
  else
42
42
  {}.tap do |hash|
43
43
  variables.each do |key, value|
44
- hash["$#{key}"] = "\"#{value}\""
44
+ hash["$#{key}"] = serialize_variable_value(value)
45
45
  end
46
46
  end
47
47
  end.merge(query: groq_query)
@@ -56,6 +56,17 @@ module Sanity
56
56
 
57
57
  query_and_variables.to_json
58
58
  end
59
+
60
+ def serialize_variable_value(value)
61
+ case value
62
+ when String
63
+ "\"#{value}\""
64
+ when Array, Hash
65
+ value.to_json
66
+ else
67
+ value.to_s
68
+ end
69
+ end
59
70
  end
60
71
  end
61
72
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sanity
4
- VERSION = "0.5.0"
4
+ VERSION = "0.7.0"
5
5
  end
data/lib/sanity.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "logger"
3
4
  require "active_model"
4
5
  require "forwardable"
5
6
  require "sanity/refinements"
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.5.0
4
+ version: 0.7.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: 2024-04-25 00:00:00.000000000 Z
11
+ date: 2025-03-24 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