Class: Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/buttless.rb

Instance Method Summary collapse

Instance Method Details

#example_curl(method, ip = "http://0.0.0.0:3000", no = 3) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/buttless.rb', line 71

def example_curl(method, ip="http://0.0.0.0:3000", no=3)
  unparsed_path = method["path"]
  puts unparsed_path
  path = example_path(method, ip)
  i = 0
  params = []
  method["parameters"].select{|p|p["type"]=="POST" || (p["type"].nil? && method["method"]=="POST")}.each do |p|
    is_in_path = unparsed_path.include?(":#{p['name']}")
    puts ":#{p['name']} in path #{unparsed_path}? #{is_in_path}"
    break unless (i < no or p["optional"] == false)
    i=i+1 unless p["optional"] == false
    if ! is_in_path
      params << "#{p["name"]}=#{p["example"]}"
    else
      #path.gsub!(":#{p['name']}", p["example"].to_s)
    end
  end if method["parameters"]
  data = nil
  unless params.empty?
    data = "--data \"#{params.join(" \\ \n&")} \" "
  end
  "curl -v #{data.nil? ? '' : "\\\n #{data}"}\\\n#{path}".chomp('?')
end

#example_path(method, ip = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/buttless.rb', line 51

def example_path(method, ip=nil)
  path = method["path"]
  params = []
  method["parameters"].each do |p|
    is_in_path = path.include?(":#{p['name']}")
    next if not is_in_path and (p["optional"] or p["type"]=="POST" or (p["type"].nil? and method["method"] == "POST"))
    if path.include?(":#{p['name']}")
      path = path.gsub(":#{p['name']}", p["example"].to_s)
      next
    else
      params << "#{p["name"]}=#{p["example"]}"
    end

  end unless method["parameters"].nil?

  api_call = "#{path}?#{params.join('&')}"
  #puts "**** WARNING **** \n Method length is #{api_call.length}, which is greater than 19 so won't work on some embedded devices: #{api_call}" if "#{path}?#{params.join('&')}".length > 19
  "#{ip}#{api_call}"
end