Module: Lapiz

Defined in:
lib/lapiz.rb,
lib/lapiz/version.rb

Constant Summary collapse

VERSION =
"1.1.0"

Instance Method Summary collapse

Instance Method Details

#delete(path, params, &block) ⇒ Object



81
82
83
# File 'lib/lapiz.rb', line 81

def delete(path, params, &block)
  http_call(:delete, path, params, &block)
end

#get(path, params = {}, &block) ⇒ Object



65
66
67
# File 'lib/lapiz.rb', line 65

def get(path, params = {}, &block)
  http_call(:get, path, params, &block)
end

#group(name, &block) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lapiz.rb', line 49

def group(name, &block)
  describe(name) do
    config = YAML.load(IO.read("config.yml"))
    [:base_uri] = config["server"]["base_uri"]
    [:group_name] = name

    FileUtils.mkdir_p("api_docs")
    File.open("api_docs/#{name.gsub(/[^a-zA-Z_]+/,'_').underscore}.txt", "w+") do |fp|
      fp.puts "# Group #{name}"
      fp.puts
    end

    instance_eval &block
  end
end

#http_call(method, path_pattern, params, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/lapiz.rb', line 85

def http_call(method, path_pattern, params, &block)
  path = path_pattern.gsub(/{([^{}]*)}/) { $1.split("=").last }       # path_pattern contains {field=default_value}. gsub it with default value 
  pattern = path_pattern.gsub(/{([^{}]*)}/) { "{#{$1.split("=").first}}" }  # same as above, but gsub it with field

  if block.nil?
    config = YAML.load(IO.read("config.yml"))
    base_uri = config["server"]["base_uri"]
    return HTTParty.send(method, base_uri + path, params)
  end

  it "tests action '#{pattern}'", self  do |group|
    expect {
      @response = HTTParty.send(method, group.[:base_uri] + path, params)
    }.to_not raise_error
    instance_eval "def response;@response;end"
    instance_eval &block

    request_type = nil
    unless method == :head || method == :get
      # Attempt to find request type
      if @response.request.options[:headers]
        request_type = @response.request.options[:headers]["Content-Type"]
      end

      if  request_type.nil? # it was not set explicitly
        # if body is present, assume they meant x-www-form-urlencoded
        request_type = "x-www-form-urlencoded"
      end
    end

    group_name = group.[:group_name]
    File.open("api_docs/#{group_name.gsub(/[^a-zA-Z_]+/,'_').underscore}.txt", "a+") do |fp|
      fp.puts "## #{method.to_s.upcase} #{pattern}"
      fp.puts

      if @response.request.options[:description]
        fp.puts @response.request.options[:description].lstrip
      end 

      if request_type || (path != pattern) # path != pattern when there is a url pattern # TODO: Use better checks
        if request_type == "application/json"
          fp.puts "+ Request (#{request_type})"
        elsif request_type == "x-www-form-urlencoded" || (path != pattern)
          fp.puts "+ Parameters"

          flattened_params = {}
          if @response.request.options[:body]
            flattened_params = @response.request.options[:body] ? @response.request.options[:body].flatten : {}
            flattened_params.each_pair do |k,v| 
              description = nil
              if @response.request.options[:parameter_descriptions]
                description = @response.request.options[:parameter_descriptions][k.to_s] || @response.request.options[:parameter_descriptions][k.to_sym]
              end
              fp.puts "    + #{CGI.escape(k)}: (#{v.class.name})#{description ? " - #{description}" : ""}"
            end
          end

          # This converts param_desc hash to an array ([[k1,v1],[k2,v2]]), removes any which are already in the body (and hence documented), and then convers back to hash
          @response.request.options[:parameter_descriptions].to_a.map{|e| [e[0].to_s, e[1]]}.reject{|e| flattened_params.keys.include?(e[0])}.to_h.each_pair do |k,v|
            fp.puts "    + #{CGI.escape(k)}: (String) - #{v}"
          end
        end
      end

      if @response.request.options[:headers]
        fp.puts
        fp.puts "+ Request"
        fp.puts
        fp.puts "    + Headers"
        fp.puts
        @response.request.options[:headers].each_pair  do |k,v|
          fp.puts "            #{k}: #{v}"
        end
        fp.puts
      end

      if @response.body && (@response.code / 100 == 2)
        fp.puts
        fp.puts "+ Response #{@response.code} (#{@response.content_type})"
        fp.puts 

        hash = JSON.parse(@response.body)

        fp.puts JSON.pretty_generate(hash).split("\n").map{ |line| "        #{line}" }.join("\n")

        fp.puts
      end
    end
  end
end

#patch(path, params, &block) ⇒ Object



73
74
75
# File 'lib/lapiz.rb', line 73

def patch(path, params, &block)
  http_call(:patch, path, params, &block)
end

#post(path, params, &block) ⇒ Object



69
70
71
# File 'lib/lapiz.rb', line 69

def post(path, params, &block)
  http_call(:post, path, params, &block)
end

#put(path, params, &block) ⇒ Object



77
78
79
# File 'lib/lapiz.rb', line 77

def put(path, params, &block)
  http_call(:put, path, params, &block)
end