Class: ServiceBase

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vc = nil, path = '/', silent = false) ⇒ ServiceBase

Returns a new instance of ServiceBase.



6
7
8
9
10
11
12
# File 'lib/service_base.rb', line 6

def initialize(vc=nil,path='/',silent=false)
  @path     = path
  @response = {}
  @parent   = vc 
  reset(%w(headers body vars))
  load_service
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/service_base.rb', line 5

def path
  @path
end

Instance Method Details

#_body(prm) ⇒ Object



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

def _body(prm)
  prm==[] ? show_vars(bodies,'body') : set_value(:bodies,prm)
end

#_headers(prm) ⇒ Object



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

def _headers(prm)
  prm==[] ? show_vars(headers,'headers') : set_value(:headers,prm)
end

#_info(prm) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/service_base.rb', line 21

def _info(prm)
  headerx = headers.select{|k,v|v!=""}
  bodyx = bodies.select{|k,v|v!=""}
  varx = vars.select{|k,v|v!=""}
  line = ('-'*65)

  puts ('='*65).yellow
  puts line.blue
  puts "headers".intense_green 
  puts headerx.pj
  puts line.blue
  puts "body".intense_green
  puts bodyx.pj
  puts line.blue
  puts "vars".intense_green
  puts vars.pj
  ''
end

#_reset(prm) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/service_base.rb', line 57

def _reset(prm)
  reset(prm)
  show_vars(headers,'headers')
  show_vars(bodies,'bodies')
  show_vars(vars,'vars')
  save_vars
end

#_response(prm) ⇒ Object



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

def _response(prm)
  puts prm==[] ? @response.pj : @response[prm[0].to_sym].pj
  "Inspecting..."
end

#_send(prm) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/service_base.rb', line 65

def _send(prm)
  headerx = headers.select{|k,v|v!=""}
  bodyx = bodies.select{|k,v|v!=""}
  varx = vars.select{|k,v|v!=""}
  line = ('-'*65)

  puts line.yellow
  puts "[#{varx[:conn]}] #{varx[:url]}".intense_blue
  puts line.blue
  puts "request".intense_green 
  puts({"headers" => headerx,"body" => bodyx}.pj)
  begin
    conn = Faraday.new(:url => varx[:url])
    resp = conn.send(varx[:conn]) do |req|
      req.body = body_join 
      req.headers = headerx
    end.env
    ct = resp[:response_headers]["content-type"]
    if ct && /application\/json/=~ct.downcase
      body = JSON.parse(resp[:body])
    else
      body = resp[:body]
    end
    @response = {
      :status => resp[:status],
      :headers => resp[:response_headers],
      :body => body
    }
    Rest::Terminal.instance_variable_set("@service",@path)
    Rest::Terminal.instance_variable_set("@response",@response)
    puts line.red
    puts "response".intense_green 
    puts @response.pj
    save_vars
    ""
  rescue Faraday::Error::ConnectionFailed => e
    "#{e}"
  end

end

#_vars(prm) ⇒ Object



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

def _vars(prm)
  prm==[] ? show_vars(vars,'vars') : set_value(:vars,prm)
end

#bodies(up = true) ⇒ Object



110
111
112
# File 'lib/service_base.rb', line 110

def bodies(up=true)
  inheritances(:bodies,up).select{|k,v|v} 
end

#headers(up = true) ⇒ Object



106
107
108
# File 'lib/service_base.rb', line 106

def headers(up=true)
  inheritances(:headers,up).select{|k,v|v} 
end

#inheritances(key, up) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/service_base.rb', line 118

def inheritances(key,up)
  @_tmp = {}
  if @parent && up
    v_par = @parent.send(:inheritances,key,up)
    @_tmp.merge!(v_par) if v_par
  end
  v_ins = instance_variable_get("@#{key}")
  @_tmp = @_tmp.merge(v_ins) if v_ins
  @_tmp
end

#load_serviceObject



14
15
16
17
18
19
# File 'lib/service_base.rb', line 14

def load_service
  # p "services#{path}service.rb"
  if File.exists?("services#{path}service.rb")
    instance_eval(IO.read("services#{path}service.rb"))
  end
end

#vars(up = true) ⇒ Object



114
115
116
# File 'lib/service_base.rb', line 114

def vars(up=true)
  inheritances(:vars,up).select{|k,v|v} 
end