Class: Apiary::Command::Styleguide
- Inherits:
-
Object
- Object
- Apiary::Command::Styleguide
show all
- Includes:
- Helpers
- Defined in:
- lib/apiary/command/styleguide.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Helpers
#api_description_source, #api_description_source_path, #convert_from_json
Constructor Details
Returns a new instance of Styleguide.
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/apiary/command/styleguide.rb', line 16
def initialize(opts)
@options = OpenStruct.new(opts)
@options.fetch ||= false
@options.add ||= '.'
@options.functions ||= '.'
@options.rules ||= '.'
@options.api_key ||= ENV['APIARY_API_KEY']
@options.proxy ||= ENV['http_proxy']
@options.api_host ||= 'api.apiary.io'
@options.vk_url ||= 'https://voight-kampff.apiary-services.com'
@options. ||= {
content_type: :json,
accept: :json,
user_agent: Apiary.user_agent
}
@options.failedOnly = !@options.full_report
@options.json
end
|
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
14
15
16
|
# File 'lib/apiary/command/styleguide.rb', line 14
def options
@options
end
|
Instance Method Details
#call_apiary(path, data, headers, method) ⇒ Object
184
185
186
|
# File 'lib/apiary/command/styleguide.rb', line 184
def call_apiary(path, data, , method)
call_resource("https://#{@options.api_host}/#{path}", data, , method)
end
|
#call_resource(url, data, headers, method) ⇒ Object
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/apiary/command/styleguide.rb', line 188
def call_resource(url, data, , method)
RestClient.proxy = @options.proxy
method = :post unless method
begin
response = RestClient::Request.execute(method: method, url: url, payload: data, headers: )
rescue RestClient::Exception => e
begin
err = JSON.parse e.response
rescue JSON::ParserError
err = {}
end
message = 'Error: Apiary service responded with:'
if err.key? 'message'
abort "#{message} #{e.http_code} #{err['message']}"
else
abort "#{message} #{e.message}"
end
end
response
end
|
#check_api_key ⇒ Object
220
221
222
223
|
# File 'lib/apiary/command/styleguide.rb', line 220
def check_api_key
return if @options.api_key && @options.api_key != ''
abort 'Error: API key must be provided through environment variable APIARY_API_KEY. \Please go to https://login.apiary.io/tokens to obtain it.'
end
|
#default_functions_file_name ⇒ Object
180
181
182
|
# File 'lib/apiary/command/styleguide.rb', line 180
def default_functions_file_name
'functions.js'
end
|
#default_rules_file_name ⇒ Object
176
177
178
|
# File 'lib/apiary/command/styleguide.rb', line 176
def default_rules_file_name
'rules.json'
end
|
#execute ⇒ Object
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/apiary/command/styleguide.rb', line 35
def execute
check_api_key
if @options.fetch
fetch
elsif @options.push
push
else
validate
end
end
|
#fetch ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/apiary/command/styleguide.rb', line 70
def fetch
begin
assertions = fetch_from_apiary
assertions = JSON.parse(assertions)
rescue => e
abort "Error: Can not fetch rules and functions: #{e}"
end
begin
File.write("./#{default_functions_file_name}", assertions['functions']['functions'])
File.write("./#{default_rules_file_name}", JSON.pretty_generate(assertions['rules']['rules']))
puts "`./#{default_functions_file_name}` and `./#{default_rules_file_name}` has beed succesfully created"
rescue => e
abort "Error: Can not write into the rules/functions file: #{e}"
end
end
|
#fetch_from_apiary ⇒ Object
234
235
236
237
238
239
|
# File 'lib/apiary/command/styleguide.rb', line 234
def fetch_from_apiary
path = 'styleguide-cli/get-assertions/'
= @options..clone
[:authentication] = "Token #{@options.api_key}"
call_apiary(path, {}, , :get)
end
|
#get_file_content(path) ⇒ Object
267
268
269
270
271
|
# File 'lib/apiary/command/styleguide.rb', line 267
def get_file_content(path)
source = nil
File.open(path, 'r:bom|utf-8') { |file| source = file.read }
source
end
|
#get_functions(path) ⇒ Object
245
246
247
|
# File 'lib/apiary/command/styleguide.rb', line 245
def get_functions(path)
get_file_content(get_path(path, 'functions'))
end
|
#get_path(path, type) ⇒ Object
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# File 'lib/apiary/command/styleguide.rb', line 249
def get_path(path, type)
raise "`#{path}` not found" unless File.exist? path
return path if File.file? path
file = case type
when 'rules'
default_rules_file_name
else
default_functions_file_name
end
path = File.join(path, file)
return path if File.file? path
raise "`#{path}` not found"
end
|
#get_rules(path) ⇒ Object
241
242
243
|
# File 'lib/apiary/command/styleguide.rb', line 241
def get_rules(path)
JSON.parse get_file_content(get_path(path, 'rules'))
end
|
#human_output(json_response) ⇒ Object
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
|
# File 'lib/apiary/command/styleguide.rb', line 134
def human_output(json_response)
if json_response.empty?
puts 'All tests has passed'
exit 0
end
puts ''
at_least_one_failed = false
json_response.each do |response|
puts " #{(response['intent'] || response['ruleName'] || response['functionName'])}"
(response['results'] || []).each do |result|
print_output_text result
at_least_one_failed = true if result['result'] != true
end
puts ''
end
if at_least_one_failed
exit 1
else
exit 0
end
end
|
#json_output(json_response) ⇒ Object
130
131
132
|
# File 'lib/apiary/command/styleguide.rb', line 130
def json_output(json_response)
puts JSON.pretty_generate json_response
end
|
#jwt ⇒ Object
213
214
215
216
217
218
|
# File 'lib/apiary/command/styleguide.rb', line 213
def jwt
path = 'styleguide-cli/get-token/'
= @options..clone
[:authentication] = "Token #{@options.api_key}"
call_apiary(path, {}, , :get)
end
|
#load(add: true, functions: true, rules: true) ⇒ Object
225
226
227
228
229
230
231
232
|
# File 'lib/apiary/command/styleguide.rb', line 225
def load(add: true, functions: true, rules: true)
if add
@add_path = api_description_source_path(@options.add)
@add = api_description_source(@add_path)
end
@functions = get_functions(@options.functions) if functions
@rules = get_rules(@options.rules) if rules
end
|
#output(raw_response) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/apiary/command/styleguide.rb', line 162
def output(raw_response)
begin
json_response = JSON.parse(raw_response)
rescue
abort "Error: Can not parse result: #{raw_response}"
end
if @options.json
json_output json_response
else
human_output json_response
end
end
|
#print_output_text(result) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/apiary/command/styleguide.rb', line 116
def print_output_text(result)
lines = if result['sourcemapLines']['start'] == result['sourcemapLines']['end']
"on line #{result['sourcemapLines']['start']}"
else
"on lines #{result['sourcemapLines']['start']} - #{result['sourcemapLines']['end']}"
end
if result['result'] == true
puts " [\u2713] PASSED: #{(result['path'] || '').gsub('-', ' #')} #{lines}"
else
puts " [\u274C] FAILED: #{(result['path'] || '').gsub('-', ' #')} #{lines} - `#{result['result']}`"
end
end
|
#push ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/apiary/command/styleguide.rb', line 46
def push
begin
load(add: false)
rescue StandardError => e
abort "Error: #{e.message}"
end
path = 'styleguide-cli/set-assertions/'
= @options..clone
[:authentication] = "Token #{@options.api_key}"
data = {
functions: @functions,
rules: @rules
}.to_json
begin
call_apiary(path, data, , :post)
rescue => e
puts e
abort "Error: Can not write into the rules/functions file: #{e}"
end
end
|
#validate ⇒ Object
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
|
# File 'lib/apiary/command/styleguide.rb', line 87
def validate
token = jwt
begin
token = JSON.parse(token)['jwt']
rescue JSON::ParserError => e
abort "Can not authenticate: #{e}"
end
begin
load
rescue StandardError => e
abort "Error: #{e.message}"
end
data = {
functions: @functions,
rules: @rules,
add: @add,
failedOnly: @options.failedOnly
}.to_json
= @options..clone
[:Authorization] = "Bearer #{token}"
['Accept-Encoding'] = 'identity'
output call_resource(@options.vk_url, data, , :post)
end
|