class Github::Nippou::Settings

Public Instance Methods

access_token(verbose: true) click to toggle source

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
client() click to toggle source

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() click to toggle source

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
default_url() click to toggle source

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
dictionary() click to toggle source

Getting dictionary settings

@return [OpenStruct]

# File lib/github/nippou/settings.rb, line 130
def dictionary
  open_struct(data[:dictionary])
end
format() click to toggle source

Getting format settings

@return [OpenStruct]

# File lib/github/nippou/settings.rb, line 123
def format
  open_struct(data[:format])
end
gist_id() click to toggle source

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
thread_num() click to toggle source

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
url() click to toggle source

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
user(verbose: true) click to toggle source

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

data() click to toggle source

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
default_settings() click to toggle source

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
open_struct(hash) click to toggle source

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