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, resource_path: []) ⇒ Context

Returns a new instance of Context.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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, resource_path: [])
  @server = server
  @version = version
  @request = request
  @resource = resource
  @action = action
  @path = path
  @args = args
  @params = params
  @current_user = user
  @authorization = authorization
  @endpoint = endpoint
  @resource_path = resource_path
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

#resource_pathObject

Returns the value of attribute resource_path.



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

def resource_path
  @resource_path
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

#action_scopeObject



89
90
91
# File 'lib/haveapi/context.rb', line 89

def action_scope
  "#{resource_path.map(&:downcase).join('.')}##{action.action_name.underscore}"
end

#call_path_params(action, obj) ⇒ Object



63
64
65
66
67
68
69
# 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



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 36

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

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

  ret = top_route.dup

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

  ret
end

#path_params_from_argsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/haveapi/context.rb', line 75

def path_params_from_args
  ret = {}
  return ret if args.nil?

  my_args = args.clone

  path.scan(/\{([a-zA-Z\-_]+)\}/) do |match|
    path_param = match.first
    ret[path_param] = my_args.shift
  end

  ret
end

#path_with_params(action, obj) ⇒ Object



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

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

#resolved_pathObject



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

def resolved_path
  return @path unless @args

  ret = @path.dup

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

  ret
end