module Gumiho

Constants

VERSION

Public Instance Methods

check_if_json(response_body) click to toggle source
# File lib/gumiho/route_methods.rb, line 23
def check_if_json(response_body)
  if JSON.is_json?(response_body)
    json_data = JSON.parse(response_body)
  end
end
check_status(response, template_param) click to toggle source

CHECK HTTTP REQUEST STATUS !!!!!

# File lib/gumiho/generate_response.rb, line 27
def check_status(response, template_param)
  status = parse_response(response, template_param)
end
delete_method(path) click to toggle source

DELETE request

# File lib/gumiho/route_methods.rb, line 91
def delete_method(path)
  uri = URI(path)
  http = Net::HTTP.new(uri.host, uri.port)

  print_processing_url_method(uri, 'DELETE')

  response = http.delete(path)
  
  check_if_json(response.body)
end
generate_documentation(params, export_dir) click to toggle source
# File lib/generate_doc.rb, line 11
def generate_documentation(params, export_dir)
  #Checks for command line parameters and load them
  
  #Check for config file
  if params[:config]

    if File.file?(params[:config])
      #parses yaml config file and creates PathConfig
      yaml = YAML.load_file(params[:config])
       
      path_conf = PathConfig.new(yaml['generate_strategy'], 
                                yaml['http']['hostname'], 
                                yaml['http']['port'],
                                yaml['http']['path'])
      template_file = yaml['template']
    else

      abort('Wrong file')

    end

  else  
    
    #Creates new PathConfig object with following params
    path_conf = PathConfig.new(params[:protocol], params[:hostname],
                               params[:port], params[:route])

    template_file = params[:template]
  end

  routes_hash = routes_get(path_conf.build_path)
  response = []
  
  routes_hash.each do |route|
    
    if route_filter(route['path'])==false
      path = path_conf.build_path(route['path']) 
     
      params[:methods].each do |method|
        case method
        when 'GET' 
          response << { :method => 'GET', 
                        :url => path, 
                        :response => get_method(path) 
                      } if route['method'].downcase == 'get'
        when 'POST'
          response << { :method => 'POST', 
                        :url => path, 
                        :response => post_method(path) 
                      } if route['method'].downcase == 'post'
        when 'PUT' 
          response << { :method => 'PUT', 
                        :url => path, 
                        :response => put_method(path) 
                      } if route['method'].downcase == 'put'
        when 'DELETE'
          response << { :method => 'DELETE', 
                        :url => path, 
                        :response => delete_method(path) 
                      } if route['method'].downcase == 'delete'
        end
      end
    end

  end
  
  generate_response(response, template_file, export_dir)
  
end
generate_response(response, template_file, export_dir) click to toggle source
# File lib/gumiho/generate_response.rb, line 7
def generate_response(response, template_file, export_dir)
  template = File.open(template_file, 'r').read
  erb = ERB.new(template)
  File.open(File.join(File.dirname(export_dir), File.basename(export_dir)), 'w+') { 
    |file| file.write(erb.result(binding)) 
  }
end
get_cl_params() click to toggle source
# File lib/gumiho/command_line_params.rb, line 5
def get_cl_params
 
  spec = Gem::Specification.find_by_name("gumiho")
  gem_root = spec.gem_dir
  template = gem_root + "/lib/gumiho/template/template.html.erb"
 
  Choice.options do 
    header ''
    header 'Specific options:'

    option :config do
      short '-c'
      long '--config'
      desc 'Add config file'
      default nil
    end

    option :methods do
      short '-m'
      long '--method'
      desc 'Add GET, POST, PUT, DELETE methods'
      default ['GET', 'POST', 'PUT', 'DELETE']
    end

    option :protocol do
      short '-pr'
      long '--protocol'
      desc 'Change current protocol'
      default 'http'
    end
      
    option :hostname do
      short '-h'
      long '--host'
      desc 'Change current hostname'
      default '0.0.0.0'
    end

    option :port do
      short '-p'
      long '--port'
      desc 'Change current port'
      default '4000'
    end      

    option :route do
      short '-r'
      long '--route'
      desc 'Change current route'
      default '/v1/routes'
    end  

    option :template do
      short '-t'
      long '--template'
      desc 'Add report template file'
      default template
    end 

    option :export do
      short '-e'
      long '--export'
      desc 'Export path'
      default './report.html'
    end
  end
      
  Choice.choices
end
get_method(path) click to toggle source

GET request

# File lib/gumiho/route_methods.rb, line 45
def get_method(path)
  uri = URI(path)

  uri.query = URI.encode_www_form({ :doc => true })
  http = Net::HTTP.new(uri.host, uri.port)  

  print_processing_url_method(uri, 'GET')

  response = http.get(uri)

  #Checks if JSON response is right
  check_if_json(response.body)
 
end
map_array(response, template_param) { |one_param| ... } click to toggle source
# File lib/gumiho/generate_response.rb, line 32
def map_array(response, template_param)
  result = Array.new
  data = parse_response(response, template_param)

  #Checks for wrong or empty array
  if data.class == Array and data.empty? == false
    result.push(data)
  end
  
  result.each do |param|
   # ap param
    param.each do |one_param|
      yield one_param
    end
  end

end
map_string(response, template_param) click to toggle source
# File lib/gumiho/generate_response.rb, line 50
def map_string(response, template_param)
  data = parse_response(response, template_param)
end
parse_response(response, template_param) click to toggle source

Methods used in the template file

# File lib/gumiho/generate_response.rb, line 16
def parse_response(response, template_param)
  params = template_param.split('.')
  result = response
  params.each do |param|
    result = result[param] if result.class == Hash
  end

  result
end
post_method(path) click to toggle source

POST request

# File lib/gumiho/route_methods.rb, line 61
def post_method(path)
  
  uri = URI(path)

  request = Net::HTTP::Post.new(uri.path)

  request.set_form_data({:doc=>true})

  response = Net::HTTP.start(uri.hostname, uri.port) do |http|
    http.request(request)
  end
  print_processing_url_method(uri, 'POST')
  check_if_json(response.body)
end
print_processing_url_method(url, method) click to toggle source
put_method(path) click to toggle source

PUT request

# File lib/gumiho/route_methods.rb, line 77
def put_method(path)
  uri = URI(path)
  http = Net::HTTP.new(uri.host, uri.port)

  print_processing_url_method(uri, 'PUT')

  request = Net::HTTP::Put.new(path)
  request.set_form_data({:doc=>true })
  response = http.request(request)
  
  check_if_json(response.body)
end
route_filter(path) click to toggle source

Example filter for routes containing given substring used in generate_doc

# File lib/gumiho/route_methods.rb, line 103
def route_filter(path)
  path.include? 'time'
end
routes_get(path) click to toggle source

Without doc true

# File lib/gumiho/route_methods.rb, line 30
def routes_get(path)
  uri = URI(path)

 # uri.query = URI.encode_www_form()
  http = Net::HTTP.new(uri.host, uri.port)  

  print_processing_url_method(uri, 'GET')

  response = http.get(uri)

  #Checks if JSON response is right
  check_if_json(response.body)
end