Class: LcpRuby::DataSource::RestJson

Inherits:
Base
  • Object
show all
Defined in:
lib/lcp_ruby/data_source/rest_json.rb

Overview

REST/JSON data source adapter. Fetches records from an external REST API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#count, #destroy, #save, #select_options, #writable?

Constructor Details

#initialize(config, model_definition) ⇒ RestJson

Returns a new instance of RestJson.



11
12
13
14
15
16
17
18
# File 'lib/lcp_ruby/data_source/rest_json.rb', line 11

def initialize(config, model_definition)
  @config = config
  @model_definition = model_definition
  @field_mapping = config["field_mapping"] || {}
  @reverse_mapping = @field_mapping.invert
  @id_field = config["id_field"] || "id"
  @timeout = config["timeout"] || 30
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/lcp_ruby/data_source/rest_json.rb', line 9

def config
  @config
end

#model_definitionObject (readonly)

Returns the value of attribute model_definition.



9
10
11
# File 'lib/lcp_ruby/data_source/rest_json.rb', line 9

def model_definition
  @model_definition
end

Instance Method Details

#find(id) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/lcp_ruby/data_source/rest_json.rb', line 20

def find(id)
  endpoint = record_endpoint(id)
  response = http_get(endpoint)
  data = navigate_response(response, config.dig("endpoints", "show", "response_path"))
  hydrate(data)
rescue ConnectionError
  raise
rescue => e
  raise RecordNotFound, "Record #{id} not found: #{e.message}"
end

#find_many(ids) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lcp_ruby/data_source/rest_json.rb', line 31

def find_many(ids)
  batch_endpoint = config.dig("endpoints", "batch")
  if batch_endpoint
    url = build_url(batch_endpoint["path"], ids: ids.join(","))
    response = http_get(url)
    items = navigate_response(response, batch_endpoint["response_path"])
    Array(items).map { |item| hydrate(item) }
  else
    super
  end
end

#search(params = {}, sort: nil, page: 1, per: 25) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lcp_ruby/data_source/rest_json.rb', line 43

def search(params = {}, sort: nil, page: 1, per: 25)
  endpoint_config = config.dig("endpoints", "search") || config.dig("endpoints", "index") || {}
  method = (endpoint_config["method"] || "GET").upcase

  query_params = build_search_params(params, sort: sort, page: page, per: per)

  if method == "POST"
    url = build_url(endpoint_config["path"] || collection_path)
    response = http_post(url, query_params)
  else
    url = build_url(collection_path, **query_params)
    response = http_get(url)
  end

  items = navigate_response(response, endpoint_config["response_path"] || config["response_path"])
  total = extract_total_count(response, endpoint_config, items)
  records = Array(items).map { |item| hydrate(item) }

  SearchResult.new(records: records, total_count: total, current_page: page, per_page: per)
end

#supported_operatorsObject



64
65
66
# File 'lib/lcp_ruby/data_source/rest_json.rb', line 64

def supported_operators
  config["supported_operators"] || super
end