sanity-ruby 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e02c772808fc5112f526e0e8bd329a829dd4ba0e2395d82a83d34f8419a05e66
4
- data.tar.gz: c2b80c8bee2242e68ec2b814fc18e11ad699687a2174889cdf4a7b34823405f2
3
+ metadata.gz: a6bf214154655cc746cb50c817499ddef2dc104915633814fb4bbb60dbcf18d6
4
+ data.tar.gz: 0e906831bd32ae8b98431adb107a1aca54ea0b6baa30327807cb0764f0a35c25
5
5
  SHA512:
6
- metadata.gz: e265993399cfa3c24f821cac9625daaf763a0468353d18f0fac375231bc9e62e727f2a3314bbf55fc7aaa3b5d51f733822cc16409489bf6f3aebbff75ba5c72d
7
- data.tar.gz: 5bf5fbfdb1fcf02d691fec0ad2226d2c24a9b993559f6e92eba16d6961eb0f54d97c70616e6642f6f42982666f3e3443b98bc8a9c53dc356b6fa956e99ae077b
6
+ metadata.gz: 99db43870e1f4ac67f63e46b142545c5588657d93eaee2eb007352a33646f36f5d7b31d1992b8dbca12eb98f92b9bab27ae8c0122637b3d0a3815f3eb1543b02
7
+ data.tar.gz: 9c09be7fec031eda815290b3e6dab1c2e080a7cb053eaf0274989181661d8e9f84c264ef798c7d17ee9b828a1fd2abae9fe17547f137d1f6dcb167768d5425eb
data/README.md CHANGED
@@ -44,6 +44,18 @@ Sanity.configure do |s|
44
44
  end
45
45
  ```
46
46
 
47
+ or you can set the following ENV variables at runtime:
48
+
49
+ ```bash
50
+ SANITY_TOKEN="yoursupersecrettoken"
51
+ SANITY_API_VERSION="v2021-03-25"
52
+ SANITY_PROJECT_ID="1234"
53
+ SANITY_DATASET="development"
54
+ SANITY_USE_CDN="false"
55
+ ```
56
+
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.
58
+
47
59
  To create a new document:
48
60
 
49
61
  ```ruby
@@ -70,12 +82,15 @@ To make any PORO a sanity resource:
70
82
  ```ruby
71
83
  class User < Sanity::Resource
72
84
  attribute :_id, default: ""
73
- attribute :_type: default: ""
85
+ attribute :_type, default: ""
74
86
  mutatable only: %i(create delete)
75
87
  queryable
88
+ publishable
76
89
  end
77
90
  ```
78
91
 
92
+
93
+
79
94
  Since `Sanity::Resource` includes `ActiveModel::Model` and
80
95
  `ActiveModel::Attributes`, you're able to define types on attributes and use
81
96
  methods like `alias_attribute`.
@@ -204,6 +219,20 @@ To [patch a document](https://www.sanity.io/docs/http-mutations#2f480b2baca5):
204
219
  Sanity::Document.patch(params: { _id: "1234-321", set: { first_name: "Carl" }})
205
220
  ```
206
221
 
222
+ ## Publishing
223
+
224
+ To [publish a document](https://www.sanity.io/docs/scheduling-api#dcb47be520d0):
225
+
226
+ ```ruby
227
+ Sanity::Document.publish(["1234-321"])
228
+ ```
229
+
230
+ To [unpublish a document](https://www.sanity.io/docs/scheduling-api#64f3de350651):
231
+
232
+ ```ruby
233
+ Sanity::Document.unpublish(["1234-321", "1432432-545"])
234
+ ```
235
+
207
236
  ## Querying
208
237
 
209
238
  To [find document(s) by id](https://www.sanity.io/docs/http-doc):
@@ -18,11 +18,11 @@ 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
@@ -32,7 +32,7 @@ module Sanity
32
32
  end
33
33
 
34
34
  def self.configuration
35
- @configuration ||= Configuration.new
35
+ Thread.current[:sanity_configuration] ||= Configuration.new
36
36
  end
37
37
 
38
38
  class << self
@@ -40,7 +40,7 @@ module Sanity
40
40
  end
41
41
 
42
42
  def self.configuration=(config)
43
- @configuration = config
43
+ Thread.current[:sanity_configuration] = config
44
44
  end
45
45
 
46
46
  def self.configure
@@ -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
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sanity
4
+ module Http
5
+ class Publish
6
+ include Sanity::Http::Publication
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sanity
4
+ module Http
5
+ class Unpublish
6
+ include Sanity::Http::Publication
7
+ end
8
+ end
9
+ 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
 
@@ -26,8 +26,9 @@ module Sanity
26
26
  end
27
27
 
28
28
  module ClassMethods
29
- DEFAULT_KLASS_MUTATIONS = %i[create create_or_replace create_if_not_exists patch delete].freeze
30
- DEFAULT_INSTANCE_MUTATIONS = %i[create create_or_replace create_if_not_exists delete].freeze
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
@@ -28,6 +28,8 @@ module Sanity
28
28
 
29
29
  include Sanity::Mutatable
30
30
  include Sanity::Queryable
31
+ include Sanity::Publishable
32
+
31
33
  include Sanity::Serializable
32
34
  end
33
35
  end
@@ -21,5 +21,6 @@ module Sanity
21
21
  # See https://www.sanity.io/docs/http-mutations#ac77879076d4
22
22
  mutatable api_endpoint: "data/mutate"
23
23
  queryable
24
+ publishable
24
25
  end
25
26
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sanity
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/sanity.rb CHANGED
@@ -14,6 +14,7 @@ require "sanity/helpers"
14
14
  require "sanity/mutatable"
15
15
  require "sanity/queryable"
16
16
  require "sanity/serializable"
17
+ require "sanity/publishable"
17
18
 
18
19
  require "sanity/resource"
19
20
  require "sanity/resources"
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.0
4
+ version: 0.5.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-02-25 00:00:00.000000000 Z
11
+ date: 2024-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -66,10 +66,14 @@ files:
66
66
  - lib/sanity/http/find.rb
67
67
  - lib/sanity/http/mutation.rb
68
68
  - lib/sanity/http/patch.rb
69
+ - lib/sanity/http/publication.rb
70
+ - lib/sanity/http/publish.rb
69
71
  - lib/sanity/http/query.rb
70
72
  - lib/sanity/http/results.rb
73
+ - lib/sanity/http/unpublish.rb
71
74
  - lib/sanity/http/where.rb
72
75
  - lib/sanity/mutatable.rb
76
+ - lib/sanity/publishable.rb
73
77
  - lib/sanity/queryable.rb
74
78
  - lib/sanity/refinements.rb
75
79
  - lib/sanity/refinements/arrays.rb