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

#result_wrapperObject (readonly)

Returns the value of attribute result_wrapper.



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

def result_wrapper
  @result_wrapper
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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sanity/http/query.rb', line 33

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(result_wrapper.call(data)) : result_wrapper.call(data)
  end
end

#initialize(**args) ⇒ Object



27
28
29
30
# File 'lib/sanity/http/query.rb', line 27

def initialize(**args)
  @resource_klass = args.delete(:resource_klass)
  @result_wrapper = args.delete(:result_wrapper) || Sanity::Http::Results
end