Class: Swagger::Shell::ApiStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger/shell/api_struct.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, parent = nil) ⇒ ApiStruct

Returns a new instance of ApiStruct.



30
31
32
33
34
# File 'lib/swagger/shell/api_struct.rb', line 30

def initialize(key, parent = nil)
  @key = key
  @parent = parent
  @children = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



28
29
30
# File 'lib/swagger/shell/api_struct.rb', line 28

def children
  @children
end

#parentObject (readonly)

Returns the value of attribute parent.



28
29
30
# File 'lib/swagger/shell/api_struct.rb', line 28

def parent
  @parent
end

Class Method Details

.module_class(method) ⇒ Object



109
110
111
112
# File 'lib/swagger/shell/api_struct.rb', line 109

def self.module_class(method)
  camelize_name = method.to_s.dup.tap {|s| s[0] = s[0].upcase }
  Swagger::Shell.const_get("Api#{camelize_name}")
end

Instance Method Details

#add_api(path_keys, method, api_info: nil) ⇒ Object



48
49
50
51
52
# File 'lib/swagger/shell/api_struct.rb', line 48

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) if api_struct
  end
end

#add_api_module(method) ⇒ Object



83
84
85
# File 'lib/swagger/shell/api_struct.rb', line 83

def add_api_module(method)
  extend self.class.module_class(method)
end

#api_ancestorsObject



75
76
77
78
79
80
81
# File 'lib/swagger/shell/api_struct.rb', line 75

def api_ancestors
  loop.inject([self]) do |parents|
    break parents if parents.last.parent.nil?
    parents << parents.last.parent
    parents
  end.reverse
end

#api_keyObject



40
41
42
# File 'lib/swagger/shell/api_struct.rb', line 40

def api_key
  @key
end

#api_listObject



54
55
56
57
58
59
60
61
62
# File 'lib/swagger/shell/api_struct.rb', line 54

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 |api_method|
      hash[api_method] = ""
    end
  end
end

#api_methodsObject



69
70
71
72
73
# File 'lib/swagger/shell/api_struct.rb', line 69

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)
  end.compact
end

#api_urlObject



64
65
66
67
# File 'lib/swagger/shell/api_struct.rb', line 64

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/swagger/shell/api_struct.rb', line 87

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 "      def self.\#{path_key}\n        @\#{path_key}\n      end\n    RUBY\n    @children << path_key.to_sym\n  end\n\n  instance_variable_get(\"@\#{path_key}\")\nend\n"

#method_keyObject



44
45
46
# File 'lib/swagger/shell/api_struct.rb', line 44

def method_key
  root? ? "api" : @key
end

#root?Boolean

Returns:



36
37
38
# File 'lib/swagger/shell/api_struct.rb', line 36

def root?
  @parent.nil?
end

#userObject



105
106
107
# File 'lib/swagger/shell/api_struct.rb', line 105

def user
  Swagger::Shell.user
end