lingfennan-github_api 0.18.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +20 -0
- data/README.md +741 -0
- data/lib/github_api.rb +93 -0
- data/lib/github_api/api.rb +398 -0
- data/lib/github_api/api/actions.rb +60 -0
- data/lib/github_api/api/arguments.rb +253 -0
- data/lib/github_api/api/config.rb +105 -0
- data/lib/github_api/api/config/property.rb +30 -0
- data/lib/github_api/api/config/property_set.rb +120 -0
- data/lib/github_api/api/factory.rb +33 -0
- data/lib/github_api/authorization.rb +75 -0
- data/lib/github_api/client.rb +77 -0
- data/lib/github_api/client/activity.rb +31 -0
- data/lib/github_api/client/activity/events.rb +233 -0
- data/lib/github_api/client/activity/feeds.rb +50 -0
- data/lib/github_api/client/activity/notifications.rb +181 -0
- data/lib/github_api/client/activity/starring.rb +130 -0
- data/lib/github_api/client/activity/watching.rb +176 -0
- data/lib/github_api/client/authorizations.rb +142 -0
- data/lib/github_api/client/authorizations/app.rb +98 -0
- data/lib/github_api/client/emojis.rb +19 -0
- data/lib/github_api/client/gists.rb +289 -0
- data/lib/github_api/client/gists/comments.rb +100 -0
- data/lib/github_api/client/git_data.rb +31 -0
- data/lib/github_api/client/git_data/blobs.rb +51 -0
- data/lib/github_api/client/git_data/commits.rb +101 -0
- data/lib/github_api/client/git_data/references.rb +150 -0
- data/lib/github_api/client/git_data/tags.rb +95 -0
- data/lib/github_api/client/git_data/trees.rb +113 -0
- data/lib/github_api/client/gitignore.rb +57 -0
- data/lib/github_api/client/issues.rb +248 -0
- data/lib/github_api/client/issues/assignees.rb +77 -0
- data/lib/github_api/client/issues/comments.rb +146 -0
- data/lib/github_api/client/issues/events.rb +50 -0
- data/lib/github_api/client/issues/labels.rb +189 -0
- data/lib/github_api/client/issues/milestones.rb +146 -0
- data/lib/github_api/client/markdown.rb +62 -0
- data/lib/github_api/client/meta.rb +19 -0
- data/lib/github_api/client/orgs.rb +127 -0
- data/lib/github_api/client/orgs/hooks.rb +182 -0
- data/lib/github_api/client/orgs/members.rb +142 -0
- data/lib/github_api/client/orgs/memberships.rb +131 -0
- data/lib/github_api/client/orgs/projects.rb +57 -0
- data/lib/github_api/client/orgs/teams.rb +407 -0
- data/lib/github_api/client/projects.rb +83 -0
- data/lib/github_api/client/projects/cards.rb +158 -0
- data/lib/github_api/client/projects/columns.rb +146 -0
- data/lib/github_api/client/pull_requests.rb +195 -0
- data/lib/github_api/client/pull_requests/comments.rb +140 -0
- data/lib/github_api/client/pull_requests/reviews.rb +158 -0
- data/lib/github_api/client/repos.rb +468 -0
- data/lib/github_api/client/repos/branches.rb +48 -0
- data/lib/github_api/client/repos/branches/protections.rb +75 -0
- data/lib/github_api/client/repos/collaborators.rb +84 -0
- data/lib/github_api/client/repos/comments.rb +125 -0
- data/lib/github_api/client/repos/commits.rb +80 -0
- data/lib/github_api/client/repos/contents.rb +246 -0
- data/lib/github_api/client/repos/deployments.rb +138 -0
- data/lib/github_api/client/repos/downloads.rb +62 -0
- data/lib/github_api/client/repos/forks.rb +48 -0
- data/lib/github_api/client/repos/hooks.rb +214 -0
- data/lib/github_api/client/repos/keys.rb +104 -0
- data/lib/github_api/client/repos/merging.rb +47 -0
- data/lib/github_api/client/repos/pages.rb +48 -0
- data/lib/github_api/client/repos/projects.rb +62 -0
- data/lib/github_api/client/repos/pub_sub_hubbub.rb +133 -0
- data/lib/github_api/client/repos/releases.rb +189 -0
- data/lib/github_api/client/repos/releases/assets.rb +136 -0
- data/lib/github_api/client/repos/releases/tags.rb +24 -0
- data/lib/github_api/client/repos/statistics.rb +89 -0
- data/lib/github_api/client/repos/statuses.rb +91 -0
- data/lib/github_api/client/say.rb +25 -0
- data/lib/github_api/client/scopes.rb +46 -0
- data/lib/github_api/client/search.rb +133 -0
- data/lib/github_api/client/search/legacy.rb +111 -0
- data/lib/github_api/client/users.rb +117 -0
- data/lib/github_api/client/users/emails.rb +65 -0
- data/lib/github_api/client/users/followers.rb +115 -0
- data/lib/github_api/client/users/keys.rb +104 -0
- data/lib/github_api/configuration.rb +70 -0
- data/lib/github_api/connection.rb +82 -0
- data/lib/github_api/constants.rb +61 -0
- data/lib/github_api/core_ext/array.rb +25 -0
- data/lib/github_api/core_ext/hash.rb +91 -0
- data/lib/github_api/core_ext/ordered_hash.rb +107 -0
- data/lib/github_api/deprecation.rb +39 -0
- data/lib/github_api/error.rb +32 -0
- data/lib/github_api/error/client_error.rb +89 -0
- data/lib/github_api/error/service_error.rb +223 -0
- data/lib/github_api/ext/faraday.rb +38 -0
- data/lib/github_api/mash.rb +7 -0
- data/lib/github_api/middleware.rb +37 -0
- data/lib/github_api/mime_type.rb +33 -0
- data/lib/github_api/normalizer.rb +23 -0
- data/lib/github_api/null_encoder.rb +25 -0
- data/lib/github_api/page_iterator.rb +138 -0
- data/lib/github_api/page_links.rb +63 -0
- data/lib/github_api/paged_request.rb +42 -0
- data/lib/github_api/pagination.rb +115 -0
- data/lib/github_api/parameter_filter.rb +35 -0
- data/lib/github_api/params_hash.rb +115 -0
- data/lib/github_api/rate_limit.rb +25 -0
- data/lib/github_api/request.rb +85 -0
- data/lib/github_api/request/basic_auth.rb +36 -0
- data/lib/github_api/request/jsonize.rb +54 -0
- data/lib/github_api/request/oauth2.rb +44 -0
- data/lib/github_api/request/verbs.rb +63 -0
- data/lib/github_api/response.rb +48 -0
- data/lib/github_api/response/atom_parser.rb +22 -0
- data/lib/github_api/response/follow_redirects.rb +140 -0
- data/lib/github_api/response/header.rb +87 -0
- data/lib/github_api/response/jsonize.rb +28 -0
- data/lib/github_api/response/mashify.rb +24 -0
- data/lib/github_api/response/raise_error.rb +22 -0
- data/lib/github_api/response/xmlize.rb +27 -0
- data/lib/github_api/response_wrapper.rb +161 -0
- data/lib/github_api/ssl_certs/cacerts.pem +2183 -0
- data/lib/github_api/utils/url.rb +63 -0
- data/lib/github_api/validations.rb +22 -0
- data/lib/github_api/validations/format.rb +26 -0
- data/lib/github_api/validations/presence.rb +32 -0
- data/lib/github_api/validations/required.rb +21 -0
- data/lib/github_api/validations/token.rb +41 -0
- data/lib/github_api/version.rb +5 -0
- metadata +338 -0
data/lib/github_api.rb
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'pp' if ENV['DEBUG']
|
|
4
|
+
|
|
5
|
+
require 'faraday'
|
|
6
|
+
require_relative 'github_api/ext/faraday'
|
|
7
|
+
|
|
8
|
+
module Github
|
|
9
|
+
LIBNAME = 'github_api'
|
|
10
|
+
|
|
11
|
+
LIBDIR = File.expand_path("../#{LIBNAME}", __FILE__)
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
# The client configuration
|
|
15
|
+
#
|
|
16
|
+
# @return [Configuration]
|
|
17
|
+
#
|
|
18
|
+
# @api public
|
|
19
|
+
def configuration
|
|
20
|
+
@configuration ||= Configuration.new
|
|
21
|
+
end
|
|
22
|
+
alias_method :config, :configuration
|
|
23
|
+
|
|
24
|
+
# Configure options
|
|
25
|
+
#
|
|
26
|
+
# @example
|
|
27
|
+
# Github.configure do |c|
|
|
28
|
+
# c.some_option = true
|
|
29
|
+
# end
|
|
30
|
+
#
|
|
31
|
+
# @yield the configuration block
|
|
32
|
+
# @yieldparam configuration [Github::Configuration]
|
|
33
|
+
# the configuration instance
|
|
34
|
+
#
|
|
35
|
+
# @return [nil]
|
|
36
|
+
#
|
|
37
|
+
# @api public
|
|
38
|
+
def configure
|
|
39
|
+
yield configuration
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Alias for Github::Client.new
|
|
43
|
+
#
|
|
44
|
+
# @param [Hash] options
|
|
45
|
+
# the configuration options
|
|
46
|
+
#
|
|
47
|
+
# @return [Github::Client]
|
|
48
|
+
#
|
|
49
|
+
# @api public
|
|
50
|
+
def new(options = {}, &block)
|
|
51
|
+
Client.new(options, &block)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Default middleware stack that uses default adapter as specified
|
|
55
|
+
# by configuration setup
|
|
56
|
+
#
|
|
57
|
+
# @return [Proc]
|
|
58
|
+
#
|
|
59
|
+
# @api private
|
|
60
|
+
def default_middleware(options = {})
|
|
61
|
+
Middleware.default(options)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Delegate to Github::Client
|
|
65
|
+
#
|
|
66
|
+
# @api private
|
|
67
|
+
def method_missing(method_name, *args, &block)
|
|
68
|
+
if new.respond_to?(method_name)
|
|
69
|
+
new.send(method_name, *args, &block)
|
|
70
|
+
elsif configuration.respond_to?(method_name)
|
|
71
|
+
Github.configuration.send(method_name, *args, &block)
|
|
72
|
+
else
|
|
73
|
+
super
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def respond_to?(method_name, include_private = false)
|
|
78
|
+
new.respond_to?(method_name, include_private) ||
|
|
79
|
+
configuration.respond_to?(method_name) ||
|
|
80
|
+
super(method_name, include_private)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end # Github
|
|
84
|
+
|
|
85
|
+
require_relative 'github_api/api'
|
|
86
|
+
require_relative 'github_api/client'
|
|
87
|
+
require_relative 'github_api/configuration'
|
|
88
|
+
require_relative 'github_api/deprecation'
|
|
89
|
+
require_relative 'github_api/core_ext/array'
|
|
90
|
+
require_relative 'github_api/core_ext/hash'
|
|
91
|
+
require_relative 'github_api/core_ext/ordered_hash'
|
|
92
|
+
require_relative 'github_api/middleware'
|
|
93
|
+
require_relative 'github_api/version'
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require_relative 'authorization'
|
|
4
|
+
require_relative 'api/actions'
|
|
5
|
+
require_relative 'api/factory'
|
|
6
|
+
require_relative 'api/arguments'
|
|
7
|
+
require_relative 'configuration'
|
|
8
|
+
require_relative 'constants'
|
|
9
|
+
require_relative 'mime_type'
|
|
10
|
+
require_relative 'null_encoder'
|
|
11
|
+
require_relative 'rate_limit'
|
|
12
|
+
require_relative 'request/verbs'
|
|
13
|
+
require_relative 'validations'
|
|
14
|
+
|
|
15
|
+
module Github
|
|
16
|
+
# Core class responsible for api interface operations
|
|
17
|
+
class API
|
|
18
|
+
include Constants
|
|
19
|
+
include Authorization
|
|
20
|
+
include MimeType
|
|
21
|
+
include RateLimit
|
|
22
|
+
include Request::Verbs
|
|
23
|
+
|
|
24
|
+
attr_reader(*Github.configuration.property_names)
|
|
25
|
+
|
|
26
|
+
attr_accessor(*Validations::VALID_API_KEYS)
|
|
27
|
+
|
|
28
|
+
attr_accessor :current_options
|
|
29
|
+
|
|
30
|
+
# Callback to update current configuration options
|
|
31
|
+
class_eval do
|
|
32
|
+
Github.configuration.property_names.each do |key|
|
|
33
|
+
define_method "#{key}=" do |arg|
|
|
34
|
+
self.instance_variable_set("@#{key}", arg)
|
|
35
|
+
self.current_options.merge!({:"#{key}" => arg})
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Requires internal libraries
|
|
41
|
+
#
|
|
42
|
+
# @param [String] prefix
|
|
43
|
+
# the relative path prefix
|
|
44
|
+
# @param [Array[String]] libs
|
|
45
|
+
# the array of libraries to require
|
|
46
|
+
#
|
|
47
|
+
# @return [self]
|
|
48
|
+
#
|
|
49
|
+
# @api public
|
|
50
|
+
def self.require_all(prefix, *libs)
|
|
51
|
+
libs.each do |lib|
|
|
52
|
+
require "#{File.join(prefix, lib)}"
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Create new API
|
|
57
|
+
#
|
|
58
|
+
# @api public
|
|
59
|
+
def initialize(options={}, &block)
|
|
60
|
+
opts = Github.configuration.fetch.merge(options)
|
|
61
|
+
@current_options = opts
|
|
62
|
+
|
|
63
|
+
Github.configuration.property_names.each do |key|
|
|
64
|
+
send("#{key}=", opts[key])
|
|
65
|
+
end
|
|
66
|
+
if opts.key?(:login) && !opts[:login].nil?
|
|
67
|
+
@login, @password = opts[:login], opts[:password]
|
|
68
|
+
elsif opts.key?(:basic_auth) && !opts[:basic_auth].nil?
|
|
69
|
+
@login, @password = extract_basic_auth(opts[:basic_auth])
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
yield_or_eval(&block) if block_given?
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Call block with argument
|
|
76
|
+
#
|
|
77
|
+
# @api private
|
|
78
|
+
def yield_or_eval(&block)
|
|
79
|
+
return unless block
|
|
80
|
+
block.arity > 0 ? yield(self) : self.instance_eval(&block)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Extract login and password from basic_auth parameter
|
|
84
|
+
#
|
|
85
|
+
# @api private
|
|
86
|
+
def extract_basic_auth(auth)
|
|
87
|
+
case auth
|
|
88
|
+
when String
|
|
89
|
+
auth.split(':', 2)
|
|
90
|
+
when Hash
|
|
91
|
+
[auth[:login], auth[:password]]
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Disable following redirects inside a block
|
|
96
|
+
#
|
|
97
|
+
# @api public
|
|
98
|
+
def disable_redirects
|
|
99
|
+
self.follow_redirects = false
|
|
100
|
+
yield
|
|
101
|
+
ensure
|
|
102
|
+
self.follow_redirects = true
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# List of before callbacks
|
|
106
|
+
#
|
|
107
|
+
# @api public
|
|
108
|
+
def self.before_callbacks
|
|
109
|
+
@before_callbacks ||= []
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# List of after callbacks
|
|
113
|
+
#
|
|
114
|
+
# @api public
|
|
115
|
+
def self.after_callbacks
|
|
116
|
+
@after_callbacks ||= []
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Before request filter
|
|
120
|
+
#
|
|
121
|
+
# @api public
|
|
122
|
+
def self.before_request(callback, params = {})
|
|
123
|
+
before_callbacks << params.merge(callback: callback)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# After request filter
|
|
127
|
+
#
|
|
128
|
+
# @api public
|
|
129
|
+
def self.after_request(callback, params = {})
|
|
130
|
+
after_callbacks << params.merge(callback: callback)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
class << self
|
|
134
|
+
attr_reader :root
|
|
135
|
+
alias_method :root?, :root
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def self.root!
|
|
139
|
+
@root = true
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def self.inherited(child_class)
|
|
143
|
+
before_callbacks.reverse_each { |callback|
|
|
144
|
+
child_class.before_callbacks.unshift(callback)
|
|
145
|
+
}
|
|
146
|
+
after_callbacks.reverse_each { |callback|
|
|
147
|
+
child_class.after_callbacks.unshift(callback)
|
|
148
|
+
}
|
|
149
|
+
extend_with_actions(child_class)
|
|
150
|
+
unless child_class.instance_variable_defined?(:@root)
|
|
151
|
+
child_class.instance_variable_set(:@root, false)
|
|
152
|
+
end
|
|
153
|
+
super
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
root!
|
|
157
|
+
|
|
158
|
+
def self.internal_methods
|
|
159
|
+
api = self
|
|
160
|
+
api = api.superclass until api.root?
|
|
161
|
+
api.public_instance_methods(true)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def self.extra_methods
|
|
165
|
+
['actions']
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# Find all the api methods that should be considred by
|
|
169
|
+
# request callbacks.
|
|
170
|
+
#
|
|
171
|
+
# @return [Set]
|
|
172
|
+
#
|
|
173
|
+
# @api private
|
|
174
|
+
def self.request_methods
|
|
175
|
+
@request_methods ||= begin
|
|
176
|
+
methods = (public_instance_methods(true) -
|
|
177
|
+
internal_methods +
|
|
178
|
+
public_instance_methods(false)).uniq.map(&:to_s)
|
|
179
|
+
Set.new(methods - extra_methods)
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def self.clear_request_methods!
|
|
184
|
+
@request_methods = nil
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def self.method_added(method_name)
|
|
188
|
+
method_name = method_name.to_s.gsub(/_with(out)?_callback_.*$/, '')
|
|
189
|
+
# Only subclasses matter
|
|
190
|
+
return if self.root?
|
|
191
|
+
return if extra_methods.include?(method_name)
|
|
192
|
+
# Only public methods are of interest
|
|
193
|
+
return unless request_methods.include?(method_name)
|
|
194
|
+
# Do not redefine
|
|
195
|
+
return if (@__methods_added ||= []).include?(method_name)
|
|
196
|
+
|
|
197
|
+
class_name = self.name.to_s.split('::').last.downcase
|
|
198
|
+
with_method = "#{method_name}_with_callback_#{class_name}"
|
|
199
|
+
without_method = "#{method_name}_without_callback_#{class_name}"
|
|
200
|
+
|
|
201
|
+
return if public_method_defined?(with_method)
|
|
202
|
+
|
|
203
|
+
[method_name, with_method, without_method].each do |met|
|
|
204
|
+
@__methods_added << met
|
|
205
|
+
end
|
|
206
|
+
return if public_method_defined?(with_method)
|
|
207
|
+
|
|
208
|
+
define_method(with_method) do |*args, &block|
|
|
209
|
+
send(:execute, without_method, *args, &block)
|
|
210
|
+
end
|
|
211
|
+
alias_method without_method, method_name
|
|
212
|
+
alias_method method_name, with_method
|
|
213
|
+
clear_request_methods!
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# Filter callbacks based on kind
|
|
217
|
+
#
|
|
218
|
+
# @param [Symbol] kind
|
|
219
|
+
# one of :before or :after
|
|
220
|
+
#
|
|
221
|
+
# @return [Array[Hash]]
|
|
222
|
+
#
|
|
223
|
+
# @api private
|
|
224
|
+
def filter_callbacks(kind, action_name)
|
|
225
|
+
self.class.send("#{kind}_callbacks").select do |callback|
|
|
226
|
+
callback[:only].nil? || callback[:only].include?(action_name)
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# Run all callbacks associated with this action
|
|
231
|
+
#
|
|
232
|
+
# @apram [Symbol] action_name
|
|
233
|
+
#
|
|
234
|
+
# @api private
|
|
235
|
+
def run_callbacks(action_name, &block)
|
|
236
|
+
filter_callbacks(:before, action_name).each { |hook| send hook[:callback] }
|
|
237
|
+
yield if block_given?
|
|
238
|
+
filter_callbacks(:after, action_name).each { |hook| send hook[:callback] }
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# Execute action
|
|
242
|
+
#
|
|
243
|
+
# @param [Symbol] action
|
|
244
|
+
#
|
|
245
|
+
# @api private
|
|
246
|
+
def execute(action, *args, &block)
|
|
247
|
+
action_name = action.to_s.gsub(/_with(out)?_callback_.*$/, '')
|
|
248
|
+
result = nil
|
|
249
|
+
run_callbacks(action_name) do
|
|
250
|
+
result = send(action, *args, &block)
|
|
251
|
+
end
|
|
252
|
+
result
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# Responds to attribute query or attribute clear
|
|
256
|
+
#
|
|
257
|
+
# @api private
|
|
258
|
+
def method_missing(method_name, *args, &block) # :nodoc:
|
|
259
|
+
case method_name.to_s
|
|
260
|
+
when /^(.*)\?$/
|
|
261
|
+
return !!send($1.to_s)
|
|
262
|
+
when /^clear_(.*)$/
|
|
263
|
+
send("#{$1.to_s}=", nil)
|
|
264
|
+
else
|
|
265
|
+
super
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
def respond_to?(method_name, include_private = false)
|
|
270
|
+
method_name.to_s.start_with?('clear_') || super
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Acts as setter and getter for api requests arguments parsing.
|
|
274
|
+
#
|
|
275
|
+
# Returns Arguments instance.
|
|
276
|
+
#
|
|
277
|
+
def arguments(args=(not_set = true), options={}, &block)
|
|
278
|
+
if not_set
|
|
279
|
+
@arguments
|
|
280
|
+
else
|
|
281
|
+
@arguments = Arguments.new(options.merge!(api: self)).parse(*args, &block)
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
# Set a configuration option for a given namespace
|
|
286
|
+
#
|
|
287
|
+
# @param [String] option
|
|
288
|
+
# @param [Object] value
|
|
289
|
+
# @param [Boolean] ignore_setter
|
|
290
|
+
#
|
|
291
|
+
# @return [self]
|
|
292
|
+
#
|
|
293
|
+
# @api public
|
|
294
|
+
def set(option, value=(not_set=true), ignore_setter=false, &block)
|
|
295
|
+
raise ArgumentError, 'value not set' if block and !not_set
|
|
296
|
+
return self if !not_set and value.nil?
|
|
297
|
+
|
|
298
|
+
if not_set
|
|
299
|
+
set_options option
|
|
300
|
+
return self
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
if respond_to?("#{option}=") and not ignore_setter
|
|
304
|
+
return __send__("#{option}=", value)
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
define_accessors option, value
|
|
308
|
+
self
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
# Defines a namespace
|
|
312
|
+
#
|
|
313
|
+
# @param [Array[Symbol]] names
|
|
314
|
+
# the name for the scope
|
|
315
|
+
#
|
|
316
|
+
# @example
|
|
317
|
+
# namespace :scopes
|
|
318
|
+
#
|
|
319
|
+
# @return [self]
|
|
320
|
+
#
|
|
321
|
+
# @api public
|
|
322
|
+
def self.namespace(*names)
|
|
323
|
+
options = names.last.is_a?(Hash) ? names.pop : {}
|
|
324
|
+
names = names.map(&:to_sym)
|
|
325
|
+
name = names.pop
|
|
326
|
+
|
|
327
|
+
if public_method_defined?(name)
|
|
328
|
+
raise ArgumentError, "namespace '#{name}' is already defined"
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
class_name = extract_class_name(name, options)
|
|
332
|
+
|
|
333
|
+
define_method(name) do |*args, &block|
|
|
334
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
335
|
+
API::Factory.new(class_name, current_options.merge(options), &block)
|
|
336
|
+
end
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
# Extracts class name from options
|
|
340
|
+
#
|
|
341
|
+
# @param [Hash] options
|
|
342
|
+
# @option options [String] :full_name
|
|
343
|
+
# the full name for the class
|
|
344
|
+
# @option options [Boolean] :root
|
|
345
|
+
# if the class is at the root or not
|
|
346
|
+
#
|
|
347
|
+
# @example
|
|
348
|
+
# extract_class_name(:stats, class_name: :statistics)
|
|
349
|
+
#
|
|
350
|
+
# @return [String]
|
|
351
|
+
#
|
|
352
|
+
# @api private
|
|
353
|
+
def self.extract_class_name(name, options)
|
|
354
|
+
converted = options.fetch(:full_name, name).to_s
|
|
355
|
+
converted = converted.split('_').map(&:capitalize).join
|
|
356
|
+
class_name = options.fetch(:root, false) ? '': "#{self.name}::"
|
|
357
|
+
class_name += converted
|
|
358
|
+
class_name
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
private
|
|
362
|
+
|
|
363
|
+
# Set multiple options
|
|
364
|
+
#
|
|
365
|
+
# @api private
|
|
366
|
+
def set_options(options)
|
|
367
|
+
unless options.respond_to?(:each)
|
|
368
|
+
raise ArgumentError, 'cannot iterate over value'
|
|
369
|
+
end
|
|
370
|
+
options.each { |key, value| set(key, value) }
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
# Define setters and getters
|
|
374
|
+
#
|
|
375
|
+
# @api private
|
|
376
|
+
def define_accessors(option, value)
|
|
377
|
+
setter = proc { |val| set option, val, true }
|
|
378
|
+
getter = proc { value }
|
|
379
|
+
|
|
380
|
+
define_singleton_method("#{option}=", setter) if setter
|
|
381
|
+
define_singleton_method(option, getter) if getter
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
# Dynamically define a method for setting request option
|
|
385
|
+
#
|
|
386
|
+
# @api private
|
|
387
|
+
def define_singleton_method(method_name, content=Proc.new)
|
|
388
|
+
(class << self; self; end).class_eval do
|
|
389
|
+
undef_method(method_name) if method_defined?(method_name)
|
|
390
|
+
if String === content
|
|
391
|
+
class_eval("def #{method_name}() #{content}; end")
|
|
392
|
+
else
|
|
393
|
+
define_method(method_name, &content)
|
|
394
|
+
end
|
|
395
|
+
end
|
|
396
|
+
end
|
|
397
|
+
end # API
|
|
398
|
+
end # Github
|