class Github::API
Core class responsible for api interface operations
def self.after_callbacks
- Api: - public
def self.after_callbacks @after_callbacks ||= [] end
def self.after_request(callback, params = {})
- Api: - public
def self.after_request(callback, params = {}) after_callbacks << params.merge(callback: callback) end
def self.before_callbacks
- Api: - public
def self.before_callbacks @before_callbacks ||= [] end
def self.before_request(callback, params = {})
- Api: - public
def self.before_request(callback, params = {}) before_callbacks << params.merge(callback: callback) end
def self.clear_request_methods!
def self.clear_request_methods! @request_methods = nil end
def self.extend_with_actions(child_class)
- Api: - public
Returns:
-
(nil)
-
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
def self.extra_methods
def self.extra_methods ['actions'] end
def self.extract_class_name(name, options)
- Api: - private
Returns:
-
(String)
-
Options Hash:
(**options)
-
:root
(Boolean
) -- -
:full_name
(String
) --
Parameters:
-
options
(Hash
) --
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
def self.inherited(child_class)
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
def self.internal_methods
def self.internal_methods api = self api = api.superclass until api.root? api.public_instance_methods(true) end
def self.method_added(method_name)
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
def self.namespace(*names)
- Api: - public
Returns:
-
(self)
-
Parameters:
-
names
(Array[Symbol]
) --
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
def self.request_methods
- Api: - private
Returns:
-
(Set)
-
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
def self.root!
def self.root! @root = true end
def api_methods_in(klass)
- Api: - private
Parameters:
-
klass
(Class
) --
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
def arguments(args=(not_set = true), options={}, &block)
Returns Arguments instance.
Acts as setter and getter for api requests arguments parsing.
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
def define_accessors(option, value)
- Api: - private
def define_accessors(option, value) setter = proc { |val| set option, val, true } getter = proc { value } define_singleton_method("#{option}=", setter) if setter define_singleton_method(option, getter) if getter end
def define_singleton_method(method_name, content=Proc.new)
- Api: - private
def define_singleton_method(method_name, content=Proc.new) (class << self; self; end).class_eval do undef_method(method_name) if method_defined?(method_name) if String === content class_eval("def #{method_name}() #{content}; end") else define_method(method_name, &content) end end end
def execute(action, *args, &block)
- Api: - private
Parameters:
-
action
(Symbol
) --
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
def extract_basic_auth(auth)
- Api: - private
def extract_basic_auth(auth) case auth when String auth.split(':', 2) when Hash [auth[:login], auth[:password]] end end
def filter_callbacks(kind, action_name)
- Api: - private
Returns:
-
(Array[Hash])
-
Parameters:
-
kind
(Symbol
) --
def filter_callbacks(kind, action_name) matched_callbacks = self.class.send("#{kind}_callbacks").select do |callback| callback[:only].nil? || callback[:only].include?(action_name) end end
def initialize(options={}, &block)
- Api: - public
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
def method_missing(method_name, *args, &block) # :nodoc:
- Api: - private
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
def module_methods_in(klass)
- Api: - private
Parameters:
-
klass
(Class
) --
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
def run_callbacks(action_name, &block)
- Api: - private
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
def set(option, value=(not_set=true), ignore_setter=false, &block)
- Api: - public
Returns:
-
(self)
-
Parameters:
-
ignore_setter
(Boolean
) -- -
value
(Object
) -- -
option
(String
) --
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
def set_options(options)
- Api: - private
def set_options(options) unless options.respond_to?(:each) raise ArgumentError, 'cannot iterate over value' end options.each { |key, value| set(key, value) } end
def yield_or_eval(&block)
- Api: - private
def yield_or_eval(&block) return unless block block.arity > 0 ? yield(self) : self.instance_eval(&block) end