class WatirApi::Base
Attributes
code[R]
data[R]
header[R]
response[R]
Public Class Methods
base_url()
click to toggle source
# File lib/watir_api.rb, line 40 def base_url @@base_url || '' end
base_url=(base_url)
click to toggle source
# File lib/watir_api.rb, line 36 def base_url=(base_url) @@base_url = base_url end
create(obj = nil, opt = {})
click to toggle source
# File lib/watir_api.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)
click to toggle source
# File lib/watir_api.rb, line 25 def destroy(id:, **opt) new rest_call({method: :delete, url: "#{route(opt)}/#{id}".chomp('/')}.merge opt) end
endpoint()
click to toggle source
# File lib/watir_api.rb, line 48 def endpoint '' end
index(opt = {})
click to toggle source
# File lib/watir_api.rb, line 9 def index(opt = {}) new rest_call({method: :get, url: route(opt)}.merge opt) end
model_object()
click to toggle source
# File lib/watir_api.rb, line 52 def model_object eval "Model::#{self.to_s[/[^:]*$/]}" end
new(args)
click to toggle source
# File lib/watir_api.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
route(opt = {})
click to toggle source
# File lib/watir_api.rb, line 44 def route(opt = {}) "#{opt[:base_url] || base_url}/#{opt.delete(:endpoint) || endpoint}" end
show(id:, **opt)
click to toggle source
# File lib/watir_api.rb, line 14 def show(id:, **opt) new rest_call({method: :get, url: "#{route(opt)}/#{id}".chomp('/')}.merge opt) end
update(id:, with:, **opt)
click to toggle source
# File lib/watir_api.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
Private Class Methods
generate_payload(obj)
click to toggle source
# File lib/watir_api.rb, line 66 def generate_payload(obj) case obj when NilClass model_object.new.to_api when WatirModel obj.to_api when JSON # noop else obj.to_json end end
headers()
click to toggle source
# File lib/watir_api.rb, line 79 def headers {content_type: :json} end
rest_call(opt)
click to toggle source
# File lib/watir_api.rb, line 58 def rest_call(opt) opt[:verify_ssl] = opt.delete(:ssl) if opt.key?(:ssl) opt[:headers] ||= headers RestClient::Request.execute(opt) do |response, request, result| [response, request, result] end end
Public Instance Methods
convert_to_model(data)
click to toggle source
# File lib/watir_api.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)
click to toggle source
# File lib/watir_api.rb, line 125 def define_attribute(key, value) instance_variable_set("@#{key}", value) singleton_class.class_eval { attr_reader key } end
model_object()
click to toggle source
# File lib/watir_api.rb, line 121 def model_object self.class.model_object end
set_watir_model_attr()
click to toggle source
# File lib/watir_api.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