Class: Jekyll::GitHubMetadata::Client
  
  
  
  
  
    - Inherits:
- 
      Object
      
        
          - Object
- Jekyll::GitHubMetadata::Client
 show all
    - Defined in:
- lib/jekyll-github-metadata/client.rb
 
  
    
      Constant Summary
      collapse
    
    
      
        - InvalidMethodError =
          
        
- Class.new(NoMethodError) 
- BadCredentialsError =
          
        
- Class.new(StandardError) 
- API_CALLS =
          
  
 
- Set.new(%w(
  repository
  organization
  user
  repository?
  pages
  contributors
  releases
  latest_release
  list_repos
  organization_public_members
)) 
      Instance Method Summary
      collapse
    
    
  
  Constructor Details
  
    
  
  
    #initialize(options = nil)  ⇒ Client 
  
  
  
  
    
Returns a new instance of Client.
   
 
  
  
    | 
25
26
27 | # File 'lib/jekyll-github-metadata/client.rb', line 25
def initialize(options = nil)
  @client = build_octokit_client(options)
end | 
 
  
 
  Dynamic Method Handling
  
    This class handles dynamic methods through the method_missing method
    
  
  
    
  
  
    #method_missing(method_name, *args, &block)  ⇒ Object 
  
  
  
  
    | 
58
59
60
61
62
63
64
65
66
67
68
69 | # 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 | 
 
  
 
  
    Instance Method Details
    
      
  
  
    #accepts_client_method?(method_name)  ⇒ Boolean 
  
  
  
  
    | 
50
51
52 | # 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?  ⇒ Boolean 
  
  
  
  
    | 
91
92
93 | # File 'lib/jekyll-github-metadata/client.rb', line 91
def authenticated?
  !@client.access_token.to_s.empty?
end | 
 
    
      
  
  
    #build_octokit_client(options = nil)  ⇒ Object 
  
  
  
  
    | 
44
45
46
47
48 | # 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  ⇒ Object 
  
  
  
 
    
      
  
  
    #inspect  ⇒ Object 
  
  
  
  
    | 
87
88
89 | # File 'lib/jekyll-github-metadata/client.rb', line 87
def inspect
  "#<#{self.class.name} @client=#{client_inspect} @internet_connected=#{internet_connected?}>"
end | 
 
    
      
  
  
    #internet_connected?  ⇒ Boolean 
  
  
  
  
    | 
95
96
97
98
99
100
101
102
103
104
105
106
107
108 | # File 'lib/jekyll-github-metadata/client.rb', line 95
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 | 
 
    
      
  
  
    #respond_to_missing?(method_name, include_private = false)  ⇒ Boolean 
  
  
  
  
    | 
54
55
56 | # 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)  ⇒ Object 
  
  
  
  
    | 
29
30
31
32
33
34 | # 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)  ⇒ Object 
  
  
  
  
    | 
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 | # File 'lib/jekyll-github-metadata/client.rb', line 71
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::Error::ConnectionFailed, Octokit::TooManyRequests => e
  GitHubMetadata.log :warn, e.message
  default
rescue Octokit::NotFound
  default
end |