Module: Sanity::Http::Mutation

Included in:
Create, CreateIfNotExists, CreateOrReplace, Delete, Patch
Defined in:
lib/sanity/http/mutation.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ALLOWED_VISIBILITY =
%i[sync async deferred]
REQUEST_KEY =
"mutations"
DEFAULT_QUERY_PARAMS =
{
  return_ids: false,
  return_documents: false,
  visibility: :sync
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



38
39
40
# File 'lib/sanity/http/mutation.rb', line 38

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



38
39
40
# File 'lib/sanity/http/mutation.rb', line 38

def params
  @params
end

#query_setObject (readonly)

Returns the value of attribute query_set.



38
39
40
# File 'lib/sanity/http/mutation.rb', line 38

def query_set
  @query_set
end

#resource_klassObject (readonly)

Returns the value of attribute resource_klass.



38
39
40
# File 'lib/sanity/http/mutation.rb', line 38

def resource_klass
  @resource_klass
end

#serializerObject (readonly)

Returns the value of attribute serializer.



38
39
40
# File 'lib/sanity/http/mutation.rb', line 38

def serializer
  @serializer
end

Class Method Details

.included(base) ⇒ Object



11
12
13
14
15
16
# File 'lib/sanity/http/mutation.rb', line 11

def included(base)
  base.extend(ClassMethods)
  base.extend(Forwardable)
  base.delegate(%i[project_id api_version dataset token api_subdomain] => :"Sanity.config")
  base.delegate(mutatable_api_endpoint: :resource_klass)
end

Instance Method Details

#body_keyObject



62
63
64
# File 'lib/sanity/http/mutation.rb', line 62

def body_key
  self.class.name.demodulize.underscore.camelize_lower
end

#callObject



66
67
68
69
70
# File 'lib/sanity/http/mutation.rb', line 66

def call
  Net::HTTP.post(uri, {"#{REQUEST_KEY}": body}.to_json, headers).then do |result|
    block_given? ? yield(serializer.call(result)) : serializer.call(result)
  end
end

#initialize(**args) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sanity/http/mutation.rb', line 40

def initialize(**args)
  @resource_klass = args.delete(:resource_klass)
  @params = args.delete(:params)
  @query_set = Set.new

  warn RESULT_WRAPPER_DEPRECATION_WARNING if args[:result_wrapper]
  @serializer = args.delete(:serializer) ||
    args.delete(:result_wrapper) || # kept for backwards compatibility
    klass_serializer ||
    Sanity::Http::Results

  raise ArgumentError, "resource_klass must be defined" unless resource_klass
  raise ArgumentError, "params argument is missing" unless params

  (args.delete(:options) || {}).then do |opts|
    DEFAULT_QUERY_PARAMS.keys.each do |qup|
      query_set << [qup, opts.fetch(qup, DEFAULT_QUERY_PARAMS[qup])]
    end
  end
  raise ArgumentError, "visibility argument must be one of #{ALLOWED_VISIBILITY}" unless valid_invisibility?
end

#result_wrapperObject



72
73
74
75
# File 'lib/sanity/http/mutation.rb', line 72

def result_wrapper
  warn RESULT_WRAPPER_DEPRECATION_WARNING
  serializer
end