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



16
# File 'lib/hyper_resource/modules/http.rb', line 16

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

#deleteObject



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

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).



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

def faraday_connection(url=nil)
  url ||= URI.join(self.root, self.href)
  key = "faraday_connection_#{url}"
  return Thread.current[key] if Thread.current[key]

  fc = Faraday.new(: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.



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

def get
  self.response = faraday_connection.get(self.href || '')
  finish_up
end

#post(params = nil) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/hyper_resource/modules/http.rb', line 17

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

#put(params = nil) ⇒ Object



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

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

#update(*args) ⇒ Object



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

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