Sha256: 12d1a55d05de6e79ef3a8b18755bf23634dc363b6aa81d53f7e9e1f2f8394895

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

require 'infrataster/resources/base_resource'
require 'uri'

module Infrataster
  module Resources
    class HttpResource < BaseResource
      Error = Class.new(StandardError)

      attr_reader :uri, :options

      def initialize(url_str, options = {})
        @options = {params: {}, method: :get, headers: {}}.merge(options)
        @uri = URI.parse(url_str)
        if @uri.scheme
          unless %w!http https!.include?(@uri.scheme)
            raise Error, "The provided url, '#{@uri}', is not http or https."
          end
        else
          @uri = URI::HTTP.build([@uri.userinfo, @uri.host, @uri.port, @uri.path, @uri.query, @uri.fragment])
        end
      end

      def to_s
        "http '#{@uri}' with #{@options}"
      end

      def params
        @options[:params]
      end

      def method
        valid_methods = [:get, :head, :delete, :post, :put, :patch]
        unless valid_methods.include?(@options[:method])
          raise Error, "#{@options[:method]} is not supported HTTP method."
        end

        @options[:method]
      end

      def headers
        @options[:headers]
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
infrataster-0.2.0 lib/infrataster/resources/http_resource.rb
infrataster-0.2.0.beta1 lib/infrataster/resources/http_resource.rb
infrataster-0.1.13 lib/infrataster/resources/http_resource.rb
infrataster-0.1.12 lib/infrataster/resources/http_resource.rb