Class: SinatraWebService

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

Defined Under Namespace

Classes: SinatraStem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SinatraWebService

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_threadObject

Returns the value of attribute current_thread.



7
8
9
# File 'lib/sinatra_webservice.rb', line 7

def current_thread
  @current_thread
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/sinatra_webservice.rb', line 6

def host
  @host
end

#portObject

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 = "", headers = nil, dest = nil)
  data = data.empty? ? "_method=delete" : data += "&_method=delete"
  post_response(action, data, headers, dest)
end

#find_free_portObject



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, headers = nil, dest = nil)
  res = Net::HTTP.start(self.host, self.port) do |http|
    http.post(action, data, headers, 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 = "", headers = nil, dest = nil)
  data = data.empty? ? "_method=put" : data += "&_method=put"
  post_response(action, data, headers, 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

Returns:

  • (Boolean)


28
29
30
# File 'lib/sinatra_webservice.rb', line 28

def running?
  self.current_thread.alive? rescue false
end