Class: ItamaeMitsurin::Resource::HttpRequest

Inherits:
File
  • Object
show all
Defined in:
lib/itamae-mitsurin/resource/http_request.rb

Constant Summary collapse

RedirectLimitExceeded =
Class.new(StandardError)

Instance Attribute Summary

Attributes inherited from Base

#attributes, #current_attributes, #notifications, #recipe, #resource_name, #subscriptions, #updated

Instance Method Summary collapse

Methods inherited from File

#action_create, #action_edit, #set_current_attributes

Methods inherited from Base

#action_nothing, define_attribute, inherited, #initialize, #resource_type, #run

Constructor Details

This class inherits a constructor from ItamaeMitsurin::Resource::Base

Instance Method Details

#action_delete(options) ⇒ Object



65
66
67
# File 'lib/itamae-mitsurin/resource/http_request.rb', line 65

def action_delete(options)
  _action_create(options)
end

#action_get(options) ⇒ Object



69
70
71
# File 'lib/itamae-mitsurin/resource/http_request.rb', line 69

def action_get(options)
  _action_create(options)
end

#action_options(options) ⇒ Object



73
74
75
# File 'lib/itamae-mitsurin/resource/http_request.rb', line 73

def action_options(options)
  _action_create(options)
end

#action_post(options) ⇒ Object



77
78
79
# File 'lib/itamae-mitsurin/resource/http_request.rb', line 77

def action_post(options)
  _action_create(options)
end

#action_put(options) ⇒ Object



81
82
83
# File 'lib/itamae-mitsurin/resource/http_request.rb', line 81

def action_put(options)
  _action_create(options)
end

#fetch_contentObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/itamae-mitsurin/resource/http_request.rb', line 33

def fetch_content
  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 @current_action
    when :delete, :get, :options
      response = http.method(@current_action).call(uri.request_uri, attributes.headers)
    when :post, :put
      response = http.method(@current_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

  response.body
end

#pre_actionObject



19
20
21
22
23
24
25
# File 'lib/itamae-mitsurin/resource/http_request.rb', line 19

def pre_action
  attributes.content = fetch_content

  super

  attributes.exist = true
end

#show_differencesObject



27
28
29
30
31
# File 'lib/itamae-mitsurin/resource/http_request.rb', line 27

def show_differences
  super

  show_content_diff if @temppath && @current_action == :delete
end