Class: ChangesSince::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/changes_since/api_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApiClient

Returns a new instance of ApiClient.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/changes_since/api_client.rb', line 9

def initialize
  puts "Please enter your Github credentials:"
  print "Username:"
  username = gets.chomp
  print "Password:"
  password = STDIN.noecho(&:gets).chomp
  puts

  @client = Octokit::Client.new(
    :login => username,
    :password => password
  )

  begin
    fetch_authorization
  rescue Octokit::OneTimePasswordRequired => e
    puts "Enter two factor auth token:"
    otp_token = gets.chomp
    fetch_authorization(otp_token)
  end

  @client = Octokit::Client.new(:access_token => access_token)
  git = Git.open(Dir.pwd)
  @repo = git.remote.url.split(/[:.]/)[-2]
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



7
8
9
# File 'lib/changes_since/api_client.rb', line 7

def access_token
  @access_token
end

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/changes_since/api_client.rb', line 7

def client
  @client
end

#repoObject (readonly)

Returns the value of attribute repo.



7
8
9
# File 'lib/changes_since/api_client.rb', line 7

def repo
  @repo
end

Instance Method Details

#fetch_authorization(otp_token = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/changes_since/api_client.rb', line 35

def fetch_authorization(otp_token=nil)
  options = { :scopes => ["repo"], :note => "Changes Since" }

  headers = {}
  if otp_token
    headers = { "X-GitHub-OTP" => otp_token }
    options.merge!(:headers => headers)
  end

  begin
    @access_token = client.create_authorization(options)
  rescue Octokit::UnprocessableEntity => e
    authorizations = client.authorizations(:headers => headers)
    @access_token = authorizations.find { |a| a.note == "Changes Since" }.token
  end
end

#pull_request_risks(number) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/changes_since/api_client.rb', line 52

def pull_request_risks(number)
  pr = client.pull_request(repo, number)
  body = pr.body
  if pr.body.include?("Risks")
    body = body[pr.body.index("Risks") + 5, body.size]
  elsif pr.body.include?("Risk")
    body = body[pr.body.index("Risk") + 4, body.size]
  else
    return " "
  end
  body = body.strip
  if index = body.index(/\r\n\r\n/)
    body = body[0, index]
  end
  body
end