module-and-template-store/app/controllers/clients_controller.rb

132 lines
3.2 KiB
Ruby
Raw Normal View History

2014-03-03 07:22:40 +00:00
require "net/http"
require "uri"
require 'json'
2014-02-13 06:51:23 +00:00
class ClientsController < ApplicationController
before_action :set_client, only: [:show, :edit, :update, :destroy]
2014-02-24 06:48:32 +00:00
before_filter :authenticate_user!, except: [:confirm_client, :post_confirmation, :resend_confirmation, :reconfirm_client]
2014-02-13 06:51:23 +00:00
# GET /clients
# GET /clients.json
def index
@clients = Client.all
end
# GET /clients/1
# GET /clients/1.json
def show
end
# GET /clients/new
def new
@client = Client.new
end
# GET /clients/1/edit
def edit
end
2014-02-24 06:48:32 +00:00
def confirm_client
client = Client.confirm_email(params[:token])
2014-03-03 07:22:40 +00:00
if client[:success].eql?("true")
pass = confirm_client_site(client[:id])
if !pass
client[:success] = "false"
end
end
2014-02-24 06:48:32 +00:00
redirect_to post_confirmation_clients_path
2014-03-03 07:22:40 +00:00
if client[:success].eql?("true")
2014-02-24 06:48:32 +00:00
flash[:notice] = "You have confirmed successfully"
else
flash[:notice] = "Error in confirmation please try again."
end
2014-03-03 07:22:40 +00:00
# render :json => client.to_json
2014-02-24 06:48:32 +00:00
end
def resend_confirmation
end
def reconfirm_client
client = Client.send_reconformation_email(params[:email])
redirect_to post_confirmation_clients_path
end
def post_confirmation
end
2014-02-13 06:51:23 +00:00
# POST /clients
# POST /clients.json
def create
@client = Client.new(client_params)
respond_to do |format|
if @client.save
format.html { redirect_to @client, notice: 'Client was successfully created.' }
format.json { render action: 'show', status: :created, location: @client }
else
format.html { render action: 'new' }
format.json { render json: @client.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /clients/1
# PATCH/PUT /clients/1.json
def update
respond_to do |format|
if @client.update(client_params)
format.html { redirect_to @client, notice: 'Client was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @client.errors, status: :unprocessable_entity }
end
end
end
# DELETE /clients/1
# DELETE /clients/1.json
def destroy
@client.destroy
respond_to do |format|
format.html { redirect_to clients_url }
format.json { head :no_content }
end
end
2014-03-03 07:22:40 +00:00
def confirm_client_site(client_id = nil)
if client_id
client = Client.find(client_id)
url = client.url + "/store_confirmation/confirm?site_token=#{client.site_token}"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({:site_token => client.site_token})
response = http.request(request)
success = response.body
success = JSON.parse(success)
if success["success"] == "true"
return true
else
return false
end
else
return false
end
end
2014-02-13 06:51:23 +00:00
private
2014-03-03 07:22:40 +00:00
2014-02-13 06:51:23 +00:00
# Use callbacks to share common setup or constraints between actions.
def set_client
@client = Client.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def client_params
params.require(:client).permit(:site_name, :site_token, :site_id)
end
end