forked from piotrmurach/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.rb
More file actions
169 lines (145 loc) · 4.29 KB
/
api.rb
File metadata and controls
169 lines (145 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- encoding: utf-8 -*-
require 'github_api/configuration'
require 'github_api/connection'
require 'github_api/request'
require 'github_api/mime_type'
require 'github_api/rate_limit'
require 'github_api/core_ext/hash'
require 'github_api/core_ext/array'
require 'github_api/compatibility'
require 'github_api/api/actions'
require 'github_api/api_factory'
module Github
# Core class for api interface operations
class API
include Constants
include Authorization
include MimeType
include Connection
include Request
include RateLimit
attr_reader *Configuration.keys
attr_accessor *Validations::VALID_API_KEYS
attr_accessor :current_options
# Callback to update current configuration options
class_eval do
Configuration.keys.each do |key|
define_method "#{key}=" do |arg|
self.instance_variable_set("@#{key}", arg)
self.current_options.merge!({:"#{key}" => arg})
end
end
end
# Create new API
#
def initialize(options={}, &block)
setup(options)
yield_or_eval(&block) if block_given?
end
def yield_or_eval(&block)
return unless block
block.arity > 0 ? yield(self) : self.instance_eval(&block)
end
# Configure options and process basic authorization
#
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
# Extract login and password from basic_auth parameter
#
def process_basic_auth(auth)
case auth
when String
self.login, self.password = auth.split(':', 2)
when Hash
self.login = auth[:login]
self.password = auth[:password]
end
end
# Responds to attribute query or attribute clear
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
# Acts as setter and getter for api requests arguments parsing.
#
# Returns Arguments instance.
#
def arguments(args=(not_set = true), options={}, &block)
if not_set
@arguments
else
@arguments = Arguments.new(self, options).parse(*args, &block)
end
end
# Scope for passing request required arguments.
#
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
# Set an option to a given value
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
private
# Set multiple options
#
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 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
# Dynamically define a method for setting request option
#
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 _merge_mime_type(resource, params) # :nodoc:
# params['resource'] = resource
# params['mime_type'] = params['mime_type'] || :raw
end
end # API
end # Github