Class: Desertcart::Searcher

Inherits:
LedgerSync::Ledgers::Searcher
  • Object
show all
Defined in:
lib/desertcart/searcher.rb

Instance Method Summary collapse

Instance Method Details

#deserialized_resource(hash:) ⇒ Object



25
26
27
28
29
30
# File 'lib/desertcart/searcher.rb', line 25

def deserialized_resource(hash:)
  searcher_deserializer.deserialize(
    hash: hash,
    resource: resource_class.new
  )
end

#ledger_namespace_pathObject



53
54
55
# File 'lib/desertcart/searcher.rb', line 53

def ledger_namespace_path
  @ledger_namespace_path ||= nil
end

#ledger_resource_typeObject



64
65
66
67
68
# File 'lib/desertcart/searcher.rb', line 64

def ledger_resource_type
  @ledger_resource_type ||= client.class.ledger_resource_type_for(
    resource_class: resource_class
  )
end

#ledger_resources_pathObject



57
58
59
60
61
62
# File 'lib/desertcart/searcher.rb', line 57

def ledger_resources_path
  @ledger_resources_path ||= [
    ledger_namespace_path,
    ledger_resource_type.pluralize
  ].compact.join('/')
end

#next?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
# File 'lib/desertcart/searcher.rb', line 16

def next?
  case response.status
  when 200
    response.body.dig('meta', 'pagination', 'next_page')
  else
    false
  end
end

#resource_classObject



39
40
41
# File 'lib/desertcart/searcher.rb', line 39

def resource_class
  @resource_class ||= self.class.inferred_resource_class
end

#resourcesObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/desertcart/searcher.rb', line 5

def resources
  case response.status
  when 200
    response.body
            .fetch(ledger_resource_type.pluralize)
            .map { |o| deserialized_resource(hash: o) }
  else
    []
  end
end

#responseObject



32
33
34
35
36
37
# File 'lib/desertcart/searcher.rb', line 32

def response
  @response ||= client.read(
    path: ledger_resources_path,
    params: query
  )
end

#searcher_deserializerObject



49
50
51
# File 'lib/desertcart/searcher.rb', line 49

def searcher_deserializer
  searcher_deserializer_class.new
end

#searcher_deserializer_classObject



43
44
45
46
47
# File 'lib/desertcart/searcher.rb', line 43

def searcher_deserializer_class
  @searcher_deserializer_class ||= self.class.inferred_serialization_class(
    type: 'SearcherDeserializer'
  )
end