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.
-
#server ⇒ Object
Returns the value of attribute server.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #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) ⇒ Context
constructor
A new instance of Context.
- #path_for(action, args = nil) ⇒ 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) ⇒ 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 = @endpoint = endpoint 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 |
#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
#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_path ⇒ Object
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 |