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.

Set.new(%w{
  repository
  organization
  repository?
  pages
  contributors
  releases
  list_repos
  organization_public_members
})

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Client

Returns a new instance of Client.



20
21
22
# File 'lib/jekyll-github-metadata/client.rb', line 20

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



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

def method_missing(method_name, *args, &block)
  method = method_name.to_s
  if accepts_client_method?(method_name)
    key = cache_key(method_name, args)
    Jekyll::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

Returns:

  • (Boolean)


39
40
41
# File 'lib/jekyll-github-metadata/client.rb', line 39

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



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

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



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

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

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

Returns:

  • (Boolean)


43
44
45
# File 'lib/jekyll-github-metadata/client.rb', line 43

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

#safe_require(gem_name) ⇒ Object



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

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

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



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

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