Sha256: a002ecf1a57ec2c2b1a2fe41a5e4b9737c8137dd2348f402f8fa922b6e3beace

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

require 'hurley'
require 'nori'

module PortfolioManager
  module REST
    ##
    # Manage HTTP requests
    class Request
      BASE_URL  = 'https://portfoliomanager.energystar.gov'
      LIVE_PATH = '/ws'
      TEST_PATH = '/wstest'

      attr_reader :client, :path, :request_method, :parser
      attr_accessor :options
      ##
      # @param [PortfolioManager::Client] client
      # @param [Symbol, String] request_method
      # @param [String] path
      # @param [Hash] options used for creating query params and headers
      def initialize(client, request_method, path, options)
        @client = client
        @path = path
        @options = options
        @request_method = request_method
        @conn = Hurley::Client.new(BASE_URL)
        @parser = Nori.new
        setup_client
      end

      ##
      # @return [Hash]
      def perform
        parser.parse(response_body)
      end

      private

      ##
      # @return [String]
      def response_body
        @conn.public_send(request_method, api_environment + path).body
      end

      def setup_client
        set_header
        set_query
        set_basic_authentication
      end

      def set_header
        @conn.header[:user_agent] = 'Ruby PortfolioManager API Client'
        options[:header].each do |key, value|
          @conn.header[key] = value
        end unless options[:header].nil?
      end

      def set_query
        options[:query].each do |key, value|
          @conn.query[key] = value
        end unless options[:query].nil?
      end

      def set_basic_authentication
        @conn.url.user = client.username
        @conn.url.password = client.password
      end

      def api_environment
        if client.live
          LIVE_PATH
        else
          TEST_PATH
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
portfolio_manager-0.2.0 lib/portfolio_manager/rest/request.rb
portfolio_manager-0.1.0 lib/portfolio_manager/rest/request.rb