watir_api 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/watir_api.rb +27 -15
  3. data/watir_api.gemspec +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0bd5fa8ac2cf09b66985111647ddcab4cc178fbd
4
- data.tar.gz: 0b0245d742a805fd30c29472715035454589180e
3
+ metadata.gz: 2866494791af1c0785b52cb294240c8f0a2c16a9
4
+ data.tar.gz: 0a7dd46130691e620357d03fcd66113372ee2ff2
5
5
  SHA512:
6
- metadata.gz: 516e6b75070acbe7e4db73b0398e75b111ceb4f2952237688d1b72d26256d1592c90f9c8c5077b0086a173d67bc18df2e91e04f80439b5b86413ca6ce2112d6f
7
- data.tar.gz: e783068c1804287a634dd6bfeae7f6400f4382fd41a8e5faf8ea5aca95fd53ef8a79ace7bcfcecbcdb1cc265954179f724d76f66a5a9991b66967e007d85410d
6
+ metadata.gz: 9abe2898a5c711ec90c37889123516c985e51165670040576b747983503c742f3134a12b25ce5c37b6b6508ab5a18edb0f8969a7ac49977226855e65d5b7e23d
7
+ data.tar.gz: f9a64cc277053f4c4530d978efa808bf2d22a34b15e777e9f466ea94156e909ac931edde90a2949980e070c5c0b8a7a7ae5dec79ca31c068fbba5bad397d059f
data/lib/watir_api.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "rest-client"
2
2
  require "json"
3
+ require 'active_support/inflector'
3
4
 
4
5
  module WatirApi
5
6
  class Base
@@ -7,30 +8,29 @@ module WatirApi
7
8
 
8
9
  def index(opt = {})
9
10
  new rest_call({method: :get,
10
- url: route}.merge opt)
11
+ url: route(opt)}.merge opt)
11
12
  end
12
13
 
13
14
  def show(id:, **opt)
14
15
  new rest_call({method: :get,
15
- url: "#{route}/#{id}".chomp('/')}.merge opt)
16
+ url: "#{route(opt)}/#{id}".chomp('/')}.merge opt)
16
17
  end
17
18
 
18
- def create(obj = nil)
19
- new rest_call(method: :post,
20
- url: route,
21
- payload: generate_payload(obj),
22
- headers: headers)
19
+ def create(obj = nil, opt = {})
20
+ new rest_call({method: :post,
21
+ url: route(opt),
22
+ payload: generate_payload(obj)}.merge opt)
23
23
  end
24
24
 
25
25
  def destroy(id:, **opt)
26
26
  new rest_call({method: :delete,
27
- url: "#{route}/#{id}".chomp('/')}.merge opt)
27
+ url: "#{route(opt)}/#{id}".chomp('/')}.merge opt)
28
28
  end
29
29
 
30
30
  def update(id:, with:, **opt)
31
31
  new rest_call({method: :put,
32
- url: "#{route}/#{id}".chomp('/'),
33
- payload: generate_payload(with)}.merge opt)
32
+ url: "#{route(opt)}/#{id}".chomp('/'),
33
+ payload: generate_payload(with)}.merge opt)
34
34
  end
35
35
 
36
36
  def base_url=(base_url)
@@ -57,7 +57,7 @@ module WatirApi
57
57
 
58
58
  def rest_call(opt)
59
59
  opt[:verify_ssl] = opt.delete(:ssl) if opt.key?(:ssl)
60
- opt[:headers] = opt.delete(:headers) || headers
60
+ opt[:headers] ||= headers
61
61
  RestClient::Request.execute(opt) do |response, request, result|
62
62
  [response, request, result]
63
63
  end
@@ -81,15 +81,27 @@ module WatirApi
81
81
  end
82
82
  end
83
83
 
84
- attr_reader :data, :code, :response
84
+ attr_reader :response, :code, :header, :data
85
85
 
86
86
  def initialize(args)
87
- response, request, _result = *args
87
+ response, _request, _result = *args
88
88
  @response = response
89
89
  @code = response.code
90
- @header = request.instance_variable_get('@header')
90
+ @header = response.instance_variable_get('@headers')
91
91
  @data = JSON.parse(response.body, symbolize_names: true) rescue nil
92
- @data = (convert_to_model(@data) || @data) unless @data.nil? || !(defined? model_object.new)
92
+
93
+ set_watir_model_attr
94
+ end
95
+
96
+ def set_watir_model_attr
97
+ return unless defined?(model_object.new)
98
+ model = convert_to_model(@data) unless @data.nil?
99
+ var = model_object.to_s[/[^:]*$/].underscore
100
+ var = var.pluralize if @data.is_a? Array
101
+ instance_variable_set "@#{var}", model
102
+ singleton_class.class_eval { attr_accessor var }
103
+ @id = @data[:id] if @data.is_a? Hash
104
+ singleton_class.class_eval { attr_accessor :id } unless @id.nil?
93
105
  end
94
106
 
95
107
  def convert_to_model(data)
data/watir_api.gemspec CHANGED
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "watir_api"
6
- spec.version = "0.1.2"
6
+ spec.version = "0.2.0"
7
7
  spec.authors = ["Titus Fortner"]
8
8
  spec.email = ["titusfortner@gmail.com"]
9
9
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: watir_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Titus Fortner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-30 00:00:00.000000000 Z
11
+ date: 2017-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client