Class: OdataDuty::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/odata_duty/executor.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:, url:, context:, query_options:) ⇒ Executor

Returns a new instance of Executor.



24
25
26
27
28
29
# File 'lib/odata_duty/executor.rb', line 24

def initialize(schema:, url:, context:, query_options:)
  @schema = schema
  @url = url
  @context = context
  @query_options = query_options
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



22
23
24
# File 'lib/odata_duty/executor.rb', line 22

def context
  @context
end

#query_optionsObject (readonly)

Returns the value of attribute query_options.



22
23
24
# File 'lib/odata_duty/executor.rb', line 22

def query_options
  @query_options
end

#schemaObject (readonly)

Returns the value of attribute schema.



22
23
24
# File 'lib/odata_duty/executor.rb', line 22

def schema
  @schema
end

#urlObject (readonly)

Returns the value of attribute url.



22
23
24
# File 'lib/odata_duty/executor.rb', line 22

def url
  @url
end

Class Method Details

.createObject



10
11
12
# File 'lib/odata_duty/executor.rb', line 10

def self.create(**)
  new(**).create
end

.deleteObject



18
19
20
# File 'lib/odata_duty/executor.rb', line 18

def self.delete(**)
  new(**).delete
end

.executeObject



6
7
8
# File 'lib/odata_duty/executor.rb', line 6

def self.execute(**)
  new(**).execute
end

.updateObject



14
15
16
# File 'lib/odata_duty/executor.rb', line 14

def self.update(**)
  new(**).update
end

Instance Method Details

#createObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/odata_duty/executor.rb', line 49

def create
  unless endpoint.supports_create?
    raise NoImplementationError, "create not implemented for #{endpoint.url}"
  end

  Oj.dump(endpoint
      .create(context: wrapped_context)
      .merge(
        '@odata.context': wrapped_context.od_full_url('$metadata',
                                                      anchor: "#{endpoint.name}/$entity")
      ),
          mode: :compat)
end

#deleteObject



78
79
80
81
82
83
84
85
86
# File 'lib/odata_duty/executor.rb', line 78

def delete
  unless endpoint.supports_delete?
    raise NoImplementationError, "delete not implemented for #{endpoint.url}"
  end

  entity_id = extract_value_from_brackets(url)
  endpoint.delete(entity_id, context: wrapped_context)
  Oj.dump({ '@odata.context': wrapped_context.od_full_url('$metadata') }, mode: :compat)
end

#executeObject



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

def execute
  set_builder = prepare_builder(endpoint, wrapped_context, query_options)
  select = query_options['$select']
  props = selected(endpoint.entity_type, select) if select
  apply_select(set_builder, props)

  if url.include?('(')
    individual(set_builder, endpoint, wrapped_context, props)
  elsif url.include?('/$count')
    apply_search(set_builder, query_options['$search'])
    set_builder.count
  else
    collection(set_builder, endpoint, wrapped_context, query_options, props)
  end
end

#pointsObject



31
# File 'lib/odata_duty/executor.rb', line 31

def points = schema.endpoints

#prepare_builder(endpoint, context, query_options) ⇒ Object



88
89
90
91
92
93
# File 'lib/odata_duty/executor.rb', line 88

def prepare_builder(endpoint, context, query_options)
  endpoint.new_entity_set(context: context).tap do |set_builder|
    filter = query_options['$filter']
    apply_filter(endpoint, set_builder, filter) if query_options.key?('$filter')
  end
end

#updateObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/odata_duty/executor.rb', line 63

def update
  unless endpoint.supports_update?
    raise NoImplementationError, "update not implemented for #{endpoint.url}"
  end

  entity_id = extract_value_from_brackets(url)
  Oj.dump(endpoint
      .update(entity_id, context: wrapped_context)
      .merge(
        '@odata.context': wrapped_context.od_full_url('$metadata',
                                                      anchor: "#{endpoint.name}/$entity")
      ),
          mode: :compat)
end