Class: MemoriClient::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/memori_client/resource.rb

Direct Known Subclasses

Backend::Resource, Engine::Resource

Class Method Summary collapse

Class Method Details

.build_arguments(the_binding) ⇒ Object



2
3
4
5
6
7
# File 'lib/memori_client/resource.rb', line 2

def self.build_arguments(the_binding)
  the_binding.local_variables.each_with_object({}) do |key, acc|
    value = the_binding.local_variable_get(key)
    acc[key] = value
  end
end

.build_url(_url) ⇒ Object

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/memori_client/resource.rb', line 40

def self.build_url(_url)
  raise NotImplementedError
end

.exec_http_request(method, path, args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/memori_client/resource.rb', line 9

def self.exec_http_request(method, path, args)
  stop = false
  processed_tokens = []
  path.split('/').each do |token|
    break if stop == true
    if token =~ /^{.*}$/
      param_name = token.match(/^{(.*)}$/).captures.first
      if args[param_name.to_sym].blank?
        stop = true
      else
        processed_tokens << args[param_name.to_sym]
      end
    else
      processed_tokens << token
    end
  end

  url = processed_tokens.join('/')
  url = build_url(url)
  http = MemoriClient::HttpClient.new

  case method
  when 'get'
    status, body = http.get(url)
  else
    status, body = http.send(method, url, payload: args[:payload])
  end

  [status, body]
end

.validate_payload!(payload, keys:, required:) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/memori_client/resource.rb', line 44

def self.validate_payload!(payload, keys:, required:)
  bad_names = []
  payload.each do |k, v|
    unless keys.include?(k.to_s)
      bad_names << k
    end
  end

  if bad_names.size > 0
    raise ArgumentError.new "Found invalid keys: #{bad_names.join(', ')}"
  end
end