Sha256: ef5d1a60137fd3bd9ffa17b7fdc374bcd068f04d24a2c7afd185fa61cb6d8891

Contents?: true

Size: 1.7 KB

Versions: 18

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require 'active_support'
require 'evostream/event/info'
require 'evostream/event/service'
require 'evostream/event/commands'
require 'evostream/event/event'
require 'evostream/action/action'
require 'evostream/event/response/response'
require 'net/http'
require 'evostream/event/response/mock'

# :reek:NilCheck

# Primary command to gem
module Evostream
  def self.send_command(cmd, uri_in = nil)
    Evostream.logger "CMD : #{cmd}"
    Evostream::Responses.new(prepare_request(cmd, uri_in)).message
  end

  def self.logger(message)
    Rails.logger.debug "[#{Evostream::GEM_NAME}] #{message}" \
      if Evostream::Service.environment.eql?('development') && defined?(Rails)
  end

  def self.prepare_request(cmd, uri_in = nil)
    env = Evostream::Service.environment.to_sym
    Evostream.logger "ENV  ------> #{env}"
    case env
    when :test
      Evostream.request_test(cmd)
    when :development, :production
      prepare_request_real(uri_in.nil? ? Evostream::Service.uri_in : uri_in,
                           cmd)
    end
  end

  def self.prepare_request_real(url, cmd)
    Evostream.request_real(URI.parse("#{url}/#{cmd}"))
  end

  class << self
    def request_test(command)
      json = JSON.parse(File.read(find_fixture(command)))
      Net::HTTPSuccess.mock(json)
    end

    def request_real(uri)
      http = Net::HTTP.new(uri.host, uri.port)
      http.request(Net::HTTP::Get.new(uri.request_uri))
    end

    def path_fixture
      File.realpath(File.join(File.dirname(__FILE__), '..', '..', 'spec',
                              'support', 'fixtures'))
    end

    def find_fixture(command)
      File.join(path_fixture, "#{command.split('?')[0].underscore}.json")
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
evostream-event-2.0.3 lib/evostream/event.rb
evostream-event-2.0.3.pre.1265906291 lib/evostream/event.rb
evostream-event-2.0.3.pre.1265756691 lib/evostream/event.rb
evostream-event-2.0.3.pre.1265752332 lib/evostream/event.rb
evostream-event-2.0.2 lib/evostream/event.rb
evostream-event-2.0.2.pre.994185738 lib/evostream/event.rb
evostream-event-2.0.0.pre.994170151 lib/evostream/event.rb
evostream-event-2.0.0.pre.994165179 lib/evostream/event.rb
evostream-event-2.0.0.pre.994159616 lib/evostream/event.rb
evostream-event-2.0.0.pre.856454391 lib/evostream/event.rb
evostream-event-2.0.0.pre.856447460 lib/evostream/event.rb
evostream-event-2.0.0.pre.856441003 lib/evostream/event.rb
evostream-event-2.0.0.pre.816370746 lib/evostream/event.rb
evostream-event-2.0.0.pre.809444619 lib/evostream/event.rb
evostream-event-1.0.3 lib/evostream/event.rb
evostream-event-1.0.3.pre.95 lib/evostream/event.rb
evostream-event-1.0.3.pre.93 lib/evostream/event.rb
evostream-event-1.0.3.pre.91 lib/evostream/event.rb