Sha256: ec76249a839e30070d2437e4f6d991a1cc7391aac866b4ede73d4260f1578622

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 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/event/action'
require 'evostream/event/response/response'
require 'net/http'
require 'evostream/event/response/mock'

# Primary command to gem
module Evostream
  def self.send_command(cmd)
    Evostream.logger "CMD : #{cmd}"
    response = case Evostream::Service.response_format
               when :hash
                 Evostream::Response::FormatHash
               else
                 Evostream::Response::FormatJSON
               end.new(prepare_request(cmd))
    response.message
  end

  def self.logger(message)
    Rails.logger.debug "[#{Evostream::GEM_NAME}] #{message}" if defined?(Rails)
  end

  def self.prepare_request(cmd)
    if Evostream::Service.environment.eql?(:test)
      Evostream.request_test(cmd)
    else
      Evostream.request_real(URI.parse("#{Evostream::Service.uri_in}/#{cmd}"))
    end
  end

  class << self
    private_class_method

    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

1 entries across 1 versions & 1 rubygems

Version Path
evostream-event-0.2.7.pre.44 lib/evostream/event.rb