module Sanity::Http::Mutation

Constants

ALLOWED_VISIBILITY

See www.sanity.io/docs/http-mutations#visibility-937bc4250c79

DEFAULT_QUERY_PARAMS

See www.sanity.io/docs/http-mutations#952b77deb110

REQUEST_KEY

See www.sanity.io/docs/http-mutations#aa493b1c2524

Attributes

options[R]
params[R]
query_set[R]
resource_klass[R]
result_wrapper[R]

Public Class Methods

included(base) click to toggle source
# File lib/sanity/http/mutation.rb, line 10
def included(base)
  base.extend(ClassMethods)
  base.extend(Forwardable)
  base.delegate(%i[project_id api_version dataset token] => :"Sanity.config")
  base.delegate(mutatable_api_endpoint: :resource_klass)
end
new(**args) click to toggle source
# File lib/sanity/http/mutation.rb, line 39
def initialize(**args)
  @resource_klass = args.delete(:resource_klass)
  @params = args.delete(:params)
  @query_set = Set.new
  @result_wrapper = args.delete(:result_wrapper) || 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

Public Instance Methods

body_key() click to toggle source
# File lib/sanity/http/mutation.rb, line 56
def body_key
  self.class.name.demodulize.underscore.camelize_lower
end
call() { |call| ... } click to toggle source
# File lib/sanity/http/mutation.rb, line 60
def call
  Net::HTTP.post(uri, {"#{REQUEST_KEY}": body}.to_json, headers).then do |result|
    block_given? ? yield(result_wrapper.call(result)) : result_wrapper.call(result)
  end
end

Private Instance Methods

base_url() click to toggle source
# File lib/sanity/http/mutation.rb, line 68
def base_url
  "https://#{project_id}.api.sanity.io/#{api_version}/#{mutatable_api_endpoint}/#{dataset}"
end
body() click to toggle source
# File lib/sanity/http/mutation.rb, line 72
def body
  return Array.wrap({"#{body_key}": params}) if params.is_a?(Hash)

  Array.wrap(params.map { |pam| {"#{body_key}": pam} })
end
camelize_query_set() click to toggle source
# File lib/sanity/http/mutation.rb, line 78
def camelize_query_set
  query_set.to_h.transform_keys do |key|
    key.to_s.camelize_lower
  end
end
headers() click to toggle source
# File lib/sanity/http/mutation.rb, line 84
def headers
  {
    "Content-Type": "application/json",
    Authorization: "Bearer #{token}"
  }
end
query_params() click to toggle source
# File lib/sanity/http/mutation.rb, line 91
def query_params
  camelize_query_set.map do |key, val|
    "#{key}=#{val}"
  end.join("&")
end
uri() click to toggle source
# File lib/sanity/http/mutation.rb, line 97
def uri
  URI(base_url).tap do |obj|
    obj.query = query_params
  end
end
valid_invisibility?() click to toggle source
# File lib/sanity/http/mutation.rb, line 103
def valid_invisibility?
  ALLOWED_VISIBILITY.include? query_set.to_h[:visibility]
end