Sha256: 6a48c340805fe6bbc3432d2ed54ef21b480ec634b380e1ad2639caa3bfc6f35e

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

require 'itamae'
require 'uri'
require 'net/http'

module Itamae
  module Resource
    class HttpRequest < File
      UrlNotFoundError = Class.new(StandardError)

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

      def pre_action
        uri = URI.parse(attributes.url)
        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

        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

2 entries across 2 versions & 1 rubygems

Version Path
itamae-1.9.2 lib/itamae/resource/http_request.rb
itamae-1.9.1 lib/itamae/resource/http_request.rb