Class: UI2API::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Base

Returns a new instance of Base.



86
87
88
89
90
91
92
93
94
# File 'lib/ui2api.rb', line 86

def initialize(args)
  response, _request, _result = *args
  @response = response
  @code = response.code
  @header = response.instance_variable_get('@headers')
  @data = JSON.parse(response.body, symbolize_names: true) rescue nil

  set_watir_model_attr
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



84
85
86
# File 'lib/ui2api.rb', line 84

def code
  @code
end

#dataObject (readonly)

Returns the value of attribute data.



84
85
86
# File 'lib/ui2api.rb', line 84

def data
  @data
end

#headerObject (readonly)

Returns the value of attribute header.



84
85
86
# File 'lib/ui2api.rb', line 84

def header
  @header
end

#responseObject (readonly)

Returns the value of attribute response.



84
85
86
# File 'lib/ui2api.rb', line 84

def response
  @response
end

Class Method Details

.base_urlObject



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

def base_url
  @@base_url || ''
end

.base_url=(base_url) ⇒ Object



36
37
38
# File 'lib/ui2api.rb', line 36

def base_url=(base_url)
  @@base_url = base_url
end

.create(obj = nil, opt = {}) ⇒ Object



19
20
21
22
23
# File 'lib/ui2api.rb', line 19

def create(obj = nil, opt = {})
  new rest_call({method: :post,
                 url: route(opt),
                 payload: generate_payload(obj)}.merge opt)
end

.destroy(id:, **opt) ⇒ Object



25
26
27
28
# File 'lib/ui2api.rb', line 25

def destroy(id:, **opt)
  new rest_call({method: :delete,
                 url: "#{route(opt)}/#{id}".chomp('/')}.merge opt)
end

.endpointObject



48
49
50
# File 'lib/ui2api.rb', line 48

def endpoint
  ''
end

.index(opt = {}) ⇒ Object



9
10
11
12
# File 'lib/ui2api.rb', line 9

def index(opt = {})
  new rest_call({method: :get,
                 url: route(opt)}.merge opt)
end

.model_objectObject



52
53
54
# File 'lib/ui2api.rb', line 52

def model_object
  eval "Model::#{self.to_s[/[^:]*$/]}"
end

.route(opt = {}) ⇒ Object



44
45
46
# File 'lib/ui2api.rb', line 44

def route(opt = {})
  "#{opt[:base_url] || base_url}/#{opt.delete(:endpoint) || endpoint}"
end

.show(id:, **opt) ⇒ Object



14
15
16
17
# File 'lib/ui2api.rb', line 14

def show(id:, **opt)
  new rest_call({method: :get,
                 url: "#{route(opt)}/#{id}".chomp('/')}.merge opt)
end

.update(id:, with:, **opt) ⇒ Object



30
31
32
33
34
# File 'lib/ui2api.rb', line 30

def update(id:, with:, **opt)
  new rest_call({method: :put,
                 url: "#{route(opt)}/#{id}".chomp('/'),
                 payload: generate_payload(with)}.merge opt)
end

Instance Method Details

#convert_to_model(data) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ui2api.rb', line 105

def convert_to_model(data)
  if data.is_a? Hash
    return if (model_object.valid_keys & data.keys).empty?
    begin
      model_object.convert(data)
    rescue StandardError => ex
      raise unless ex.message.include?('Can not convert Hash to Model')
    end
  elsif data.is_a? Array
    data.map do |hash|
      model = convert_to_model(hash)
      model.nil? ? return : model
    end
  end
end

#define_attribute(key, value) ⇒ Object



125
126
127
128
# File 'lib/ui2api.rb', line 125

def define_attribute(key, value)
  instance_variable_set("@#{key}", value)
  singleton_class.class_eval { attr_reader key }
end

#model_objectObject



121
122
123
# File 'lib/ui2api.rb', line 121

def model_object
  self.class.model_object
end

#set_watir_model_attrObject



96
97
98
99
100
101
102
103
# File 'lib/ui2api.rb', line 96

def set_watir_model_attr
  return unless defined?(model_object.new)
  model = convert_to_model(@data) unless @data.nil?
  var = model_object.to_s[/[^:]*$/].underscore
  var = var.pluralize if @data.is_a? Array
  define_attribute(var, model)
  define_attribute(:id, @data[:id]) if @data.is_a? Hash
end