Sha256: 96330bc940ae8da000812820d542fa3ff1e98f5c62377de29b603898e823d102

Contents?: true

Size: 1.9 KB

Versions: 61

Compression:

Stored size: 1.9 KB

Contents

require 'itamae-mitsurin'
require 'uri'
require 'net/https'

module ItamaeMitsurin
  module Resource
    class HttpRequest < File
      RedirectLimitExceeded = Class.new(StandardError)

      define_attribute :action, default: :get
      define_attribute :headers, type: Hash, default: {}
      define_attribute :message, type: String, default: ""
      define_attribute :redirect_limit, type: Integer, default: 10
      define_attribute :url, type: String, required: true

      def pre_action
        uri = URI.parse(attributes.url)
        response = nil
        redirects_followed = 0

        loop do
          http = Net::HTTP.new(uri.host, uri.port)
          http.use_ssl = true if uri.scheme == "https"

          case attributes.action
          when :delete, :get, :options
            response = http.method(attributes.action).call(uri.request_uri, attributes.headers)
          when :post, :put
            response = http.method(attributes.action).call(uri.request_uri, attributes.message, attributes.headers)
          end

          if response.kind_of?(Net::HTTPRedirection)
            if redirects_followed < attributes.redirect_limit
              uri = URI.parse(response["location"])
              redirects_followed += 1
              ItamaeMitsurin.logger.debug "Following redirect #{redirects_followed}/#{attributes.redirect_limit}"
            else
              raise RedirectLimitExceeded
            end
          else
            break
          end
        end

        attributes.content = response.body

        super
      end

      def action_delete(options)
        action_create(options)
      end

      def action_get(options)
        action_create(options)
      end

      def action_options(options)
        action_create(options)
      end

      def action_post(options)
        action_create(options)
      end

      def action_put(options)
        action_create(options)
      end
    end
  end
end

Version data entries

61 entries across 43 versions & 1 rubygems

Version Path
itamae-mitsurin-0.50 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.49 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.48 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.47 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.46 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.45 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.44 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.43 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.42 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.41 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.40 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.39 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.38 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.37 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.36 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.35 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.34 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.33 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.32 test/test_itamae-mitsurin/resource/test_http_request.rb
itamae-mitsurin-0.31 test/test_itamae-mitsurin/resource/test_http_request.rb