class Swagger::Shell::ApiStruct

Attributes

children[R]
parent[R]

Public Class Methods

module_class(method) click to toggle source
# 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
new(key, parent = nil) click to toggle source
# File lib/swagger/shell/api_struct.rb, line 34
def initialize(key, parent = nil)
  @key = key
  @parent = parent
  @children = []
end

Public Instance Methods

add_api(path_keys, method, api_info = nil) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/swagger/shell/api_struct.rb, line 44
def api_key
  @key
end
api_list() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/swagger/shell/api_struct.rb, line 48
def method_key
  root? ? "api" : @key
end
root?() click to toggle source
# File lib/swagger/shell/api_struct.rb, line 40
def root?
  @parent.nil?
end
user() click to toggle source
# File lib/swagger/shell/api_struct.rb, line 111
def user
  Swagger::Shell.user
end

Private Instance Methods

_delete(url, message = {}) click to toggle source
# File lib/swagger/shell/api_struct.rb, line 157
def _delete(url, message = {})
  _request(:delete, url, message)
end
_get(url, message = {}) click to toggle source
# File lib/swagger/shell/api_struct.rb, line 145
def _get(url, message = {})
  _request(:get, url, message)
end
_post(url, message = {}) click to toggle source
# File lib/swagger/shell/api_struct.rb, line 149
def _post(url, message = {})
  _request(:post, url, message)
end
_put(url, message = {}) click to toggle source
# File lib/swagger/shell/api_struct.rb, line 153
def _put(url, message = {})
  _request(:put, url, message)
end
_request(method, url, params = {}) click to toggle source
# File lib/swagger/shell/api_struct.rb, line 161
def _request(method, url, params = {})
  res = begin
    client = Faraday.new(:url => Swagger::Shell.config_env.api_url)
    client.public_send(method) do |req|
      req.url url
      hook_request_headers.each do |k, v|
        req.headers[k] = v.to_s
      end
      req.body = hook_request_body(params).to_json
    end
  rescue => e
    raise e
  end

  hook_response_header(res.env.response_headers)
  hook_response_body(res.body)
end
find_or_create_api_struct(path_keys) click to toggle source
# File lib/swagger/shell/api_struct.rb, line 137
def find_or_create_api_struct(path_keys)
  path_keys.inject(self) do |api_struct, path_key|
    break nil if api_struct.nil?

    api_struct.child(path_key)
  end
end
hook_request_body(body) click to toggle source
# File lib/swagger/shell/api_struct.rb, line 122
def hook_request_body(body)
  body
end
hook_request_headers() click to toggle source
# File lib/swagger/shell/api_struct.rb, line 126
def hook_request_headers
  Swagger::Shell.config_env.request_headers.to_h
end
hook_response_body(body) click to toggle source
# File lib/swagger/shell/api_struct.rb, line 130
def hook_response_body(body)
  # TODO: need to implement expect for json
  JSON.parse body, symbolize_names: true
end
hook_response_header(header) click to toggle source
# File lib/swagger/shell/api_struct.rb, line 135
def hook_response_header(header); end