Module: Sanity::Http::Query

Included in:
Find, Where
Defined in:
lib/sanity/http/query.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#resource_klassObject (readonly)

Returns the value of attribute resource_klass.



24
25
26
# File 'lib/sanity/http/query.rb', line 24

def resource_klass
  @resource_klass
end

#serializerObject (readonly)

Returns the value of attribute serializer.



24
25
26
# File 'lib/sanity/http/query.rb', line 24

def serializer
  @serializer
end

Class Method Details

.included(base) ⇒ Object



11
12
13
14
15
# File 'lib/sanity/http/query.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")
end

Instance Method Details

#callObject

TODO:

Add query support



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sanity/http/query.rb', line 38

def call
  http = Net::HTTP.new(uri.host, uri.port)

  http.use_ssl = uri.scheme == "https"

  request = Module.const_get("Net::HTTP::#{method.to_s.classify}").new(uri, headers)

  request.body = request_body

  http.request(request).then do |result|
    data = JSON.parse(result.body)

    block_given? ? yield(serializer.call(data)) : serializer.call(data)
  end
end

#initialize(**args) ⇒ Object

TODO:

Add query support



27
28
29
30
31
32
33
34
35
# File 'lib/sanity/http/query.rb', line 27

def initialize(**args)
  @resource_klass = args.delete(:resource_klass)

  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
end

#result_wrapperObject



54
55
56
57
# File 'lib/sanity/http/query.rb', line 54

def result_wrapper
  warn RESULT_WRAPPER_DEPRECATION_WARNING
  serializer
end