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.



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

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#parentObject (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

.module_class(method) ⇒ Object



115
116
117
118
# File 'lib/swagger/shell/api_struct.rb', line 115

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



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_ancestorsObject



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_keyObject



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

def api_key
  @key
end

#api_listObject



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_methodsObject



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_urlObject



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_keyObject



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

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

#root?Boolean

Returns:

  • (Boolean)


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

def root?
  @parent.nil?
end

#userObject



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

def user
  Swagger::Shell.user
end