Class: UI2API::Base
- Inherits:
-
Object
- Object
- UI2API::Base
- Defined in:
- lib/ui2api.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
- .base_url ⇒ Object
- .base_url=(base_url) ⇒ Object
- .create(obj = nil, opt = {}) ⇒ Object
- .destroy(id:, **opt) ⇒ Object
- .endpoint ⇒ Object
- .index(opt = {}) ⇒ Object
- .model_object ⇒ Object
- .route(opt = {}) ⇒ Object
- .show(id:, **opt) ⇒ Object
- .update(id:, with:, **opt) ⇒ Object
Instance Method Summary collapse
- #convert_to_model(data) ⇒ Object
- #define_attribute(key, value) ⇒ Object
-
#initialize(args) ⇒ Base
constructor
A new instance of Base.
- #model_object ⇒ Object
- #set_watir_model_attr ⇒ Object
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
#code ⇒ Object (readonly)
Returns the value of attribute code.
84 85 86 |
# File 'lib/ui2api.rb', line 84 def code @code end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
84 85 86 |
# File 'lib/ui2api.rb', line 84 def data @data end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
84 85 86 |
# File 'lib/ui2api.rb', line 84 def header @header end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
84 85 86 |
# File 'lib/ui2api.rb', line 84 def response @response end |
Class Method Details
.base_url ⇒ Object
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 |
.endpoint ⇒ Object
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_object ⇒ Object
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..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_object ⇒ Object
121 122 123 |
# File 'lib/ui2api.rb', line 121 def model_object self.class.model_object end |
#set_watir_model_attr ⇒ Object
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 |