Class: XodClient::EndpointCall

Inherits:
Object
  • Object
show all
Includes:
ParseUtil
Defined in:
lib/xod_client/endpoint_call.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ParseUtil

#parse_xod_response

Constructor Details

#initialize(xod_conn, endpoint_name, data_transformer: nil, **params) ⇒ EndpointCall

Returns a new instance of EndpointCall.



7
8
9
10
11
12
# File 'lib/xod_client/endpoint_call.rb', line 7

def initialize(xod_conn, endpoint_name, data_transformer: nil, **params)
  @xod_conn = xod_conn
  @endpoint_name = endpoint_name
  @data_transformer = data_transformer
  @params = params
end

Instance Attribute Details

#endpoint_nameObject (readonly)

Returns the value of attribute endpoint_name.



5
6
7
# File 'lib/xod_client/endpoint_call.rb', line 5

def endpoint_name
  @endpoint_name
end

#jsonObject (readonly)

Returns the value of attribute json.



5
6
7
# File 'lib/xod_client/endpoint_call.rb', line 5

def json
  @json
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/xod_client/endpoint_call.rb', line 5

def params
  @params
end

#xod_connObject (readonly)

Returns the value of attribute xod_conn.



5
6
7
# File 'lib/xod_client/endpoint_call.rb', line 5

def xod_conn
  @xod_conn
end

Instance Method Details

#[](key) ⇒ Object



69
70
71
# File 'lib/xod_client/endpoint_call.rb', line 69

def [](key)
  fetch[key]
end

#each(&block) ⇒ Object



43
44
45
46
47
# File 'lib/xod_client/endpoint_call.rb', line 43

def each(&block)
  fetch do |endpoint|
    endpoint.first_array.each { |json| block.call(json) }
  end
end

#fetch(force: false, **custom_params, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/xod_client/endpoint_call.rb', line 14

def fetch(force: false, **custom_params, &block)
  return @json if @json && !force

  params = @params.merge(custom_params)
  conn = xod_conn.faraday(url: xod_conn.config.root_url, headers: { 'Authorization': "Idaas #{xod_conn.token}" })
  method = endpoint_name.to_s.start_with?('POST') ? :post : :get
  path = build_endpoint_path(params)

  res = conn.send(method, path)
  @json = process_xod_response(res)

  if block
    block.call(self)

    if (pagination = @json.dig(:__pagination__, 0))
      ((pagination[:page_number] + 1)..pagination[:page_count]).each do |page|
        fetch!(page: page)
        block.call(self)
      end
    end
  end

  @json
end

#fetch!(**custom_params) ⇒ Object



39
40
41
# File 'lib/xod_client/endpoint_call.rb', line 39

def fetch!(**custom_params)
  fetch(custom_params.merge(force: true))
end

#firstObject



57
58
59
# File 'lib/xod_client/endpoint_call.rb', line 57

def first
  first_array.first
end

#first_arrayObject



49
50
51
# File 'lib/xod_client/endpoint_call.rb', line 49

def first_array
  fetch[first_array_key]
end

#first_array_keyObject



61
62
63
# File 'lib/xod_client/endpoint_call.rb', line 61

def first_array_key
  @json.keys.find { |k| @json[k].is_a?(Array) }
end

#first_array_of_deletedObject



53
54
55
# File 'lib/xod_client/endpoint_call.rb', line 53

def first_array_of_deleted
  fetch[:"#{first_array_key}_deleted"] || []
end

#next_changed_rowsObject



65
66
67
# File 'lib/xod_client/endpoint_call.rb', line 65

def next_changed_rows
  @json.dig(:__changed_rows__, 0, :next_changed_rows)
end