Class: Github::API

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
Authorization, Connection, Constants, MimeType, RateLimit, Request
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

Core class for api interface operations

Direct Known Subclasses

Client, Client::Activity, Client::Activity::Events, Client::Activity::Notifications, Client::Activity::Starring, Client::Activity::Watching, Client::Authorizations, 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::Members, Client::Orgs::Teams, Client::PullRequests, Client::PullRequests::Comments, Client::Repos, 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::Keys, Client::Repos::Merging, Client::Repos::Pages, Client::Repos::PubSubHubbub, Client::Repos::Releases, Client::Repos::Releases::Assets, Client::Repos::Statistics, Client::Repos::Statuses, Client::Scopes, Client::Search, Client::Search::Legacy, Client::Users, Client::Users::Emails, Client::Users::Followers, Client::Users::Keys, Emojis, Say

Defined Under Namespace

Classes: Arguments, Config, Factory

Constant Summary

Constant Summary

Constants included from Request

Request::HTTP_METHODS, Request::METHODS_WITH_BODIES

Constants included from Connection

Connection::ALLOWED_OPTIONS

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::SERVER, Constants::USER_AGENT

Constants included from MimeType

MimeType::MEDIA_LOOKUP

Instance Attribute Summary (collapse)

Attributes included from Authorization

#scopes

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from ClassMethods

configuration, require_all

Methods included from RateLimit

#ratelimit, #ratelimit_remaining

Methods included from Request

#delete_request, #get_request, #patch_request, #post_request, #put_request, #request

Methods included from Connection

#caching?, #clear_cache, #connection, #default_middleware, #default_options, #stack

Methods included from MimeType

#lookup_media, #parse

Methods included from Authorization

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

Constructor Details

- (API) initialize(options = {}, &block)

Create new API



47
48
49
50
# File 'lib/github_api/api.rb', line 47

def initialize(options={}, &block)
  setup(options)
  yield_or_eval(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method_name, *args, &block)

Responds to attribute query or attribute clear



82
83
84
85
86
87
88
89
90
91
# File 'lib/github_api/api.rb', line 82

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

Instance Attribute Details

- (Object) current_options

Returns the value of attribute current_options



32
33
34
# File 'lib/github_api/api.rb', line 32

def current_options
  @current_options
end

Class Method Details

+ (String) extract_class_name(name, options)

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

Returns:

  • (String)


172
173
174
175
176
177
178
# File 'lib/github_api/api.rb', line 172

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

+ (Object) inherited(klass)

Returns all API public methods for a given class.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/github_api/api/actions.rb', line 7

def self.inherited(klass)
  klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
    def self.actions
      self.new.api_methods_in(#{klass})
    end
    def actions
      api_methods_in(#{klass})
    end
  RUBY_EVAL
  super
end

+ (self) namespace(*names)

Defines a namespace

Parameters:

  • names (Array[Symbol])

    the name for the scope

Returns:

  • (self)


153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/github_api/api.rb', line 153

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

  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
  self
end

Instance Method Details

- (Object) api_methods_in(klass)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/github_api/api/actions.rb', line 19

def api_methods_in(klass)
  puts "---"
  (klass.send(:instance_methods, false) - ['actions']).sort.each do |method|
    puts "|--> #{method}"
  end
  klass.included_modules.each do |mod|
    if mod.to_s =~ /#{klass}/
      puts "| \\ #{mod.to_s}"
      mod.instance_methods(false).each do |met|
        puts "|  |--> #{met}"
      end
      puts "| /"
    end
  end
  puts "---"
  nil
end

- (Object) append_arguments(method)



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/github_api/api/actions.rb', line 37

def append_arguments(method)
  _method = self.method(method)
  if _method.arity == 0
    args = "()"
  elsif _method.arity > 0
    args = "(few)"
  else
    args = "(else)"
  end
  args
end

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

Acts as setter and getter for api requests arguments parsing.

Returns Arguments instance.



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

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

- (Object) process_basic_auth(auth)

Extract login and password from basic_auth parameter



71
72
73
74
75
76
77
78
79
# File 'lib/github_api/api.rb', line 71

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

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

Set an option to a given value

Parameters:

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

Returns:

  • (self)

Raises:

  • (ArgumentError)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/github_api/api.rb', line 128

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

- (Object) setup(options = {})

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.

Configure options and process basic authorization



60
61
62
63
64
65
66
67
# File 'lib/github_api/api.rb', line 60

def setup(options={})
  options = Github.configuration.fetch.merge(options)
  self.current_options = options
  Github.configuration.property_names.each do |key|
    send("#{key}=", options[key])
  end
  process_basic_auth(options[:basic_auth])
end

- (Object) with(args)

Scope for passing request required arguments.



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/github_api/api.rb', line 107

def with(args)
  case args
  when Hash
    set args
  when /.*\/.*/i
    user, repo = args.split('/')
    set :user => user, :repo => repo
  else
    ::Kernel.raise ArgumentError, 'This api does not support passed in arguments'
  end
end

- (Object) yield_or_eval(&block)



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

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