Class: HaveAPI::Context
- Inherits:
-
Object
- Object
- HaveAPI::Context
- Defined in:
- lib/haveapi/context.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#action_instance ⇒ Object
Returns the value of attribute action_instance.
-
#action_prepare ⇒ Object
Returns the value of attribute action_prepare.
-
#args ⇒ Object
Returns the value of attribute args.
-
#authorization ⇒ Object
Returns the value of attribute authorization.
-
#current_user ⇒ Object
Returns the value of attribute current_user.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#layout ⇒ Object
Returns the value of attribute layout.
-
#params ⇒ Object
Returns the value of attribute params.
-
#path ⇒ Object
Returns the value of attribute path.
-
#request ⇒ Object
Returns the value of attribute request.
-
#resource ⇒ Object
Returns the value of attribute resource.
-
#resource_path ⇒ Object
Returns the value of attribute resource_path.
-
#server ⇒ Object
Returns the value of attribute server.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #action_scope ⇒ Object
- #call_path_params(action, obj) ⇒ Object
-
#initialize(server, version: nil, request: nil, resource: [], action: nil, path: nil, args: nil, params: nil, user: nil, authorization: nil, endpoint: nil, resource_path: []) ⇒ Context
constructor
A new instance of Context.
- #path_for(action, args = nil) ⇒ Object
- #path_params_from_args ⇒ Object
- #path_with_params(action, obj) ⇒ Object
- #resolved_path ⇒ Object
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 = @endpoint = endpoint @resource_path = resource_path end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def action @action end |
#action_instance ⇒ Object
Returns the value of attribute action_instance.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def action_instance @action_instance end |
#action_prepare ⇒ Object
Returns the value of attribute action_prepare.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def action_prepare @action_prepare end |
#args ⇒ Object
Returns the value of attribute args.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def args @args end |
#authorization ⇒ Object
Returns the value of attribute authorization.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def @authorization end |
#current_user ⇒ Object
Returns the value of attribute current_user.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def current_user @current_user end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def endpoint @endpoint end |
#layout ⇒ Object
Returns the value of attribute layout.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def layout @layout end |
#params ⇒ Object
Returns the value of attribute params.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def params @params end |
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def path @path end |
#request ⇒ Object
Returns the value of attribute request.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def request @request end |
#resource ⇒ Object
Returns the value of attribute resource.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def resource @resource end |
#resource_path ⇒ Object
Returns the value of attribute resource_path.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def resource_path @resource_path end |
#server ⇒ Object
Returns the value of attribute server.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def server @server end |
#version ⇒ Object
Returns the value of attribute version.
3 4 5 |
# File 'lib/haveapi/context.rb', line 3 def version @version end |
Instance Method Details
#action_scope ⇒ Object
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_args ⇒ Object
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_path ⇒ Object
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 |