Module: HyperResource::Modules::HTTP

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

Instance Method Summary collapse

Instance Method Details

#create(*args) ⇒ Object

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



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

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

#deleteObject

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



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

def delete
  self.response = faraday_connection.delete
  finish_up
end

#faraday_connection(url = nil) ⇒ Object

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



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

def faraday_connection(url=nil)
  url ||= URI.join(self.root, self.href)
  key = Digest::MD5.hexdigest({
    'faraday_connection' => {
      'url' => url,
      'headers' => self.headers,
      'ba' => self.auth[:basic]
    }
  }.to_json)
  return Thread.current[key] if Thread.current[key]

  fc = Faraday.new(self.faraday_options.merge(:url => url))
  fc.headers.merge!('User-Agent' => "HyperResource #{HyperResource::VERSION}")
  fc.headers.merge!(self.headers || {})
  if ba=self.auth[:basic]
    fc.basic_auth(*ba)
  end
  Thread.current[key] = fc
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.



12
13
14
15
# File 'lib/hyper_resource/modules/http.rb', line 12

def get
  self.response = faraday_connection.get(self.href || '')
  finish_up
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.



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

def patch(attrs=nil)
  attrs ||= self.attributes.changed_attributes
  self.response = faraday_connection.patch do |req|
    req.body = adapter.serialize(attrs)
  end
  finish_up
end

#post(attrs = nil) ⇒ Object

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



25
26
27
28
29
30
31
# File 'lib/hyper_resource/modules/http.rb', line 25

def post(attrs=nil)
  attrs || self.attributes
  self.response = faraday_connection.post do |req|
    req.body = adapter.serialize(attrs)
  end
  finish_up
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.



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

def put(attrs=nil)
  attrs ||= self.attributes
  self.response = faraday_connection.put do |req|
    req.body = adapter.serialize(attrs)
  end
  finish_up
end

#update(*args) ⇒ Object

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



35
36
37
# File 'lib/hyper_resource/modules/http.rb', line 35

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