Class: WatirApi::Base
- Inherits:
-
Object
- Object
- WatirApi::Base
- Defined in:
- lib/watir_api.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
- .base_url ⇒ Object
- .base_url=(base_url) ⇒ Object
- .create(obj = nil) ⇒ 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
-
#initialize(args) ⇒ Base
constructor
A new instance of Base.
- #model_object ⇒ Object
Constructor Details
#initialize(args) ⇒ Base
Returns a new instance of Base.
86 87 88 89 90 91 92 93 |
# File 'lib/watir_api.rb', line 86 def initialize(args) response, request, _result = *args @response = response @code = response.code @header = request.instance_variable_get('@header') @data = JSON.parse(response.body, symbolize_names: true) rescue nil @data = (convert_to_model(@data) || @data) unless @data.nil? || !(defined? model_object.new) end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
84 85 86 |
# File 'lib/watir_api.rb', line 84 def code @code end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
84 85 86 |
# File 'lib/watir_api.rb', line 84 def data @data end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
84 85 86 |
# File 'lib/watir_api.rb', line 84 def response @response end |
Class Method Details
.base_url ⇒ Object
40 41 42 |
# File 'lib/watir_api.rb', line 40 def base_url @@base_url || '' end |
.base_url=(base_url) ⇒ Object
36 37 38 |
# File 'lib/watir_api.rb', line 36 def base_url=(base_url) @@base_url = base_url end |
.create(obj = nil) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/watir_api.rb', line 18 def create(obj = nil) new rest_call(method: :post, url: route, payload: generate_payload(obj), headers: headers) end |
.destroy(id:, **opt) ⇒ Object
25 26 27 28 |
# File 'lib/watir_api.rb', line 25 def destroy(id:, **opt) new rest_call({method: :delete, url: "#{route}/#{id}".chomp('/')}.merge opt) end |
.endpoint ⇒ Object
48 49 50 |
# File 'lib/watir_api.rb', line 48 def endpoint '' end |
.index(opt = {}) ⇒ Object
8 9 10 11 |
# File 'lib/watir_api.rb', line 8 def index(opt = {}) new rest_call({method: :get, url: route}.merge opt) end |
.model_object ⇒ Object
52 53 54 |
# File 'lib/watir_api.rb', line 52 def model_object eval "Model::#{self.to_s[/[^:]*$/]}" end |
.route(opt = {}) ⇒ Object
44 45 46 |
# File 'lib/watir_api.rb', line 44 def route(opt = {}) "#{opt[:base_url] || base_url}/#{opt.delete(:endpoint) || endpoint}" end |
.show(id:, **opt) ⇒ Object
13 14 15 16 |
# File 'lib/watir_api.rb', line 13 def show(id:, **opt) new rest_call({method: :get, url: "#{route}/#{id}".chomp('/')}.merge opt) end |
.update(id:, with:, **opt) ⇒ Object
30 31 32 33 34 |
# File 'lib/watir_api.rb', line 30 def update(id:, with:, **opt) new rest_call({method: :put, url: "#{route}/#{id}".chomp('/'), payload: generate_payload(with)}.merge opt) end |
Instance Method Details
#convert_to_model(data) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/watir_api.rb', line 95 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 |
#model_object ⇒ Object
111 112 113 |
# File 'lib/watir_api.rb', line 111 def model_object self.class.model_object end |