Class: Swagger::Shell::ApiStruct
- Inherits:
-
Object
- Object
- Swagger::Shell::ApiStruct
- Defined in:
- lib/swagger/shell/api_struct.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
Instance Method Summary collapse
- #add_api(path_keys, method, api_info = nil) ⇒ Object
- #add_api_module(method, api_info) ⇒ Object
- #api_ancestors ⇒ Object
- #api_key ⇒ Object
- #api_list ⇒ Object
- #api_methods ⇒ Object
- #api_url ⇒ Object
- #child(path_key) ⇒ Object
-
#initialize(key, parent = nil) ⇒ ApiStruct
constructor
A new instance of ApiStruct.
- #method_key ⇒ Object
- #root? ⇒ Boolean
- #user ⇒ Object
Constructor Details
#initialize(key, parent = nil) ⇒ ApiStruct
Returns a new instance of ApiStruct.
34 35 36 37 38 |
# File 'lib/swagger/shell/api_struct.rb', line 34 def initialize(key, parent = nil) @key = key @parent = parent @children = [] end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
32 33 34 |
# File 'lib/swagger/shell/api_struct.rb', line 32 def children @children end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
32 33 34 |
# File 'lib/swagger/shell/api_struct.rb', line 32 def parent @parent end |
Class Method Details
Instance Method Details
#add_api(path_keys, method, api_info = nil) ⇒ Object
52 53 54 55 56 |
# File 'lib/swagger/shell/api_struct.rb', line 52 def add_api(path_keys, method, api_info = nil) find_or_create_api_struct(path_keys).tap do |api_struct| api_struct.add_api_module(method, api_info) if api_struct end end |
#add_api_module(method, api_info) ⇒ Object
88 89 90 91 |
# File 'lib/swagger/shell/api_struct.rb', line 88 def add_api_module(method, api_info) extend self.class.module_class(method) send("#{method}_api_info=", api_info) end |
#api_ancestors ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/swagger/shell/api_struct.rb', line 80 def api_ancestors loop.inject([self]) do |parents| break parents if parents.last.parent.nil? parents << parents.last.parent parents end.reverse end |
#api_key ⇒ Object
44 45 46 |
# File 'lib/swagger/shell/api_struct.rb', line 44 def api_key @key end |
#api_list ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/swagger/shell/api_struct.rb', line 58 def api_list @children.each_with_object({}) do |key, hash| hash.merge!(instance_variable_get("@#{key}").api_list) end.tap do |hash| api_methods.each do |method| hash[(api_ancestors.map(&:method_key) << method).join(".")] = send("#{method}_api_info") end end end |
#api_methods ⇒ Object
73 74 75 76 77 78 |
# File 'lib/swagger/shell/api_struct.rb', line 73 def api_methods %i[get post put delete].map do |method| # (api_ancestors.map(&:method_key) << method).join(".") if singleton_class.include? self.class.module_class(method) method if singleton_class.include? self.class.module_class(method) end.compact end |
#api_url ⇒ Object
68 69 70 71 |
# File 'lib/swagger/shell/api_struct.rb', line 68 def api_url # TODO: Is there simply + no problem? If possible, pass from outside. Swagger::Shell.config_api.ignore_top_url + api_ancestors.map(&:api_key).join("/") end |
#child(path_key) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/swagger/shell/api_struct.rb', line 93 def child(path_key) # not implement url with id (i.g.: hoge/111/age) # TODO: (i.g.: api.hoge(111).age.post ) return nil if /\A\{\w+\}\Z/.match(path_key) unless respond_to? path_key instance_variable_set("@#{path_key}", ApiStruct.new(path_key,self)) instance_eval <<-RUBY def self.#{path_key} @#{path_key} end RUBY @children << path_key.to_sym end instance_variable_get("@#{path_key}") end |
#method_key ⇒ Object
48 49 50 |
# File 'lib/swagger/shell/api_struct.rb', line 48 def method_key root? ? "api" : @key end |
#root? ⇒ Boolean
40 41 42 |
# File 'lib/swagger/shell/api_struct.rb', line 40 def root? @parent.nil? end |