Class: SinatraWebService
- Inherits:
-
Object
show all
- Defined in:
- lib/sinatra_webservice.rb
Defined Under Namespace
Classes: SinatraStem
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#delete_response(action, data = "", headers = nil, dest = nil) ⇒ Object
-
#find_free_port ⇒ Object
-
#get_response(action) ⇒ Object
-
#initialize(options = {}) ⇒ SinatraWebService
constructor
A new instance of SinatraWebService.
-
#method_missing(method, *args, &block) ⇒ Object
-
#post_response(action, data, headers = nil, dest = nil) ⇒ Object
-
#put_response(action, data = "", headers = nil, dest = nil) ⇒ Object
-
#run! ⇒ Object
-
#running? ⇒ Boolean
Constructor Details
Returns a new instance of SinatraWebService.
23
24
25
26
|
# File 'lib/sinatra_webservice.rb', line 23
def initialize(options = {})
@host = options[:host] ||= 'localhost'
@port = options[:port] ||= 4567
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
84
85
86
87
88
|
# File 'lib/sinatra_webservice.rb', line 84
def method_missing(method, *args, &block)
SinatraStem.instance_eval do |base|
route method.to_s.upcase, *args, &block
end
end
|
Instance Attribute Details
#current_thread ⇒ Object
Returns the value of attribute current_thread.
7
8
9
|
# File 'lib/sinatra_webservice.rb', line 7
def current_thread
@current_thread
end
|
#host ⇒ Object
Returns the value of attribute host.
6
7
8
|
# File 'lib/sinatra_webservice.rb', line 6
def host
@host
end
|
#port ⇒ Object
Returns the value of attribute port.
6
7
8
|
# File 'lib/sinatra_webservice.rb', line 6
def port
@port
end
|
Instance Method Details
#delete_response(action, data = "", headers = nil, dest = nil) ⇒ Object
68
69
70
71
|
# File 'lib/sinatra_webservice.rb', line 68
def delete_response(action, data = "", = nil, dest = nil)
data = data.empty? ? "_method=delete" : data += "&_method=delete"
post_response(action, data, , dest)
end
|
#find_free_port ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/sinatra_webservice.rb', line 45
def find_free_port
found = false
attempts = 0
while !found and attempts < 10
puts "\n== Trying port #{@port}"
begin
res = Net::HTTP.start(host,port) do |http|
http.get('/')
end
attempts += 1
@port = @port.succ
rescue Errno::ECONNREFUSED
return @port
end
end
end
|
#get_response(action) ⇒ Object
62
63
64
65
66
|
# File 'lib/sinatra_webservice.rb', line 62
def get_response(action)
res = Net::HTTP.start(self.host, self.port) do |http|
http.get(action)
end
end
|
#post_response(action, data, headers = nil, dest = nil) ⇒ Object
78
79
80
81
82
|
# File 'lib/sinatra_webservice.rb', line 78
def post_response(action, data, = nil, dest = nil)
res = Net::HTTP.start(self.host, self.port) do |http|
http.post(action, data, , dest)
end
end
|
#put_response(action, data = "", headers = nil, dest = nil) ⇒ Object
73
74
75
76
|
# File 'lib/sinatra_webservice.rb', line 73
def put_response(action, data = "", = nil, dest = nil)
data = data.empty? ? "_method=put" : data += "&_method=put"
post_response(action, data, , dest)
end
|
#run! ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/sinatra_webservice.rb', line 32
def run!
if Thread.list.size > 2
Thread.list.first.kill
end
@port = find_free_port
self.current_thread = Thread.new do
SinatraStem.run! :post => @host, :port => @port.to_i
sleep 1
end
end
|
#running? ⇒ Boolean
28
29
30
|
# File 'lib/sinatra_webservice.rb', line 28
def running?
self.current_thread.alive? rescue false
end
|