Sha256: eb912019acd4d93a1dd6d27db373910f2024309aca1e6cc3c8529ca5767fc091

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Lecter
  class Requester
    WRONG_URL_MSG = 'Wrong url'

    attr_reader :lines, :error_message

    def initialize(params)
      @method = params[:method]
      @url = params[:url]
      @payload = params[:payload]
      @lines = []
      @headers = params[:headers]
    end

    def call
      return false unless response

      prepare_lines
    rescue URI::InvalidURIError
      @error_message = WRONG_URL_MSG
      false
    rescue RestClient::ExceptionWithResponse => e
      @error_message = e.message
      false
    end

    private

    attr_accessor :method, :url, :payload, :headers

    def prepare_lines
      items.each do |item|
        file, line_number = item.split(' ')
        line_number = line_number.to_i

        if line_belongs_to_last?(file)
          lines.last[file] = lines.last[file] << line_number
        else
          lines << { file.to_s => [line_number] }
        end
      end
    end

    def response
      @response ||= RestClient::Request.execute(
        method: method,
        url: url,
        payload: payload,
        headers: headers
      )
    end

    def items
      @items ||= response.body[3..-1].split(';')
    end

    def line_belongs_to_last?(file)
      lines.last.is_a?(Hash) && lines.last.keys.first.to_s == file
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lecter-0.2.0 lib/lecter/requester.rb