Module: HyperResource::Modules::HTTP

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

Overview

HyperResource::Modules::HTTP is included by HyperResource::Link. It provides support for GET, POST, PUT, PATCH, and DELETE. Each method returns a new object which is a kind_of HyperResource.

Instance Method Summary collapse

Instance Method Details

#create(*args) ⇒ Object

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



103
104
105
106
107
# File 'lib/hyper_resource/modules/http.rb', line 103

def create(*args)
  _hr_deprecate('HyperResource::Link#create is deprecated. Please use '+
                '#post instead.')
  post(*args)
end

#deleteObject

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



153
154
155
156
# File 'lib/hyper_resource/modules/http.rb', line 153

def delete
  response = faraday_connection.delete
  new_resource_from_response(response)
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.



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hyper_resource/modules/http.rb', line 87

def get
  ## Adding default_attributes to URL query params is not automatic
  url = FuzzyURL.new(self.url || '')
  query_str = url[:query] || ''
  query_attrs = Hash[ query_str.split('&').map{|p| p.split('=')} ]
  attrs = (self.resource.default_attributes || {}).merge(query_attrs)
  attrs_str = attrs.inject([]){|pairs,(k,v)| pairs<<"#{k}=#{v}"}.join('&')
  if attrs_str != ''
    url = FuzzyURL.new(url.to_hash.merge(:query => attrs_str))
  end
  response = faraday_connection.get(url.to_s)
  new_resource_from_response(response)
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.



143
144
145
146
147
148
149
150
# File 'lib/hyper_resource/modules/http.rb', line 143

def patch(attrs=nil)
  attrs ||= self.resource.attributes.changed_attributes
  attrs = (self.resource.default_attributes || {}).merge(attrs)
  response = faraday_connection.patch do |req|
    req.body = self.resource.adapter.serialize(attrs)
  end
  new_resource_from_response(response)
end

#post(attrs = nil) ⇒ Object

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



111
112
113
114
115
116
117
118
# File 'lib/hyper_resource/modules/http.rb', line 111

def post(attrs=nil)
  attrs ||= self.resource.attributes
  attrs = (self.resource.default_attributes || {}).merge(attrs)
  response = faraday_connection.post do |req|
    req.body = self.resource.adapter.serialize(attrs)
  end
  new_resource_from_response(response)
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.



131
132
133
134
135
136
137
138
# File 'lib/hyper_resource/modules/http.rb', line 131

def put(attrs=nil)
  attrs ||= self.resource.attributes
  attrs = (self.resource.default_attributes || {}).merge(attrs)
  response = faraday_connection.put do |req|
    req.body = self.resource.adapter.serialize(attrs)
  end
  new_resource_from_response(response)
end

#update(*args) ⇒ Object

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



122
123
124
125
126
# File 'lib/hyper_resource/modules/http.rb', line 122

def update(*args)
  _hr_deprecate('HyperResource::Link#update is deprecated. Please use '+
                '#put or #patch instead.')
  put(*args)
end