Class: Github::API

Inherits:
Object
  • Object
show all
Includes:
Authorization, Connection, Constants, MimeType, RateLimit, Request
Defined in:
lib/github_api/api.rb,
lib/github_api/api/actions.rb

Overview

Core class for api interface operations

Constant Summary

Constants included from Request

Request::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 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

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

Create new API



43
44
45
46
# File 'lib/github_api/api.rb', line 43

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

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

Responds to attribute query or attribute clear



77
78
79
80
81
82
83
84
85
86
# File 'lib/github_api/api.rb', line 77

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

Instance Attribute Details

#current_optionsObject

Returns the value of attribute current_options.



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

def current_options
  @current_options
end

Class Method Details

.inherited(klass) ⇒ Object

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

Instance Method Details

#api_methods_in(klass) ⇒ Object



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

#append_arguments(method) ⇒ Object



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

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

Acts as setter and getter for api requests arguments parsing.

Returns Arguments instance.



92
93
94
95
96
97
98
# File 'lib/github_api/api.rb', line 92

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

#process_basic_auth(auth) ⇒ Object

Extract login and password from basic_auth parameter



66
67
68
69
70
71
72
73
74
# File 'lib/github_api/api.rb', line 66

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

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

Set an option to a given value

Raises:

  • (ArgumentError)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/github_api/api.rb', line 115

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

#setup(options = {}) ⇒ Object

Configure options and process basic authorization



55
56
57
58
59
60
61
62
# File 'lib/github_api/api.rb', line 55

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

#with(args) ⇒ Object

Scope for passing request required arguments.



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/github_api/api.rb', line 102

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

#yield_or_eval(&block) ⇒ Object



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

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