Class: Shiba::Review::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_url, options) ⇒ API

options “token”, “pull_request”



12
13
14
15
16
17
# File 'lib/shiba/review/api.rb', line 12

def initialize(repo_url, options)
  @repo_url = repo_url
  @http = nil
  @token = options.fetch("token")
  @pull_request = options.fetch("pull_request")
end

Instance Attribute Details

#pull_requestObject (readonly)

Returns the value of attribute pull_request.



9
10
11
# File 'lib/shiba/review/api.rb', line 9

def pull_request
  @pull_request
end

#repo_urlObject (readonly)

Returns the value of attribute repo_url.



9
10
11
# File 'lib/shiba/review/api.rb', line 9

def repo_url
  @repo_url
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/shiba/review/api.rb', line 9

def token
  @token
end

Instance Method Details

#comment_on_pull_request(comment) ⇒ Object



31
32
33
34
35
# File 'lib/shiba/review/api.rb', line 31

def comment_on_pull_request(comment)
  req = Net::HTTP::Post.new(uri)
  req.body = JSON.dump(comment)
  request(req)
end

#connectObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/shiba/review/api.rb', line 19

def connect
  Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
    begin
      @http = http
      yield
    ensure
      @http = nil
    end
  end
end

#host_and_pathObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/shiba/review/api.rb', line 57

def host_and_path
   host, path = nil
   # [email protected]:burrito-brothers/shiba.git
   if repo_url.index('@')
     host, path = repo_url.split(':')
     host.sub!('git@', '')
     path.chomp!('.git')
   # https://github.com/burrito-brothers/shiba.git
   else
     uri = URI.parse(repo_url)
     host = uri.host
     path = uri.path.chomp('.git')
     path.reverse!.chomp!("/").reverse!
   end

   return host, path
end

#previous_commentsObject



38
39
40
41
# File 'lib/shiba/review/api.rb', line 38

def previous_comments
  req = Net::HTTP::Get.new(uri)
  request(req)
end

#uriObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/shiba/review/api.rb', line 43

def uri
  return @uri if @uri

  repo_host, repo_path = host_and_path
  url = if repo_host == 'github.com'
    'https://api.github.com'
  else
    "https://#{repo_host}/api/v3"
  end
  url << "/repos/#{repo_path}/pulls/#{pull_request}/comments"

  @uri = URI(url)
end