From 7c29f3ddd27d1f09a2d721c342ec65b831b78b42 Mon Sep 17 00:00:00 2001 From: Petr Korolev Date: Fri, 27 Mar 2015 12:02:39 +0200 Subject: [PATCH] move @github to fetcher Conflicts: lib/github_changelog_generator.rb --- lib/github_changelog_generator.rb | 14 ++------- lib/github_changelog_generator/fetcher.rb | 35 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/lib/github_changelog_generator.rb b/lib/github_changelog_generator.rb index ecab437..d54cd4f 100755 --- a/lib/github_changelog_generator.rb +++ b/lib/github_changelog_generator.rb @@ -9,6 +9,7 @@ require_relative 'github_changelog_generator/parser' require_relative 'github_changelog_generator/generator' require_relative 'github_changelog_generator/version' require_relative 'github_changelog_generator/reader' +require_relative 'github_changelog_generator/fetcher' module GitHubChangelogGenerator class ChangelogGenerator @@ -21,18 +22,7 @@ module GitHubChangelogGenerator def initialize @options = Parser.parse_options - fetch_github_token - - github_options = { per_page: PER_PAGE_NUMBER } - github_options[:oauth_token] = @github_token unless @github_token.nil? - github_options[:endpoint] = options[:github_endpoint] unless options[:github_endpoint].nil? - github_options[:site] = options[:github_endpoint] unless options[:github_site].nil? - - begin - @github = Github.new github_options - rescue - puts GH_RATE_LIMIT_EXCEEDED_MSG.yellow - end + @fetcher = GitHubChangelogGenerator::Fetcher.new @options @generator = Generator.new(@options) diff --git a/lib/github_changelog_generator/fetcher.rb b/lib/github_changelog_generator/fetcher.rb index 89938e9..62d3ee4 100644 --- a/lib/github_changelog_generator/fetcher.rb +++ b/lib/github_changelog_generator/fetcher.rb @@ -1,5 +1,40 @@ module GitHubChangelogGenerator # A Fetcher responsible for all requests to GitHub and all basic manipulation with related data (such as filtering, validating, e.t.c) + # + # Example: + # fetcher = GitHubChangelogGenerator::Fetcher.new options class Fetcher + def initialize(options = {}) + @options = options + + @user = @options[:user] + @project = @options[:project] + @github_token = fetch_github_token + + github_options = { per_page: PER_PAGE_NUMBER } + github_options[:oauth_token] = @github_token unless @github_token.nil? + github_options[:endpoint] = options[:github_endpoint] unless options[:github_endpoint].nil? + github_options[:site] = options[:github_endpoint] unless options[:github_site].nil? + + begin + @github = Github.new github_options + rescue + puts GH_RATE_LIMIT_EXCEEDED_MSG.yellow + end + end + + # Returns GitHub token. First try to use variable, provided by --token option, otherwise try to fetch it from CHANGELOG_GITHUB_TOKEN env variable. + # + # @return [String] + def fetch_github_token + env_var = @options[:token] ? @options[:token] : (ENV.fetch 'CHANGELOG_GITHUB_TOKEN', nil) + + unless env_var + puts 'Warning: No token provided (-t option) and variable $CHANGELOG_GITHUB_TOKEN was not found.'.yellow + puts 'This script can make only 50 requests to GitHub API per hour without token!'.yellow + end + + env_var + end end end