Class: Itamae::Resource::HttpRequest

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

Constant Summary collapse

RedirectLimitExceeded =
Class.new(StandardError)
HTTPClientError =
Class.new(StandardError)
HTTPServerError =
Class.new(StandardError)
HTTPUnknownError =
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, #do_not_run_because_of_not_if?, #do_not_run_because_of_only_if?, inherited, #initialize, #resource_type, #run

Constructor Details

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

Instance Method Details

#action_delete(options) ⇒ Object



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

def action_delete(options)
  _action_create(options)
end

#action_get(options) ⇒ Object



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

def action_get(options)
  _action_create(options)
end

#action_options(options) ⇒ Object



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

def action_options(options)
  _action_create(options)
end

#action_post(options) ⇒ Object



89
90
91
# File 'lib/itamae/resource/http_request.rb', line 89

def action_post(options)
  _action_create(options)
end

#action_put(options) ⇒ Object



93
94
95
# File 'lib/itamae/resource/http_request.rb', line 93

def action_put(options)
  _action_create(options)
end

#fetch_contentObject



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
67
68
69
70
71
72
73
74
75
# File 'lib/itamae/resource/http_request.rb', line 38

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

    case response
    when Net::HTTPSuccess
      break
    when 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
    when Net::HTTPClientError
      raise HTTPClientError
    when Net::HTTPServerError
      raise HTTPServerError
    else
      raise HTTPUnknownError
    end
  end

  response.body
end

#pre_actionObject



21
22
23
24
25
26
27
# File 'lib/itamae/resource/http_request.rb', line 21

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

  send_tempfile
  compare_file
end

#show_differencesObject



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

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