Class: Jekyll::GitHubMetadata::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-github-metadata/client.rb

Constant Summary collapse

InvalidMethodError =
Class.new(NoMethodError)
API_CALLS =

Whitelisted API calls.

%w{
  repository
  organization
  repository?
  pages
  contributors
  releases
  list_repos
  organization_public_members
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Client

Returns a new instance of Client.



18
19
20
# File 'lib/jekyll-github-metadata/client.rb', line 18

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



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jekyll-github-metadata/client.rb', line 45

def method_missing(method_name, *args, &block)
  method = method_name.to_s
  if accepts_client_method?(method_name)
    instance_var_name = method.sub('?', '_')
    Jekyll.logger.debug "GitHub Metadata:", "Calling @client.#{method}(#{args.map(&:inspect).join(", ")})"
    instance_variable_get(:"@#{instance_var_name}") ||
      instance_variable_set(:"@#{instance_var_name}", 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

Returns:

  • (Boolean)


37
38
39
# File 'lib/jekyll-github-metadata/client.rb', line 37

def accepts_client_method?(method_name)
  API_CALLS.include?(method_name.to_s) && @client.respond_to?(method_name)
end

#build_octokit_client(options = nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/jekyll-github-metadata/client.rb', line 29

def build_octokit_client(options = nil)
  options = options || Hash.new
  unless options.key? :access_token
    options.merge! pluck_auth_method
  end
  Octokit::Client.new({:auto_paginate => true}.merge(options))
end

#inspectObject



72
73
74
# File 'lib/jekyll-github-metadata/client.rb', line 72

def inspect
  "#<#{self.class.name} @client=#{client_inspect}>"
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/jekyll-github-metadata/client.rb', line 41

def respond_to?(method_name, include_private = false)
  accepts_client_method?(method_name) || super
end

#safe_require(gem_name) ⇒ Object



22
23
24
25
26
27
# File 'lib/jekyll-github-metadata/client.rb', line 22

def safe_require(gem_name)
  require gem_name
  true
rescue LoadError
  false
end

#save_from_errors(default = false, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jekyll-github-metadata/client.rb', line 59

def save_from_errors(default = false, &block)
  if block.arity == 1
    block.call(@client)
  else
    block.call
  end
rescue Faraday::Error::ConnectionFailed,
  Octokit::NotFound,
  Octokit::Unauthorized,
  Octokit::TooManyRequests
  default
end