Module: HyperResource::Modules::HTTP

Included in:
HyperResource
Defined in:
lib/hyper_resource/modules/http.rb,
lib/hyper_resource/modules/http/wrap_errors.rb

Defined Under Namespace

Classes: WrapErrors

Constant Summary collapse

MAX_COORDINATOR_RETRIES =

A (high) limit to the number of retries a coordinator can ask for. This is to avoid breaking things if we have a buggy coordinator that retries things over and over again.

16

Instance Method Summary collapse

Instance Method Details

#create(*args) ⇒ Object

By default, calls post with the given arguments. Override to change this behavior.



23
24
25
# File 'lib/hyper_resource/modules/http.rb', line 23

def create(*args)
  post(*args)
end

#deleteObject

DELETEs this resource’s href, and returns the response resource.



63
64
65
# File 'lib/hyper_resource/modules/http.rb', line 63

def delete
  execute_request { faraday_connection.delete }
end

#faraday_connection(url = nil) ⇒ Object

Returns a raw Faraday connection to this resource’s URL, with proper headers (including auth).



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hyper_resource/modules/http.rb', line 69

def faraday_connection(url = nil)
  url ||= URI.join(root, href)

  Faraday.new(faraday_options.merge(url: url)) do |builder|
    builder.headers.merge!(headers || {})
    builder.headers['User-Agent'] = Aptible::Resource.configuration
                                                     .user_agent

    if (ba = auth[:basic])
      builder.basic_auth(*ba)
    end

    builder.use WrapErrors # This has to be first!
    builder.request :url_encoded
    builder.adapter Faraday.default_adapter
  end
end

#getObject

Loads and returns the resource pointed to by href. The returned resource will be blessed into its “proper” class, if self.class.namespace != nil.



17
18
19
# File 'lib/hyper_resource/modules/http.rb', line 17

def get
  execute_request { faraday_connection.get(href || '') }
end

#patch(attrs = nil) ⇒ Object

PATCHes this resource’s changed attributes to this resource’s href, and returns the response resource. If attributes are given, patch uses those instead.



55
56
57
58
59
60
# File 'lib/hyper_resource/modules/http.rb', line 55

def patch(attrs = nil)
  attrs ||= attributes.changed_attributes
  execute_request do
    faraday_connection.patch { |req| req.body = adapter.serialize(attrs) }
  end
end

#post(attrs = nil) ⇒ Object

POSTs the given attributes to this resource’s href, and returns the response resource.



29
30
31
32
33
34
# File 'lib/hyper_resource/modules/http.rb', line 29

def post(attrs = nil)
  attrs ||= attributes
  execute_request do
    faraday_connection.post { |req| req.body = adapter.serialize(attrs) }
  end
end

#put(attrs = nil) ⇒ Object

PUTs this resource’s attributes to this resource’s href, and returns the response resource. If attributes are given, put uses those instead.



45
46
47
48
49
50
# File 'lib/hyper_resource/modules/http.rb', line 45

def put(attrs = nil)
  attrs ||= attributes
  execute_request do
    faraday_connection.put { |req| req.body = adapter.serialize(attrs) }
  end
end

#update(*args) ⇒ Object

By default, calls put with the given arguments. Override to change this behavior.



38
39
40
# File 'lib/hyper_resource/modules/http.rb', line 38

def update(*args)
  put(*args)
end