class Jekyll::GitHubMetadata::Client
Constants
- API_CALLS
Whitelisted API calls.
- BadCredentialsError
- FARADAY_FAILED_CONNECTION
NOTE: Faraday’s error classes have been simplified from v1.0.0.
In older versions, both ‘Faraday::Error::ConnectionFailed` and `Faraday::ConnectionFailed` were valid and equivalent to each other. From v1.0.0 onwards, `Faraday::Error::ConnectionFailed` no longer exists.
TODO: Remove in v2.0 of this plugin.
- InvalidMethodError
Public Class Methods
new(options = nil)
click to toggle source
# File lib/jekyll-github-metadata/client.rb, line 25 def initialize(options = nil) @client = build_octokit_client(options) end
Public Instance Methods
accepts_client_method?(method_name)
click to toggle source
# File lib/jekyll-github-metadata/client.rb, line 50 def accepts_client_method?(method_name) API_CALLS.include?(method_name.to_s) && @client.respond_to?(method_name) end
authenticated?()
click to toggle source
# File lib/jekyll-github-metadata/client.rb, line 106 def authenticated? !@client.access_token.to_s.empty? end
build_octokit_client(options = nil)
click to toggle source
# File lib/jekyll-github-metadata/client.rb, line 44 def build_octokit_client(options = nil) options ||= {} options.merge!(pluck_auth_method) unless options.key?(:access_token) Octokit::Client.new(default_octokit_options.merge(options)) end
default_octokit_options()
click to toggle source
# File lib/jekyll-github-metadata/client.rb, line 36 def default_octokit_options { :api_endpoint => Jekyll::GitHubMetadata::Pages.api_url, :web_endpoint => Jekyll::GitHubMetadata::Pages.github_url, :auto_paginate => true, } end
inspect()
click to toggle source
# File lib/jekyll-github-metadata/client.rb, line 102 def inspect "#<#{self.class.name} @client=#{client_inspect} @internet_connected=#{internet_connected?}>" end
internet_connected?()
click to toggle source
# File lib/jekyll-github-metadata/client.rb, line 110 def internet_connected? return @internet_connected if defined?(@internet_connected) require "resolv" begin Resolv::DNS.open do |dns| dns.timeouts = 2 dns.getaddress("api.github.com") end @internet_connected = true rescue Resolv::ResolvError @internet_connected = false end end
method_missing(method_name, *args, &block)
click to toggle source
Calls superclass method
# File lib/jekyll-github-metadata/client.rb, line 58 def method_missing(method_name, *args, &block) method = method_name.to_s if accepts_client_method?(method_name) key = cache_key(method_name, args) GitHubMetadata.log :debug, "Calling @client.#{method}(#{args.map(&:inspect).join(", ")})" cache[key] ||= save_from_errors { @client.public_send(method_name, *args, &block) } elsif @client.respond_to?(method_name) raise InvalidMethodError, "#{method_name} is not whitelisted on #{inspect}" else super end end
respond_to_missing?(method_name, include_private = false)
click to toggle source
Calls superclass method
# File lib/jekyll-github-metadata/client.rb, line 54 def respond_to_missing?(method_name, include_private = false) accepts_client_method?(method_name) || super end
safe_require(gem_name)
click to toggle source
# File lib/jekyll-github-metadata/client.rb, line 29 def safe_require(gem_name) require gem_name true rescue LoadError false end
save_from_errors(default = false) { |client| ... }
click to toggle source
# File lib/jekyll-github-metadata/client.rb, line 86 def save_from_errors(default = false) unless internet_connected? GitHubMetadata.log :warn, "No internet connection. GitHub metadata may be missing or incorrect." return default end yield @client rescue Octokit::Unauthorized raise BadCredentialsError, "The GitHub API credentials you provided aren't valid." rescue Faraday::ConnectionFailed, Octokit::TooManyRequests => e GitHubMetadata.log :warn, e.message default rescue Octokit::NotFound default end
Private Instance Methods
cache()
click to toggle source
# File lib/jekyll-github-metadata/client.rb, line 153 def cache @cache ||= {} end
cache_key(method, *args)
click to toggle source
rubocop:enable Metrics/CyclomaticComplexity
# File lib/jekyll-github-metadata/client.rb, line 149 def cache_key(method, *args) Digest::SHA1.hexdigest(method.to_s + args.join(", ")) end
client_inspect()
click to toggle source
# File lib/jekyll-github-metadata/client.rb, line 127 def client_inspect if @client.nil? "nil" else "#<#{@client.class.name} (#{"un" unless authenticated?}authenticated)>" end end
pluck_auth_method()
click to toggle source
rubocop:disable Metrics/CyclomaticComplexity
# File lib/jekyll-github-metadata/client.rb, line 136 def pluck_auth_method if ENV["JEKYLL_GITHUB_TOKEN"] || Octokit.access_token { :access_token => ENV["JEKYLL_GITHUB_TOKEN"] || Octokit.access_token } elsif !ENV["NO_NETRC"] && File.exist?(File.join(Dir.home, ".netrc")) && safe_require("netrc") { :netrc => true } else GitHubMetadata.log :warn, "No GitHub API authentication could be found. " \ "Some fields may be missing or have incorrect data." {}.freeze end end