Class: Sawyer::Relation
- Inherits:
-
Object
- Object
- Sawyer::Relation
- Defined in:
- lib/sawyer/relation.rb
Defined Under Namespace
Classes: Map
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#available_methods ⇒ Object
readonly
Returns the value of attribute available_methods.
-
#href_template ⇒ Object
readonly
Returns the value of attribute href_template.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.from_link(agent, name, options) ⇒ Object
Public: Builds a single Relation from the given options.
-
.from_links(agent, index, rels = Map.new) ⇒ Object
Public: Builds an index of Relations from the value of a ‘_links` property in a resource.
Instance Method Summary collapse
-
#call(data = nil, options = nil) ⇒ Object
Public: Makes an API request with the curent Relation.
-
#delete(data = nil, options = nil) ⇒ Object
Public: Makes an API request with the curent Relation using DELETE.
-
#get(options = nil) ⇒ Object
Public: Makes an API request with the curent Relation using GET.
-
#head(options = nil) ⇒ Object
Public: Makes an API request with the curent Relation using HEAD.
- #href(options = nil) ⇒ Object
-
#initialize(agent, name, href, method = nil) ⇒ Relation
constructor
A Relation represents an available next action for a resource.
- #inspect ⇒ Object
-
#options(data = nil, opt = nil) ⇒ Object
Public: Makes an API request with the curent Relation using OPTIONS.
-
#patch(data = nil, options = nil) ⇒ Object
Public: Makes an API request with the curent Relation using PATCH.
-
#post(data = nil, options = nil) ⇒ Object
Public: Makes an API request with the curent Relation using POST.
-
#put(data = nil, options = nil) ⇒ Object
Public: Makes an API request with the curent Relation using PUT.
Constructor Details
#initialize(agent, name, href, method = nil) ⇒ Relation
A Relation represents an available next action for a resource.
agent - The Sawyer::Agent that made the request. name - The Symbol name of the relation. href - The String URL of the location of the next action. method - The Symbol HTTP method. Default: :get
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/sawyer/relation.rb', line 99 def initialize(agent, name, href, method = nil) @agent = agent @name = name.to_sym @href = href begin @href_template = URITemplate.new(href.to_s) rescue URITemplate::RFC6570::Invalid => e end methods = nil if method.is_a? String if method.size.zero? method = nil else method.downcase! methods = method.split(',').map! do |m| m.strip! m.to_sym end method = methods.first end end @method = (method || :get).to_sym @available_methods = Set.new methods || [@method] end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
47 48 49 |
# File 'lib/sawyer/relation.rb', line 47 def agent @agent end |
#available_methods ⇒ Object (readonly)
Returns the value of attribute available_methods.
47 48 49 |
# File 'lib/sawyer/relation.rb', line 47 def available_methods @available_methods end |
#href_template ⇒ Object (readonly)
Returns the value of attribute href_template.
47 48 49 |
# File 'lib/sawyer/relation.rb', line 47 def href_template @href_template end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
47 48 49 |
# File 'lib/sawyer/relation.rb', line 47 def method @method end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
47 48 49 |
# File 'lib/sawyer/relation.rb', line 47 def name @name end |
Class Method Details
.from_link(agent, name, options) ⇒ Object
Public: Builds a single Relation from the given options. These are usually taken from a ‘_links` property in a resource.
agent - The Sawyer::Agent that made the request. name - The Symbol name of the Relation. options - A Hash containing the other Relation properties.
:href - The String URL of the next action's location.
:method - The optional String HTTP method.
Returns a Relation.
84 85 86 87 88 89 90 91 |
# File 'lib/sawyer/relation.rb', line 84 def self.from_link(agent, name, ) case when Hash new agent, name, [:href], [:method] when String new agent, name, end end |
.from_links(agent, index, rels = Map.new) ⇒ Object
Public: Builds an index of Relations from the value of a ‘_links` property in a resource. :get is the default method. Any links with multiple specified methods will get multiple relations created.
index - The Hash mapping Relation names to the Hash Relation
options.
rels - A Relation::Map to store the Relations.
Returns a Relation::Map
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/sawyer/relation.rb', line 62 def self.from_links(agent, index, rels = Map.new) if index.is_a?(Array) raise ArgumentError, "Links must be a hash of rel => {_href => '...'}: #{index.inspect}" end index.each do |name, | rels << from_link(agent, name, ) end if index rels end |
Instance Method Details
#call(data = nil, options = nil) ⇒ Object
Public: Makes an API request with the curent Relation.
data - The Optional Hash or Resource body to be sent. :get or :head
requests can have no body, so this can be the options Hash
instead.
options - Hash of option to configure the API request.
:headers - Hash of API headers to set.
:query - Hash of URL query params to set.
:method - Symbol HTTP method.
Raises ArgumentError if the :method value is not in @available_methods. Returns a Sawyer::Response.
254 255 256 257 258 259 260 261 |
# File 'lib/sawyer/relation.rb', line 254 def call(data = nil, = nil) m = && [:method] if m && !@agent.allow_undefined_methods? && !@available_methods.include?(m == :head ? :get : m) raise ArgumentError, "method #{m.inspect} is not available: #{@available_methods.to_a.inspect}" end @agent.call m || @method, @href_template, data, end |
#delete(data = nil, options = nil) ⇒ Object
Public: Makes an API request with the curent Relation using DELETE.
data - The Optional Hash or Resource body to be sent. options - Hash of option to configure the API request.
:headers - Hash of API headers to set.
:query - Hash of URL query params to set.
:method - Symbol HTTP method.
Returns a Sawyer::Response.
215 216 217 218 219 |
# File 'lib/sawyer/relation.rb', line 215 def delete(data = nil, = nil) ||= {} [:method] = :delete call data, end |
#get(options = nil) ⇒ Object
Public: Makes an API request with the curent Relation using GET.
data - The Optional Hash or Resource body to be sent. :get or :head
requests can have no body, so this can be the options Hash
instead.
options - Hash of option to configure the API request.
:headers - Hash of API headers to set.
:query - Hash of URL query params to set.
:method - Symbol HTTP method.
Returns a Sawyer::Response.
155 156 157 158 159 |
# File 'lib/sawyer/relation.rb', line 155 def get( = nil) ||= {} [:method] = :get call end |
#head(options = nil) ⇒ Object
Public: Makes an API request with the curent Relation using HEAD.
data - The Optional Hash or Resource body to be sent. :get or :head
requests can have no body, so this can be the options Hash
instead.
options - Hash of option to configure the API request.
:headers - Hash of API headers to set.
:query - Hash of URL query params to set.
:method - Symbol HTTP method.
Returns a Sawyer::Response.
138 139 140 141 142 |
# File 'lib/sawyer/relation.rb', line 138 def head( = nil) ||= {} [:method] = :head call end |
#href(options = nil) ⇒ Object
236 237 238 239 240 |
# File 'lib/sawyer/relation.rb', line 236 def href( = nil) return @href if @href_template.nil? method = @href_template.method(:expand) ? method.call() : method.call end |
#inspect ⇒ Object
263 264 265 |
# File 'lib/sawyer/relation.rb', line 263 def inspect %(#<#{self.class}: #{@name}: #{@method} #{@href_template}>) end |
#options(data = nil, opt = nil) ⇒ Object
Public: Makes an API request with the curent Relation using OPTIONS.
data - The Optional Hash or Resource body to be sent. options - Hash of option to configure the API request.
:headers - Hash of API headers to set.
:query - Hash of URL query params to set.
:method - Symbol HTTP method.
Returns a Sawyer::Response.
230 231 232 233 234 |
# File 'lib/sawyer/relation.rb', line 230 def (data = nil, opt = nil) opt ||= {} opt[:method] = :options call data, opt end |
#patch(data = nil, options = nil) ⇒ Object
Public: Makes an API request with the curent Relation using PATCH.
data - The Optional Hash or Resource body to be sent. options - Hash of option to configure the API request.
:headers - Hash of API headers to set.
:query - Hash of URL query params to set.
:method - Symbol HTTP method.
Returns a Sawyer::Response.
200 201 202 203 204 |
# File 'lib/sawyer/relation.rb', line 200 def patch(data = nil, = nil) ||= {} [:method] = :patch call data, end |
#post(data = nil, options = nil) ⇒ Object
Public: Makes an API request with the curent Relation using POST.
data - The Optional Hash or Resource body to be sent. options - Hash of option to configure the API request.
:headers - Hash of API headers to set.
:query - Hash of URL query params to set.
:method - Symbol HTTP method.
Returns a Sawyer::Response.
170 171 172 173 174 |
# File 'lib/sawyer/relation.rb', line 170 def post(data = nil, = nil) ||= {} [:method] = :post call data, end |
#put(data = nil, options = nil) ⇒ Object
Public: Makes an API request with the curent Relation using PUT.
data - The Optional Hash or Resource body to be sent. options - Hash of option to configure the API request.
:headers - Hash of API headers to set.
:query - Hash of URL query params to set.
:method - Symbol HTTP method.
Returns a Sawyer::Response.
185 186 187 188 189 |
# File 'lib/sawyer/relation.rb', line 185 def put(data = nil, = nil) ||= {} [:method] = :put call data, end |