Class: BooticClient::Relation

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

Constant Summary collapse

GET =
'get'.freeze
HEAD =
'head'.freeze
OPTIONS =
'options'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, client, complain_on_undeclared_params: self.class.complain_on_undeclared_params) ⇒ Relation

Returns a new instance of Relation.


23
24
25
26
# File 'lib/bootic_client/relation.rb', line 23

def initialize(attrs, client, complain_on_undeclared_params: self.class.complain_on_undeclared_params)
  @attrs, @client = attrs, client
  @complain_on_undeclared_params = complain_on_undeclared_params
end

Class Attribute Details

.complain_on_undeclared_paramsObject


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

def complain_on_undeclared_params
  return true unless instance_variable_defined?('@complain_on_undeclared_params')
  @complain_on_undeclared_params
end

Class Method Details

.expand(href, opts = {}) ⇒ Object


82
83
84
# File 'lib/bootic_client/relation.rb', line 82

def self.expand(href, opts = {})
  WhinyURI.new(href).expand(opts)
end

Instance Method Details

#docsObject


56
57
58
# File 'lib/bootic_client/relation.rb', line 56

def docs
  attrs['docs']
end

#hrefObject


32
33
34
# File 'lib/bootic_client/relation.rb', line 32

def href
  attrs['href']
end

#inspectObject


28
29
30
# File 'lib/bootic_client/relation.rb', line 28

def inspect
  %(#<#{self.class.name} #{attrs.inspect}>)
end

#nameObject


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

def name
  attrs['name']
end

#parametersObject


40
41
42
# File 'lib/bootic_client/relation.rb', line 40

def parameters
  @parameters ||= templated? ? uri.variables : []
end

#run(opts = {}) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bootic_client/relation.rb', line 68

def run(opts = {})
  if templated?
    uri_vars = uri.variables
    payload = opts.each_with_object({}) do |(k,v),memo|
      memo[k] = v unless uri_vars.include?(k.to_s)
    end
    # remove payload vars from URI opts if destructive action
    opts = opts.reject{|k, v| !uri_vars.include?(k.to_s) } if destructive?
    client.request_and_wrap transport_method.to_sym, uri.expand(opts), payload
  else
    client.request_and_wrap transport_method.to_sym, href, opts
  end
end

#templated?Boolean

Returns:

  • (Boolean)

36
37
38
# File 'lib/bootic_client/relation.rb', line 36

def templated?
  !!attrs['templated']
end

#titleObject


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

def title
  attrs['title']
end

#to_hashObject


60
61
62
# File 'lib/bootic_client/relation.rb', line 60

def to_hash
  attrs
end

#transport_methodObject


64
65
66
# File 'lib/bootic_client/relation.rb', line 64

def transport_method
  @transport_method ||= attrs.key?('method') ? attrs['method'].to_s.downcase : GET
end

#typeObject


52
53
54
# File 'lib/bootic_client/relation.rb', line 52

def type
  attrs['type']
end