Class: QController

Inherits:
ApiController show all
Defined in:
app/controllers/q_controller.rb

Instance Method Summary collapse

Methods inherited from ApiController

#clear_session, #offline, #protect_against_forgery?, #respond_error

Instance Method Details

#authObject

authenticate an already-registered user, and potentially set their notification method params: auth_id

user: id
password: unencrypted


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/q_controller.rb', line 44

def auth
  @publisher = QPublisher.sys(_sid).where(:auth_id=>params[:auth_id]).first

  quser = @publisher.q_users.sys(_sid).where(:id=>params[:user_id]).first

  if quser && quser.password == params[:password]
    update_notification(quser)
    quser.generate_session_token 
    quser.save
    render :json=>{:error=>nil, :session=>quser.session_token}, :callback=>params[:callback]
  else
    render :json=>{:error=>"Incorrect username or password"}, :callback=>params[:callback]
  end
end

#deregisterObject

deregistered a User ID with a notification method/address - i.e. destroy the user account params: user (the external ID by which the user is known)



109
110
111
112
113
114
# File 'app/controllers/q_controller.rb', line 109

def deregister
  quser = @publisher.q_users.sys(_sid).where(:q_external_id=>params[:user]).first

  quser.destroy
  head :ok
end

#eventObject



158
159
160
161
162
163
164
# File 'app/controllers/q_controller.rb', line 158

def event
  event = QEvent.create(:system_id=>_sid, :q_publisher_id=>@publisher.id, 
                        :topic=>params[:topic], :data=>params[:data], 
                        :klass=>params[:class])

  render :json=>{:event=>event.id} 
end

#extauthObject

call client’s authentication URL with whatever params are passed to this method returns: if auth URL returns status code 200, return the same as “register” would return else, return whatever the auth URL returned



36
37
38
# File 'app/controllers/q_controller.rb', line 36

def extauth
  head :error 
end

#messagesObject

notification messages params: auth_id, user_id, session_token optional: after (date/time)

status
page (paginated results)
per (number per page, default 10)
limit (default 100)
min_id (IDs must be greater than this)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/q_controller.rb', line 13

def messages
  s = @quser.q_messages.sys(_sid).includes(:q_publisher).order("id desc")
  s = s.where(["created_at > ?", params[:after]]) if params[:after]
  s = s.where(["status = ?", params[:status]]) if params[:status]
  s = s.where(["id > ?", params[:min_id]]) if params[:min_id]
  s = s.limit(params[:limit] || 100) 
  s = s.page(params[:page]).per(params[:per] || 10) if params[:page]
  
  r = []
  s.all.each do |ss|
    r << { :id=>ss.id, :status=>ss.status, :h1=>ss.h1, :h2=>ss.h2, :body=>ss.body, :publisher=>ss.q_publisher.name, :created_at=>ss.created_at }
    unless params[:nomark]
      ss.status = "OK"
      ss.save
    end
  end 
  logger.debug r.to_json
  render :json=>r, :callback=>params[:callback]
end

#registerObject

associate a user ID with a notification method/address params: user (the external ID by which the user is known)

auth_id (the publisher auth ID)
method (the notification method)
address (the address at which they can be notified)
encrypted_password (optional, to allow direct authentication, should already be bcrypted)
password (optional, to allow direct authentication, will be bcrypted)


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 'app/controllers/q_controller.rb', line 79

def register
  quser = @publisher.q_users.sys(_sid).where(:q_external_id=>params[:user]).first
  
  if quser
    if quser.password != params[:password]
      head :forbidden
      return
    else
      quser.password = params[:new_password] if params[:new_password]
    end
  else
    quser = QUser.new
    quser.system_id = _sid
    quser.q_publisher = @publisher.id
    quser.q_external_id = params[:user]
    quser.encrypted_password = params[:encrypted_password] if params[:encrypted_password]
  end

  update_notification(quser) 
  quser.save
  unless quser.q_external_id
    quser.q_external_id = quser.id
    quser.save
  end
  render :json=>{:id=>quser.q_external_id}, :callback=>params[:callback]
end

#subscribeObject

subscribe to a topic (i.e. receive a notification when that topic happens) params: user (the external ID by which the user is known)

topic (the topic to which is being subscribed)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/controllers/q_controller.rb', line 119

def subscribe
  quser = @publisher.q_users.sys(_sid).where(:q_external_id=>params[:user]).first

  unless quser
   head :bad_request
   return
  end
  
  qs = QSubscription.sys(_sid).where(:q_publisher_id=>@publisher.id).where(:q_user_id=>quser.id).where(:topic=>params[:topic]).first
  unless qs
    qs = QSubscription.new
    qs.system_id = _sid
    qs.q_publisher = @publisher
    qs.q_user = quser
    qs.topic = params[:topic]
    qs.save
  end

  render :json=>{:subscription=>qs.id}
end

#tokenObject

send system_id and password to get a token which can be use for token authentication (if publisher auth method is token) or encrypting requests (if publisher auth method is digest) params: auth_id, auth_secret returns: token



62
63
64
65
66
67
68
69
70
# File 'app/controllers/q_controller.rb', line 62

def token
  publisher = QPublisher.sys(_sid).where(:auth_id=>params[:auth_id]).first

  if publisher && publisher.auth_secret == params[:auth_secret]
    render :json=>{:auth_id=>publisher.auth_id, :token=>publisher.token}
  else
    head :forbidden
  end
end

#unsubscribeObject

unsubscribe from topic (i.e. stop receiving notifications when a topic happens) params: user (the external ID by which the user is known)

topic (the topic to which is being unsubscribed)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/q_controller.rb', line 143

def unsubscribe
  quser = @publisher.q_users.sys(_sid).where(:q_external_id=>params[:user]).first

  unless quser
   head :bad_request
   return
  end

  qs = QSubscription.sys(_sid).where(:q_publisher_id=>@publisher.id).where(:q_user_id=>quser.id).where(:topic=>params[:topic]).first

  qs.destroy    

  head :ok
end