lapiz 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/lib/lapiz/version.rb +1 -1
  4. data/lib/lapiz.rb +148 -2
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fbcfb12b4eb5e6c27e44e6493c418445b9c54d17
4
- data.tar.gz: 248f79b173cefd7fecb53cf29bb43e3ceb0bac8d
3
+ metadata.gz: e7962443bbda79bd48219d63b261ff4bead15429
4
+ data.tar.gz: 33d0a913c948c92b8e693abf592f7d38f052f769
5
5
  SHA512:
6
- metadata.gz: de52e209ce8ff8262d30c84d8ee03225572c4e641af7400b72facd945237cfb851e97ba7785ad2f482c9fa06fd68ca03803695433bf690619ddb732b297b1bb9
7
- data.tar.gz: 729c1715f77268cbe8d62225b4fb10b29270eaafe0397d96104c78d108f78065c2810a4d7e540ab1da7acdc6df2086f9a8b9cef98604e3d23bc90c401c365e20
6
+ metadata.gz: 745e5b576c5f170f9702dfc62ec136fa9fbae143b096d9ba67b882eba1f9df4065c94329b204eba9468134bdac314fdabc4236f6617a5020bb87437defe3506e
7
+ data.tar.gz: 719f14a38c5eab259552aeffb19727e223c1e63d8b2d2dcd67ed758c54fd012654535b5cd2b485ab339c2944834ed339ee5938a78ed63b4ae120042338171ee7
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /*.gem
data/lib/lapiz/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Lapiz
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/lapiz.rb CHANGED
@@ -1,10 +1,156 @@
1
1
  require "lapiz/version"
2
+ require "fileutils"
3
+ require "cgi"
4
+
5
+ class String
6
+ def underscore
7
+ self.gsub(/::/, '/').
8
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
9
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
10
+ tr("-", "_").
11
+ downcase
12
+ end
13
+ end
14
+
15
+ class Hash
16
+ def flatten
17
+ new_hash = self.dup
18
+
19
+ while new_hash.values.map(&:class).include?(Hash)
20
+ updated_hash = {}
21
+ new_hash.each_pair do |k,v|
22
+ if v.is_a?(Hash)
23
+ v.each_pair do |ik, iv|
24
+ updated_hash["#{k},#{ik}"] = iv
25
+ end
26
+ else
27
+ updated_hash[k.to_s] = v
28
+ end
29
+ end
30
+ new_hash = updated_hash
31
+ end
32
+
33
+ new_hash = new_hash.to_a.map{ |e|
34
+ if e[0].include?(",")
35
+ main = e[0].split(",").first
36
+ rest = e[0].split(",")[1..-1]
37
+ rest_string = rest.map{ |r| "[#{r}]" }.join("")
38
+ ["#{main}#{rest_string}", e[1]]
39
+ else
40
+ e
41
+ end
42
+ }.to_h
43
+
44
+ new_hash
45
+ end
46
+ end
47
+
48
+ module Lapiz
49
+ def group(name, &block)
50
+ describe(name) do
51
+ config = YAML.load(IO.read("config.yml"))
52
+ metadata[:base_uri] = config["server"]["base_uri"]
53
+ metadata[:group_name] = name
54
+
55
+ FileUtils.mkdir_p("api_docs")
56
+ File.open("api_docs/#{name.gsub(/[^a-zA-Z_]+/,'_').underscore}.txt", "w+") do |fp|
57
+ fp.puts "# Group #{name}"
58
+ fp.puts
59
+ end
60
+
61
+ instance_eval &block
62
+ end
63
+ end
64
+
65
+ def get(path, params = {}, &block)
66
+ http_call(:get, path, params, &block)
67
+ end
68
+
69
+ def post(path, params, &block)
70
+ http_call(:post, path, params, &block)
71
+ end
72
+
73
+ def http_call(method, path, params, &block)
74
+ it "tests action '#{path}'", self do |group|
75
+ expect {
76
+ @response = HTTParty.send(method, group.metadata[:base_uri] + path, params)
77
+ }.to_not raise_error
78
+ instance_eval "def response;@response;end"
79
+ instance_eval &block
80
+
81
+ request_type = nil
82
+ unless method == :head || method == :get
83
+ # Attempt to find request type
84
+ if @response.request.options[:headers]
85
+ request_type = @response.request.options[:headers]["Content-Type"]
86
+ end
87
+
88
+ if request_type.nil? # it was not set explicitly
89
+ # if body is present, assume they meant x-www-form-urlencoded
90
+ request_type = "x-www-form-urlencoded"
91
+ end
92
+ end
93
+
94
+ group_name = group.metadata[:group_name]
95
+ File.open("api_docs/#{group_name.gsub(/[^a-zA-Z_]+/,'_').underscore}.txt", "a+") do |fp|
96
+ fp.puts "## #{method.to_s.upcase} #{path}"
97
+ fp.puts
98
+
99
+ if request_type
100
+ if request_type == "application/json"
101
+ fp.puts "+ Request (#{request_type})"
102
+ elsif request_type == "x-www-form-urlencoded"
103
+ fp.puts "+ Parameters"
104
+
105
+ if @response.request.options[:body]
106
+ flattened_params = @response.request.options[:body] ? @response.request.options[:body].flatten : {}
107
+ flattened_params.each_pair do |k,v|
108
+ fp.puts " + #{CGI.escape(k)}: \"#{v.to_s}\" (#{v.class.name})"
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ if @response.body
115
+ fp.puts
116
+ fp.puts "+ Response #{@response.code} (#{@response.content_type})"
117
+ fp.puts
118
+
119
+ hash = JSON.parse(@response.body)
120
+
121
+ fp.puts JSON.pretty_generate(hash).split("\n").map{ |line| " #{line}" }.join("\n")
122
+
123
+ fp.puts
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
2
129
 
3
130
  RSpec.configure do |config|
4
- configure.before(:suite) do
5
- # Create or clear api-docs folder
131
+ config.after(:suite) do
132
+ config = YAML.load(IO.read("config.yml"))
133
+ base_uri = config["server"]["base_uri"]
134
+
135
+ File.open("api_docs/api.md", "w+") do |fp|
136
+ fp.puts "FORMAT: 1A"
137
+ fp.puts "HOST: #{base_uri}"
138
+ fp.puts
139
+ fp.puts "# #{config["meta"]["title"]}"
140
+ fp.puts
141
+ fp.puts config["meta"]["description"]
142
+ fp.puts
143
+
144
+ Dir["api_docs/*.txt"].each do |f|
145
+ IO.readlines(f).map(&:chomp).each do |line|
146
+ fp.puts line
147
+ end
148
+ end
149
+ end
6
150
  end
7
151
 
8
152
  config.after(:example) do |example|
9
153
  end
10
154
  end
155
+
156
+ include Lapiz
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lapiz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ajith Hussain
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-01 00:00:00.000000000 Z
11
+ date: 2015-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler