class Github::Nippou::Settings
Public Instance Methods
Getting GitHub personal access token
@param verbose [Boolean] Print error message @return [String] @raise [SystemExit] cannot get the access token
# 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 <<~MESSAGE if verbose !!!! GitHub Personal access token required. Please execute the following command. !!!! $ github-nippou init MESSAGE raise GettingAccessTokenError end end
Getting Octokit client
return [Octokit::Client]
# File lib/github/nippou/settings.rb, line 86 def client @client ||= Octokit::Client.new(login: user, access_token: access_token) end
Create gist with config/settings.yml
@return [Sawyer::Resource]
# 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
Getting default settings url
@return [String]
# File lib/github/nippou/settings.rb, line 116 def default_url "https://github.com/masutaka/github-nippou/blob/v#{VERSION}/config/settings.yml" end
Getting dictionary settings
@return [OpenStruct]
# File lib/github/nippou/settings.rb, line 130 def dictionary open_struct(data[:dictionary]) end
Getting format settings
@return [OpenStruct]
# File lib/github/nippou/settings.rb, line 123 def format open_struct(data[:format]) end
Getting gist id which has settings.yml
@return [String] gist id
# 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
Getting thread number
@return [Integer]
# 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
Getting settings url
@return [String]
# 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
Getting GitHub user
@param verbose [Boolean] Print error message @return [String] @raise [SystemExit] cannot get the user
# 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 <<~MESSAGE if verbose !!!! GitHub User required. Please execute the following command. !!!! $ github-nippou init MESSAGE raise GettingUserError end end
Private Instance Methods
Getting settings data as Hash
return [Hash]
# File lib/github/nippou/settings.rb, line 149 def data @data ||= begin if gist_id.present? gist = client.gist(gist_id) yaml_data = gist[:files][:'settings.yml'][:content] YAML.load(yaml_data).deep_symbolize_keys else default_settings.deep_symbolize_keys end rescue Psych::SyntaxError puts <<~MESSAGE ** YAML syntax error. #{$!.message} #{yaml_data} MESSAGE raise $! end end
Getting default settings.yml as Hash
return [Hash]
# File lib/github/nippou/settings.rb, line 139 def default_settings @default_settings ||= YAML.load_file( File.expand_path('../../../config/settings.yml', __dir__) ) end
Cast to OpenStruct
@param hash [Hash] @return [OpenStruct]
# File lib/github/nippou/settings.rb, line 174 def open_struct(hash) JSON.parse(hash.to_json, object_class: OpenStruct) end