123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- class WebRequestsController < ApplicationController
- skip_before_action :verify_authenticity_token
- skip_before_action :authenticate_user!
- wrap_parameters false
- def handle_request
- user = User.find_by_id(params[:user_id])
- if user
- agent = user.agents.find_by_id(params[:agent_id])
- if agent
- content, status, content_type, headers = agent.trigger_web_request(request)
- if headers.present?
- headers.each do |k, v|
- response.headers[k] = v
- end
- end
- status ||= 200
- if status.to_s.in?(%w[301 302])
- redirect_to(content, allow_other_host: true, status:)
- elsif content.is_a?(String)
- render plain: content, status:, content_type: content_type || 'text/plain'
- elsif content.is_a?(Hash)
- render(json: content, status:)
- else
- head(status)
- end
- else
- render plain: 'agent not found', status: 404
- end
- else
- render plain: 'user not found', status: 404
- end
- end
-
- def update_location
- if user = User.find_by_id(params[:user_id])
- secret = params[:secret]
- user.agents.of_type(Agents::UserLocationAgent).each do |agent|
- agent.trigger_web_request(request) if agent.options[:secret] == secret
- end
- render plain: 'ok'
- else
- render plain: 'user not found', status: :not_found
- end
- end
- end
|