Sha256: 7acb8984e8184daf39048fe730411d32887634691f0a3a179b720ef6ce54d786

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

require 'rest-client'
require 'slim-rails'

module Lecter
  class DiagnosisController < ActionController::Base
    before_action :format_params, only: :create

    def new
    end

    def show
    end

    def create
      response =
        case diagnosis_params[:method]
        when 'get'
          ::RestClient.get(diagnosis_params[:endpoint], params: format_params)
        when 'post'
          ::RestClient.post(diagnosis_params[:endpoint], format_params)
        end

      return render :new unless response

      prepare_data(response.body)
      render :show
    rescue URI::InvalidURIError
      flash[:error] = 'Wrong url'
      return render :new
    end

    private

    def diagnosis_params
      params.permit(:endpoint, :params, :method)
    end

    def prepare_data(items)
      @lines = []
      @items = items.split(';')
      @items.each do |item|
        file = item.split(' ')[0]
        if @lines.last.is_a?(Hash) && @lines.last.keys.first.to_s == file
          @lines.last[file] = @lines.last[file] << item.split(' ')[1].to_i
        else
          @lines << {"#{file}" => [item.split(' ')[1].to_i]}
        end
      end
    end

    def format_params
      @format_params ||= begin
        if diagnosis_params[:method] == 'get'
          {}
        else
          json_parse(diagnosis_params[:params])
        end.merge(lecter_analysis_parameter)
      rescue JSON::ParserError
        flash[:error] = 'Wrong parameters'
        return render :new
      end
    end

    def lecter_analysis_parameter
      { lecter_analysis: true }
    end

    def json_parse(string)
      string = '{' + string + '}' unless string.match(/\A{.*}\z/)
      string.gsub!('=>', ':').gsub!(/(“|”)/, '"')
      JSON.parse(string)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lecter-0.1.5 app/controllers/lecter/diagnosis_controller.rb
lecter-0.1.4 app/controllers/lecter/diagnosis_controller.rb