Class: Github::Nippou::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/github/nippou/settings.rb

Defined Under Namespace

Classes: GettingAccessTokenError, GettingUserError

Instance Method Summary collapse

Instance Method Details

#access_token(verbose: true) ⇒ String

Getting GitHub personal access token

Parameters:

  • verbose (Boolean) (defaults to: true)

    Print error message

Returns:

  • (String)

Raises:

  • (SystemExit)

    cannot get the access token



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/github/nippou/settings.rb', line 37

def access_token(verbose: true)
  @access_token ||=
    case
    when ENV['GITHUB_NIPPOU_ACCESS_TOKEN']
      ENV['GITHUB_NIPPOU_ACCESS_TOKEN']
    when !`git config github-nippou.token`.chomp.empty?
      `git config github-nippou.token`.chomp
    else
      puts "        !!!! GitHub Personal access token required. Please execute the following command. !!!!\n\n            $ github-nippou init\n      MESSAGE\n      raise GettingAccessTokenError\n    end\nend\n" if verbose

#clientObject

Getting Octokit client

return [Octokit::Client]



86
87
88
# File 'lib/github/nippou/settings.rb', line 86

def client
  @client ||= Octokit::Client.new(login: user, access_token: access_token)
end

#create_gistSawyer::Resource

Create gist with config/settings.yml

Returns:

  • (Sawyer::Resource)


93
94
95
96
97
98
99
# File 'lib/github/nippou/settings.rb', line 93

def create_gist
  client.create_gist(
    description: 'github-nippou settings',
    public: true,
    files: { 'settings.yml' => { content: default_settings.to_yaml }}
  )
end

#default_urlString

Getting default settings url

Returns:

  • (String)


116
117
118
# File 'lib/github/nippou/settings.rb', line 116

def default_url
  "https://github.com/masutaka/github-nippou/blob/v#{VERSION}/config/settings.yml"
end

#dictionaryOpenStruct

Getting dictionary settings

Returns:

  • (OpenStruct)


130
131
132
# File 'lib/github/nippou/settings.rb', line 130

def dictionary
  open_struct(data[:dictionary])
end

#formatOpenStruct

Getting format settings

Returns:

  • (OpenStruct)


123
124
125
# File 'lib/github/nippou/settings.rb', line 123

def format
  open_struct(data[:format])
end

#gist_idString

Getting gist id which has settings.yml

Returns:

  • (String)

    gist id



57
58
59
60
61
62
63
64
65
66
# File 'lib/github/nippou/settings.rb', line 57

def gist_id
  @gist_id ||=
    begin
      ENV['GITHUB_NIPPOU_SETTINGS_GIST_ID'] ||
        begin
          git_config = `git config github-nippou.settings-gist-id`.chomp
          git_config.present? ? git_config : nil
        end
    end
end

#thread_numInteger

Getting thread number

Returns:

  • (Integer)


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/github/nippou/settings.rb', line 71

def thread_num
  @thread_num ||=
    case
    when ENV['GITHUB_NIPPOU_THREAD_NUM']
      ENV['GITHUB_NIPPOU_THREAD_NUM']
    when !`git config github-nippou.thread-num`.chomp.empty?
      `git config github-nippou.thread-num`.chomp
    else
      5
    end.to_i
end

#urlString

Getting settings url

Returns:

  • (String)


104
105
106
107
108
109
110
111
# File 'lib/github/nippou/settings.rb', line 104

def url
  @url ||=
    if gist_id
      client.gist(gist_id).html_url
    else
      default_url
    end
end

#user(verbose: true) ⇒ String

Getting GitHub user

Parameters:

  • verbose (Boolean) (defaults to: true)

    Print error message

Returns:

  • (String)

Raises:

  • (SystemExit)

    cannot get the user



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/github/nippou/settings.rb', line 15

def user(verbose: true)
  @user ||=
    case
    when ENV['GITHUB_NIPPOU_USER']
      ENV['GITHUB_NIPPOU_USER']
    when !`git config github-nippou.user`.chomp.empty?
      `git config github-nippou.user`.chomp
    else
      puts "        !!!! GitHub User required. Please execute the following command. !!!!\n\n            $ github-nippou init\n      MESSAGE\n      raise GettingUserError\n    end\nend\n" if verbose