Class: Itamae::Resource::HttpRequest

Inherits:
File
  • Object
show all
Defined in:
lib/itamae/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 Itamae::Resource::Base

Instance Method Details

#action_delete(options) ⇒ Object



68
69
70
# File 'lib/itamae/resource/http_request.rb', line 68

def action_delete(options)
  _action_create(options)
end

#action_get(options) ⇒ Object



72
73
74
# File 'lib/itamae/resource/http_request.rb', line 72

def action_get(options)
  _action_create(options)
end

#action_options(options) ⇒ Object



76
77
78
# File 'lib/itamae/resource/http_request.rb', line 76

def action_options(options)
  _action_create(options)
end

#action_post(options) ⇒ Object



80
81
82
# File 'lib/itamae/resource/http_request.rb', line 80

def action_post(options)
  _action_create(options)
end

#action_put(options) ⇒ Object



84
85
86
# File 'lib/itamae/resource/http_request.rb', line 84

def action_put(options)
  _action_create(options)
end

#fetch_contentObject



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
64
65
66
# File 'lib/itamae/resource/http_request.rb', line 36

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 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
        Itamae.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/resource/http_request.rb', line 19

def pre_action
  attributes.exist = true
  attributes.content = fetch_content

  send_tempfile
  compare_file
end

#show_differencesObject



27
28
29
30
31
32
33
34
# File 'lib/itamae/resource/http_request.rb', line 27

def show_differences
  current.mode    = current.mode.rjust(4, '0') if current.mode
  attributes.mode = attributes.mode.rjust(4, '0') if attributes.mode

  super

  show_content_diff
end