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

Modules: 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
CONTENT_TYPE_HEADERS =
{
  'Content-Type' => 'application/json; charset=utf-8'
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.http_clientObject (readonly)

Returns the value of attribute http_client.



19
20
21
# File 'lib/hyper_resource/modules/http.rb', line 19

def http_client
  @http_client
end

Class Method Details

.initialize_http_client!Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/hyper_resource/modules/http.rb', line 21

def initialize_http_client!
  @http_client = HTTPClient.new.tap do |c|
    c.cookie_manager = nil
    c.connect_timeout = 30
    c.send_timeout = 45
    c.receive_timeout = 30
    c.keep_alive_timeout = 15
    c.ssl_config.set_default_paths
  end
end

Instance Method Details

#create(*args) ⇒ Object

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



54
55
56
# File 'lib/hyper_resource/modules/http.rb', line 54

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

#deleteObject

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



109
110
111
112
113
# File 'lib/hyper_resource/modules/http.rb', line 109

def delete
  execute_request('DELETE') do |uri, headers|
    HTTP.http_client.delete(uri, header: headers)
  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.



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

def get
  execute_request('GET') do |uri, headers|
    HTTP.http_client.get(
      uri,
      follow_redirect: true,
      header: headers
    )
  end
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.



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hyper_resource/modules/http.rb', line 96

def patch(attrs = nil)
  attrs ||= attributes.changed_attributes

  execute_request('PATCH') do |uri, headers|
    HTTP.http_client.patch(
      uri,
      body: adapter.serialize(attrs),
      header: headers.merge(CONTENT_TYPE_HEADERS)
    )
  end
end

#post(attrs = nil) ⇒ Object

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



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hyper_resource/modules/http.rb', line 60

def post(attrs = nil)
  attrs ||= attributes

  execute_request('POST') do |uri, headers|
    HTTP.http_client.post(
      uri,
      body: adapter.serialize(attrs),
      header: headers.merge(CONTENT_TYPE_HEADERS)
    )
  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.



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hyper_resource/modules/http.rb', line 81

def put(attrs = nil)
  attrs ||= attributes

  execute_request('PUT') do |uri, headers|
    HTTP.http_client.put(
      uri,
      body: adapter.serialize(attrs),
      header: headers.merge(CONTENT_TYPE_HEADERS)
    )
  end
end

#update(*args) ⇒ Object

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



74
75
76
# File 'lib/hyper_resource/modules/http.rb', line 74

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