Class: Lintron::API

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

Overview

Makes requests to the lintron local lint API

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ API

Returns a new instance of API.



8
9
10
# File 'lib/lintron/api.rb', line 8

def initialize(base_url)
  @base_url = base_url
end

Instance Method Details

#post_lint_request(pr) ⇒ Object



26
27
28
# File 'lib/lintron/api.rb', line 26

def post_lint_request(pr)
  HTTParty.post(URI.join(@base_url, 'local_lints'), request_params(pr))
end

#request_params(pr) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/lintron/api.rb', line 30

def request_params(pr)
  {
    body: pr.to_json,
    headers: {
      'Content-Type' => 'application/json',
      'Accept' => 'application/json',
    },
  }
end

#violations(pr) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lintron/api.rb', line 12

def violations(pr)
  response = post_lint_request(pr)
  violations =
    JSON
    .parse(response.body)
    .map { |json| Lintron::ViolationLine.new(json) }

  violations.sort_by(&:file_and_line)
rescue JSON::ParserError
  puts 'Error occurred while parsing response from Lintron'.colorize(:red)
  puts 'Raw response body: '
  puts response.body
end