Class: Webmate::Application

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/webmate/application.rb,
lib/webmate.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure(env = nil, &block) ⇒ Object



103
104
105
106
107
# File 'lib/webmate/application.rb', line 103

def configure(env = nil, &block)
  if !env || Webmate.env?(env)
    block.call(configatron)
  end
end

.define_routes(&block) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/webmate/application.rb', line 109

def define_routes(&block)
  settings = Webmate::Application
  unless settings.routes.is_a?(RoutesCollection)
    routes = RoutesCollection.new()
    settings.set(:routes, routes)
  end
  settings.routes.define_routes(&block)

  routes
end

.dump(obj) ⇒ Object



124
125
126
# File 'lib/webmate/application.rb', line 124

def dump(obj)
  Yajl::Encoder.encode(obj)
end

.get_channel_name_for(user_id) ⇒ Object



120
121
122
# File 'lib/webmate/application.rb', line 120

def get_channel_name_for(user_id)
  channel_name = "some-unique-key-for-app-#{user_id}"
end

.load_tasksObject



132
133
134
135
# File 'lib/webmate/application.rb', line 132

def load_tasks
  file_path = Pathname.new(__FILE__)
  load File.join(file_path.dirname, "../../tasks/routes.rake")
end

.restore(str) ⇒ Object



128
129
130
# File 'lib/webmate/application.rb', line 128

def restore(str)
  Yajl::Parser.parse(str)
end

Instance Method Details

#authorized_to_open_connection?(scope = :user) ⇒ Boolean

update this method to create auth restrictions

Returns:

  • (Boolean)


98
99
100
# File 'lib/webmate/application.rb', line 98

def authorized_to_open_connection?(scope = :user)
  return true
end

#params_for_responder(route_info) ⇒ Object

this method prepare data for responder

path: '/', 
metadata: {, 
action: 'index', 
params: { test: true }

}



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/webmate/application.rb', line 60

def params_for_responder(route_info)
  # create unified request info 
  # request_info = { path: '/', metadata: {}, action: 'index', params: { test: true } }
  request_params = parsed_request_params
   = request_params.delete(:metadata)
  {
    path: @request.path,
    metadata:  || {},
    action: route_info[:action],
    params: request_params.merge(route_info[:params]),
    request: @request
  }
end

#parsed_request_paramsObject

and params in url line ?key=value



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/webmate/application.rb', line 76

def parsed_request_params
  request_params = HashWithIndifferentAccess.new
  request_params.merge!(@request.params || {})

  # read post or put params. this will erase  params
  # {code: 123, mode: 123}
  # "code=123&mode=123"
  request_body = @request.body.read
  if request_body.present?
    body_params = begin
      JSON.parse(request_body) # {code: 123, mode: 123}
    rescue JSON::ParserError
      Rack::Utils.parse_nested_query(request_body) # "code=123&mode=123"
    end
  else
    body_params = {}
  end

  request_params.merge(body_params) 
end

#route!(base = settings, pass_block = nil) ⇒ Object

override sinatra’s method



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/webmate/application.rb', line 4

def route!(base = settings, pass_block = nil) 
  transport = @request.websocket? ? 'WS' : 'HTTP'

  route_info = base.routes.match(@request.request_method, transport, @request.path)

  # no route case - use default sinatra's processors
  if !route_info
    route_eval(&pass_block) if pass_block
    route_missing
  end

  if @request.websocket?
    unless authorized_to_open_connection?(route_info[:params][:scope])
      return  [401, {}, []]
    end

    session_id = route_info[:params][:session_id].inspect
    Webmate::Websockets.subscribe(session_id, @request) do |message|
      if route_info = base.routes.match(message['method'], 'WS', message.path)
        request_info = {
          path: message.path,
          metadata: message. || {},
          action: route_info[:action],
          params: message.params.merge(route_info[:params]),
          request: @request
        }
        # here we should create subscriber who can live
        # between messages.. but not between requests.
        response = route_info[:responder].new(request_info).respond

        # result of block will be sent back to user
        response
      end
    end

    # this response not pass to user - so we keep connection alive.
    # passing other response will close connection and socket
    non_pass_response = [-1, {}, []]
    return non_pass_response

  else # HTTP
    # this should return correct Rack response..
    request_info = params_for_responder(route_info)
    response = route_info[:responder].new(request_info).respond

    return response.rack_format
  end
end