Class: Restify::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/restify/relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, template) ⇒ Relation

Returns a new instance of Relation.



17
18
19
20
# File 'lib/restify/relation.rb', line 17

def initialize(context, template)
  @context  = context
  @template = Addressable::Template.new template
end

Instance Attribute Details

#contextRestify::Context (readonly)

Relation context

Returns:



9
10
11
# File 'lib/restify/relation.rb', line 9

def context
  @context
end

#templateAddressable::Template (readonly)

Relation URI template

Returns:

  • (Addressable::Template)

    URI template



15
16
17
# File 'lib/restify/relation.rb', line 15

def template
  @template
end

Instance Method Details

#==(other) ⇒ Object



53
54
55
# File 'lib/restify/relation.rb', line 53

def ==(other)
  super || (other.is_a?(String) && template.pattern == other)
end

#delete(data = {}, params: {}, **opts) ⇒ Object



34
35
36
# File 'lib/restify/relation.rb', line 34

def delete(data = {}, params: {}, **opts)
  request(**opts, method: :delete, params: data.merge(params))
end

#expand(params) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/restify/relation.rb', line 57

def expand(params)
  params    = convert params
  variables = extract! params

  uri = template.expand variables
  uri.query_values = (uri.query_values || {}).merge params if params.any?

  context.join uri
end

#get(data = {}, params: {}, **opts) ⇒ Object



26
27
28
# File 'lib/restify/relation.rb', line 26

def get(data = {}, params: {}, **opts)
  request(**opts, method: :get, params: data.merge(params))
end

#head(data = {}, params: {}, **opts) ⇒ Object



30
31
32
# File 'lib/restify/relation.rb', line 30

def head(data = {}, params: {}, **opts)
  request(**opts, method: :head, params: data.merge(params))
end

#patch(data = nil, **opts) ⇒ Object



48
49
50
51
# File 'lib/restify/relation.rb', line 48

def patch(data = nil, **opts)
  opts[:data] = data unless opts.key?(:data)
  request(**opts, method: :patch)
end

#patternObject



67
68
69
# File 'lib/restify/relation.rb', line 67

def pattern
  template.pattern
end

#post(data = nil, **opts) ⇒ Object



38
39
40
41
# File 'lib/restify/relation.rb', line 38

def post(data = nil, **opts)
  opts[:data] = data unless opts.key?(:data)
  request(**opts, method: :post)
end

#put(data = nil, **opts) ⇒ Object



43
44
45
46
# File 'lib/restify/relation.rb', line 43

def put(data = nil, **opts)
  opts[:data] = data unless opts.key?(:data)
  request(**opts, method: :put)
end

#request(method:, params: {}, **opts) ⇒ Object



22
23
24
# File 'lib/restify/relation.rb', line 22

def request(method:, params: {}, **opts)
  context.request(method, expand(params), **opts)
end

#to_sObject



71
72
73
# File 'lib/restify/relation.rb', line 71

def to_s
  pattern
end