Class: HaveAPI::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, version: nil, request: nil, resource: [], action: nil, path: nil, args: nil, params: nil, user: nil, authorization: nil, endpoint: nil) ⇒ Context

Returns a new instance of Context.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/haveapi/context.rb', line 7

def initialize(server, version: nil, request: nil, resource: [], action: nil,
              path: nil, args: nil, params: nil, user: nil,
              authorization: nil, endpoint: nil)
  @server = server
  @version = version
  @request = request
  @resource = resource
  @action = action
  @path = path
  @args = args
  @params = params
  @current_user = user
  @authorization = authorization
  @endpoint = endpoint
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



3
4
5
# File 'lib/haveapi/context.rb', line 3

def action
  @action
end

#action_instanceObject

Returns the value of attribute action_instance.



3
4
5
# File 'lib/haveapi/context.rb', line 3

def action_instance
  @action_instance
end

#action_prepareObject

Returns the value of attribute action_prepare.



3
4
5
# File 'lib/haveapi/context.rb', line 3

def action_prepare
  @action_prepare
end

#argsObject

Returns the value of attribute args.



3
4
5
# File 'lib/haveapi/context.rb', line 3

def args
  @args
end

#authorizationObject

Returns the value of attribute authorization.



3
4
5
# File 'lib/haveapi/context.rb', line 3

def authorization
  @authorization
end

#current_userObject

Returns the value of attribute current_user.



3
4
5
# File 'lib/haveapi/context.rb', line 3

def current_user
  @current_user
end

#endpointObject

Returns the value of attribute endpoint.



3
4
5
# File 'lib/haveapi/context.rb', line 3

def endpoint
  @endpoint
end

#layoutObject

Returns the value of attribute layout.



3
4
5
# File 'lib/haveapi/context.rb', line 3

def layout
  @layout
end

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'lib/haveapi/context.rb', line 3

def params
  @params
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/haveapi/context.rb', line 3

def path
  @path
end

#requestObject

Returns the value of attribute request.



3
4
5
# File 'lib/haveapi/context.rb', line 3

def request
  @request
end

#resourceObject

Returns the value of attribute resource.



3
4
5
# File 'lib/haveapi/context.rb', line 3

def resource
  @resource
end

#serverObject

Returns the value of attribute server.



3
4
5
# File 'lib/haveapi/context.rb', line 3

def server
  @server
end

#versionObject

Returns the value of attribute version.



3
4
5
# File 'lib/haveapi/context.rb', line 3

def version
  @version
end

Instance Method Details

#call_path_params(action, obj) ⇒ Object



63
64
65
66
67
68
# File 'lib/haveapi/context.rb', line 63

def call_path_params(action, obj)
  ret = params && action.resolve_path_params(obj)

  return [ret] if ret && !ret.is_a?(Array)
  ret
end

#path_for(action, args = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/haveapi/context.rb', line 35

def path_for(action, args=nil)
  top_module = Kernel
  top_route = @server.routes[@version]

  action.to_s.split('::').each do |name|
    top_module = top_module.const_get(name)

    begin
      top_module.obj_type

    rescue NoMethodError
      next
    end

    if top_module.obj_type == :resource
      top_route = top_route[:resources][top_module]
    else
      top_route = top_route[:actions][top_module]
    end
  end

  ret = top_route.dup

  args.each { |arg| resolve_arg!(ret, arg) } if args

  ret
end

#path_with_params(action, obj) ⇒ Object



70
71
72
# File 'lib/haveapi/context.rb', line 70

def path_with_params(action, obj)
  path_for(action, call_path_params(action, obj))
end

#resolved_pathObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/haveapi/context.rb', line 23

def resolved_path
  return @path unless @args

  ret = @path.dup

  @args.each do |arg|
    resolve_arg!(ret, arg)
  end

  ret
end