Class: Github::API

Inherits:
Object
  • Object
show all
Includes:
Authorization, Constants, MimeType, RateLimit, Request::Verbs
Defined in:
lib/github_api/api.rb,
lib/github_api/api/config.rb,
lib/github_api/api/actions.rb,
lib/github_api/api/factory.rb,
lib/github_api/api/arguments.rb,
lib/github_api/api/config/property.rb,
lib/github_api/api/config/property_set.rb

Overview

Responsible for providing inspection of api methods

Direct Known Subclasses

Client, Client::Activity, Client::Activity::Events, Client::Activity::Feeds, Client::Activity::Notifications, Client::Activity::Starring, Client::Activity::Watching, Client::Authorizations, Client::Emojis, Client::Gists, Client::Gists::Comments, Client::GitData, Client::GitData::Blobs, Client::GitData::Commits, Client::GitData::References, Client::GitData::Tags, Client::GitData::Trees, Client::Gitignore, Client::Issues, Client::Issues::Assignees, Client::Issues::Comments, Client::Issues::Events, Client::Issues::Labels, Client::Issues::Milestones, Client::Markdown, Client::Meta, Client::Orgs, Client::Orgs::Hooks, Client::Orgs::Members, Client::Orgs::Memberships, Client::Orgs::Projects, Client::Orgs::Teams, Client::Projects, Client::Projects::Cards, Client::Projects::Columns, Client::PullRequests, Client::PullRequests::Comments, Client::PullRequests::Reviews, Client::Repos, Client::Repos::Branches, Client::Repos::Branches::Protections, Client::Repos::Collaborators, Client::Repos::Comments, Client::Repos::Commits, Client::Repos::Contents, Client::Repos::Deployments, Client::Repos::Downloads, Client::Repos::Forks, Client::Repos::Hooks, Client::Repos::Invitations, Client::Repos::Keys, Client::Repos::Merging, Client::Repos::Pages, Client::Repos::Projects, Client::Repos::PubSubHubbub, Client::Repos::Releases, Client::Repos::Releases::Assets, Client::Repos::Releases::Tags, Client::Repos::Statistics, Client::Repos::Statuses, Client::Say, Client::Scopes, Client::Search, Client::Search::Legacy, Client::Users, Client::Users::Emails, Client::Users::Followers, Client::Users::Keys

Defined Under Namespace

Classes: Arguments, Config, Factory

Constant Summary

Constants included from MimeType

MimeType::MEDIA_LOOKUP

Constants included from Constants

Constants::ACCEPT, Constants::ACCEPTED_OAUTH_SCOPES, Constants::ACCEPT_CHARSET, Constants::CACHE_CONTROL, Constants::CONTENT_LENGTH, Constants::CONTENT_TYPE, Constants::DATE, Constants::ETAG, Constants::HEADER_LAST, Constants::HEADER_LINK, Constants::HEADER_NEXT, Constants::LOCATION, Constants::META_FIRST, Constants::META_LAST, Constants::META_NEXT, Constants::META_PREV, Constants::META_REL, Constants::OAUTH_SCOPES, Constants::PARAM_PAGE, Constants::PARAM_PER_PAGE, Constants::PARAM_START_PAGE, Constants::RATELIMIT_LIMIT, Constants::RATELIMIT_REMAINING, Constants::RATELIMIT_RESET, Constants::SERVER, Constants::USER_AGENT

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request::Verbs

#delete_request, #get_request, #head_request, #options_request, #patch_request, #post_request, #put_request

Methods included from RateLimit

#ratelimit, #ratelimit_remaining, #ratelimit_reset

Methods included from MimeType

#lookup_media, #parse

Methods included from Authorization

#auth_code, #authenticated?, #authentication, #authorize_url, #basic_authed?, #client, #get_token

Constructor Details

#initialize(options = {}, &block) ⇒ API

Create new API



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/github_api/api.rb', line 59

def initialize(options={}, &block)
  opts = Github.configuration.fetch.merge(options)
  @current_options = opts

  Github.configuration.property_names.each do |key|
    send("#{key}=", opts[key])
  end
  if opts.key?(:login) && !opts[:login].nil?
    @login, @password = opts[:login], opts[:password]
  elsif opts.key?(:basic_auth) && !opts[:basic_auth].nil?
    @login, @password = extract_basic_auth(opts[:basic_auth])
  end

  yield_or_eval(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Responds to attribute query or attribute clear



258
259
260
261
262
263
264
265
266
267
# File 'lib/github_api/api.rb', line 258

def method_missing(method_name, *args, &block) # :nodoc:
  case method_name.to_s
  when /^(.*)\?$/
    return !!send($1.to_s)
  when /^clear_(.*)$/
    send("#{$1.to_s}=", nil)
  else
    super
  end
end

Class Attribute Details

.rootObject (readonly) Also known as: root?

Returns the value of attribute root.



134
135
136
# File 'lib/github_api/api.rb', line 134

def root
  @root
end

Instance Attribute Details

#current_optionsObject

Returns the value of attribute current_options.



28
29
30
# File 'lib/github_api/api.rb', line 28

def current_options
  @current_options
end

Class Method Details

.after_callbacksObject

List of after callbacks



115
116
117
# File 'lib/github_api/api.rb', line 115

def self.after_callbacks
  @after_callbacks ||= []
end

.after_request(callback, params = {}) ⇒ Object

After request filter



129
130
131
# File 'lib/github_api/api.rb', line 129

def self.after_request(callback, params = {})
  after_callbacks << params.merge(callback: callback)
end

.before_callbacksObject

List of before callbacks



108
109
110
# File 'lib/github_api/api.rb', line 108

def self.before_callbacks
  @before_callbacks ||= []
end

.before_request(callback, params = {}) ⇒ Object

Before request filter



122
123
124
# File 'lib/github_api/api.rb', line 122

def self.before_request(callback, params = {})
  before_callbacks << params.merge(callback: callback)
end

.clear_request_methods!Object



183
184
185
# File 'lib/github_api/api.rb', line 183

def self.clear_request_methods!
  @request_methods = nil
end

.extend_with_actions(child_class) ⇒ nil

Returns all API public methods for a given class.

Returns:

  • (nil)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/github_api/api/actions.rb', line 11

def self.extend_with_actions(child_class)
  return unless child_class.is_a?(Class)
  return if child_class.name.nil? # Skip anonymous classes

  child_class.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
    def self.actions
      self.new.actions
    end

    def actions
      api_methods_in(#{child_class}) + module_methods_in(#{child_class})
    end
  RUBY_EVAL
end

.extra_methodsObject



164
165
166
# File 'lib/github_api/api.rb', line 164

def self.extra_methods
  ['actions']
end

.extract_class_name(name, options) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extracts class name from options

Examples:

extract_class_name(:stats, class_name: :statistics)

Parameters:

Options Hash (options):

  • :full_name (String)

    the full name for the class

  • :root (Boolean)

    if the class is at the root or not

Returns:

  • (String)


353
354
355
356
357
358
359
# File 'lib/github_api/api.rb', line 353

def self.extract_class_name(name, options)
  converted  = options.fetch(:full_name, name).to_s
  converted  = converted.split('_').map(&:capitalize).join
  class_name = options.fetch(:root, false) ? '': "#{self.name}::"
  class_name += converted
  class_name
end

.inherited(child_class) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/github_api/api.rb', line 142

def self.inherited(child_class)
  before_callbacks.reverse_each { |callback|
    child_class.before_callbacks.unshift(callback)
  }
  after_callbacks.reverse_each { |callback|
    child_class.after_callbacks.unshift(callback)
  }
  extend_with_actions(child_class)
  unless child_class.instance_variable_defined?(:@root)
    child_class.instance_variable_set(:@root, false)
  end
  super
end

.internal_methodsObject



158
159
160
161
162
# File 'lib/github_api/api.rb', line 158

def self.internal_methods
  api = self
  api = api.superclass until api.root?
  api.public_instance_methods(true)
end

.method_added(method_name) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/github_api/api.rb', line 187

def self.method_added(method_name)
  method_name = method_name.to_s.gsub(/_with(out)?_callback_.*$/, '')
  # Only subclasses matter
  return if self.root?
  return if extra_methods.include?(method_name)
  # Only public methods are of interest
  return unless request_methods.include?(method_name)
  # Do not redefine
  return if (@__methods_added ||= []).include?(method_name)

  class_name     = self.name.to_s.split('::').last.downcase
  with_method    = "#{method_name}_with_callback_#{class_name}"
  without_method = "#{method_name}_without_callback_#{class_name}"

  return if public_method_defined?(with_method)

  [method_name, with_method, without_method].each do |met|
    @__methods_added << met
  end
  return if public_method_defined?(with_method)

  define_method(with_method) do |*args, &block|
    send(:execute, without_method, *args, &block)
  end
  alias_method without_method, method_name
  alias_method method_name, with_method
  clear_request_methods!
end

.namespace(*names) ⇒ self

Defines a namespace

Examples:

namespace :scopes

Parameters:

  • names (Array[Symbol])

    the name for the scope

Returns:

  • (self)


322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/github_api/api.rb', line 322

def self.namespace(*names)
  options = names.last.is_a?(Hash) ? names.pop : {}
  names   = names.map(&:to_sym)
  name    = names.pop

  if public_method_defined?(name)
    raise ArgumentError, "namespace '#{name}' is already defined"
  end

  class_name = extract_class_name(name, options)

  define_method(name) do |*args, &block|
    options = args.last.is_a?(Hash) ? args.pop : {}
    API::Factory.new(class_name, current_options.merge(options), &block)
  end
end

.request_methodsSet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find all the api methods that should be considred by request callbacks.

Returns:

  • (Set)


174
175
176
177
178
179
180
181
# File 'lib/github_api/api.rb', line 174

def self.request_methods
  @request_methods ||= begin
    methods = (public_instance_methods(true) -
               internal_methods +
               public_instance_methods(false)).uniq.map(&:to_s)
    Set.new(methods - extra_methods)
  end
end

.require_all(prefix, *libs) ⇒ self

Requires internal libraries

Parameters:

  • prefix (String)

    the relative path prefix

  • libs (Array[String])

    the array of libraries to require

Returns:

  • (self)


50
51
52
53
54
# File 'lib/github_api/api.rb', line 50

def self.require_all(prefix, *libs)
  libs.each do |lib|
    require "#{File.join(prefix, lib)}"
  end
end

.root!Object



138
139
140
# File 'lib/github_api/api.rb', line 138

def self.root!
  @root = true
end

Instance Method Details

#api_methods_in(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds api methods in a class

Parameters:

  • klass (Class)

    The klass to inspect for methods.



32
33
34
35
36
37
38
39
40
41
# File 'lib/github_api/api/actions.rb', line 32

def api_methods_in(klass)
  methods = klass.send(:instance_methods, false) - [:actions]
  methods.sort.each_with_object([]) do |method_name, accumulator|
    unless method_name.to_s.include?('with') ||
           method_name.to_s.include?('without')
      accumulator << method_name
    end
    accumulator
  end
end

#arguments(args = (not_set = true), options = {}, &block) ⇒ Object

Acts as setter and getter for api requests arguments parsing.

Returns Arguments instance.



277
278
279
280
281
282
283
# File 'lib/github_api/api.rb', line 277

def arguments(args=(not_set = true), options={}, &block)
  if not_set
    @arguments
  else
    @arguments = Arguments.new(options.merge!(api: self)).parse(*args, &block)
  end
end

#disable_redirectsObject

Disable following redirects inside a block



98
99
100
101
102
103
# File 'lib/github_api/api.rb', line 98

def disable_redirects
  self.follow_redirects = false
  yield
ensure
  self.follow_redirects = true
end

#execute(action, *args, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute action

Parameters:

  • action (Symbol)


246
247
248
249
250
251
252
253
# File 'lib/github_api/api.rb', line 246

def execute(action, *args, &block)
  action_name = action.to_s.gsub(/_with(out)?_callback_.*$/, '')
  result = nil
  run_callbacks(action_name) do
    result = send(action, *args, &block)
  end
  result
end

#extract_basic_auth(auth) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extract login and password from basic_auth parameter



86
87
88
89
90
91
92
93
# File 'lib/github_api/api.rb', line 86

def extract_basic_auth(auth)
  case auth
  when String
    auth.split(':', 2)
  when Hash
    [auth[:login], auth[:password]]
  end
end

#filter_callbacks(kind, action_name) ⇒ Array[Hash]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Filter callbacks based on kind

Parameters:

  • kind (Symbol)

    one of :before or :after

Returns:



224
225
226
227
228
# File 'lib/github_api/api.rb', line 224

def filter_callbacks(kind, action_name)
  self.class.send("#{kind}_callbacks").select do |callback|
    callback[:only].nil? || callback[:only].include?(action_name)
  end
end

#module_methods_in(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds methods included through class modules

Parameters:

  • klass (Class)

    The klass to inspect for methods.



49
50
51
52
53
54
55
56
57
58
# File 'lib/github_api/api/actions.rb', line 49

def module_methods_in(klass)
  klass.included_modules.each_with_object([]) do |mod, accumulator|
    if mod.to_s =~ /#{klass}/
      mod.instance_methods(false).each do |method|
        accumulator << method
      end
    end
    accumulator
  end
end

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

Returns:

  • (Boolean)


269
270
271
# File 'lib/github_api/api.rb', line 269

def respond_to?(method_name, include_private = false)
  method_name.to_s.start_with?('clear_') || super
end

#run_callbacks(action_name, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run all callbacks associated with this action

Parameters:

  • action_name (Symbol)


235
236
237
238
239
# File 'lib/github_api/api.rb', line 235

def run_callbacks(action_name, &block)
  filter_callbacks(:before, action_name).each { |hook| send hook[:callback] }
  yield if block_given?
  filter_callbacks(:after, action_name).each { |hook| send hook[:callback] }
end

#set(option, value = (not_set=true), ignore_setter = false, &block) ⇒ self

Set a configuration option for a given namespace

Parameters:

  • option (String)
  • value (Object) (defaults to: (not_set=true))
  • ignore_setter (Boolean) (defaults to: false)

Returns:

  • (self)

Raises:

  • (ArgumentError)


294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/github_api/api.rb', line 294

def set(option, value=(not_set=true), ignore_setter=false, &block)
  raise ArgumentError, 'value not set' if block and !not_set
  return self if !not_set and value.nil?

  if not_set
    set_options option
    return self
  end

  if respond_to?("#{option}=") and not ignore_setter
    return __send__("#{option}=", value)
  end

  define_accessors option, value
  self
end

#yield_or_eval(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Call block with argument



78
79
80
81
# File 'lib/github_api/api.rb', line 78

def yield_or_eval(&block)
  return unless block
  block.arity > 0 ? yield(self) : self.instance_eval(&block)
end