lingfennan-github_api 0.18.2
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
module Github
|
5
|
+
# Normalize client-supplied parameter keys.
|
6
|
+
module Normalizer
|
7
|
+
# Turns any keys from nested hashes including nested arrays into strings
|
8
|
+
def normalize!(params)
|
9
|
+
case params
|
10
|
+
when Hash
|
11
|
+
params.keys.each do |k|
|
12
|
+
params[k.to_s] = params.delete(k)
|
13
|
+
normalize!(params[k.to_s])
|
14
|
+
end
|
15
|
+
when Array
|
16
|
+
params.map! do |el|
|
17
|
+
normalize!(el)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
params
|
21
|
+
end
|
22
|
+
end # Normalizer
|
23
|
+
end # Github
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
module Github
|
6
|
+
|
7
|
+
# Skip encoding of the key nested parameters
|
8
|
+
module NullParamsEncoder
|
9
|
+
if defined?(Faraday::NestedParamsEncoder)
|
10
|
+
class << self
|
11
|
+
Faraday::NestedParamsEncoder.singleton_methods do |m|
|
12
|
+
define_method m, ::Faraday::NestedParamsEncoder.method(m).to_proc
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.escape(s)
|
18
|
+
s.to_s
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.unescape(s)
|
22
|
+
s.to_s
|
23
|
+
end
|
24
|
+
end # NullEncoder
|
25
|
+
end # Github
|
@@ -0,0 +1,138 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
require_relative 'constants'
|
6
|
+
require_relative 'paged_request'
|
7
|
+
require_relative 'utils/url'
|
8
|
+
|
9
|
+
module Github
|
10
|
+
# A class responsible for requesting resources through page links
|
11
|
+
#
|
12
|
+
# @api private
|
13
|
+
class PageIterator
|
14
|
+
include Github::Constants
|
15
|
+
include Github::Utils::Url
|
16
|
+
include Github::PagedRequest
|
17
|
+
|
18
|
+
# Setup attribute accesor for all the link types
|
19
|
+
ATTRIBUTES = [META_FIRST, META_NEXT, META_PREV, META_LAST]
|
20
|
+
|
21
|
+
DEFAULT_SHA = 'master'
|
22
|
+
|
23
|
+
ATTRIBUTES.each do |attr|
|
24
|
+
attr_accessor :"#{attr}_page_uri", :"#{attr}_page"
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_reader :current_api
|
28
|
+
|
29
|
+
def initialize(links, current_api)
|
30
|
+
@links = links
|
31
|
+
@current_api = current_api
|
32
|
+
update_page_links(@links)
|
33
|
+
end
|
34
|
+
|
35
|
+
def next?
|
36
|
+
next_page == 0 || !next_page_uri.nil?
|
37
|
+
end
|
38
|
+
|
39
|
+
def count
|
40
|
+
parse_query(URI(last_page_uri).query)['page'] if last_page_uri
|
41
|
+
end
|
42
|
+
|
43
|
+
# Perform http get request for the first resource
|
44
|
+
#
|
45
|
+
def first
|
46
|
+
perform_request(first_page_uri) if first_page_uri
|
47
|
+
end
|
48
|
+
|
49
|
+
# Perform http get request for the next resource
|
50
|
+
#
|
51
|
+
def next
|
52
|
+
perform_request(next_page_uri) if next?
|
53
|
+
end
|
54
|
+
|
55
|
+
# Perform http get request for the previous resource
|
56
|
+
#
|
57
|
+
def prev
|
58
|
+
perform_request(prev_page_uri) if prev_page_uri
|
59
|
+
end
|
60
|
+
|
61
|
+
# Perform http get request for the last resource
|
62
|
+
#
|
63
|
+
def last
|
64
|
+
perform_request(last_page_uri) if last_page_uri
|
65
|
+
end
|
66
|
+
|
67
|
+
# Returns the result for a specific page.
|
68
|
+
#
|
69
|
+
def get_page(page_number)
|
70
|
+
# Find URI that we can work with, if we cannot get the first or the
|
71
|
+
# last page URI then there is only one page.
|
72
|
+
page_uri = first_page_uri || last_page_uri
|
73
|
+
return nil unless page_uri
|
74
|
+
|
75
|
+
perform_request(page_uri, page_number)
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def perform_request(page_uri_path, page_number = nil)
|
81
|
+
page_uri = URI(page_uri_path)
|
82
|
+
params = parse_query(page_uri.query)
|
83
|
+
|
84
|
+
if page_number
|
85
|
+
params['page'] = page_number
|
86
|
+
elsif next_page < 1
|
87
|
+
sha = sha(params)
|
88
|
+
params['sha'] = sha if sha
|
89
|
+
else
|
90
|
+
params['page'] = parse_page_number(page_uri_path)
|
91
|
+
end
|
92
|
+
params['per_page'] = parse_per_page_number(page_uri_path)
|
93
|
+
|
94
|
+
response = page_request(page_uri.path, params)
|
95
|
+
update_page_links response.links
|
96
|
+
response
|
97
|
+
end
|
98
|
+
|
99
|
+
def sha(params)
|
100
|
+
return params['last_sha'] if params.keys.include?('last_sha')
|
101
|
+
return DEFAULT_SHA if params.keys.include?('sha')
|
102
|
+
end
|
103
|
+
|
104
|
+
def parse_per_page_number(uri) # :nodoc:
|
105
|
+
parse_page_params(uri, PARAM_PER_PAGE)
|
106
|
+
end
|
107
|
+
|
108
|
+
def parse_page_number(uri) # :nodoc:
|
109
|
+
parse_page_params(uri, PARAM_PAGE)
|
110
|
+
end
|
111
|
+
|
112
|
+
# Extracts query string parameter for given name
|
113
|
+
def parse_page_params(uri, attr) # :nodoc:
|
114
|
+
return -1 if uri.nil? || uri.empty?
|
115
|
+
parsed = nil
|
116
|
+
begin
|
117
|
+
parsed = URI.parse(uri)
|
118
|
+
rescue URI::Error
|
119
|
+
return -1
|
120
|
+
end
|
121
|
+
param = parse_query_for_param(parsed.query, attr)
|
122
|
+
return -1 if param.nil? || param.empty?
|
123
|
+
begin
|
124
|
+
return param.to_i
|
125
|
+
rescue ArgumentError
|
126
|
+
return -1
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# Wholesale update of all link attributes
|
131
|
+
def update_page_links(links) # :nodoc:
|
132
|
+
ATTRIBUTES.each do |attr|
|
133
|
+
send(:"#{attr}_page_uri=", links.send(:"#{attr}"))
|
134
|
+
send(:"#{attr}_page=", parse_page_number(links.send(:"#{attr}")))
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end # PageIterator
|
138
|
+
end # Github
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative 'constants'
|
4
|
+
|
5
|
+
module Github
|
6
|
+
# Determines the links in the current response link header to be used
|
7
|
+
# to find the links to other pages of request responses. These will
|
8
|
+
# only be present if the result set size exceeds the per page limit.
|
9
|
+
#
|
10
|
+
# @api private
|
11
|
+
class PageLinks
|
12
|
+
include Github::Constants
|
13
|
+
|
14
|
+
DELIM_LINKS = ','.freeze # :nodoc:
|
15
|
+
|
16
|
+
# Hold the extracted values for URI from the Link header
|
17
|
+
# for the first, last, next and previous page.
|
18
|
+
attr_accessor :first, :last, :next, :prev
|
19
|
+
|
20
|
+
LINK_REGEX = /<([^>]+)>; rel=\"([^\"]+)\"/
|
21
|
+
|
22
|
+
# Parses links from executed request
|
23
|
+
#
|
24
|
+
# @param [Hash] response_headers
|
25
|
+
#
|
26
|
+
# @api private
|
27
|
+
def initialize(response_headers)
|
28
|
+
link_header = response_headers[HEADER_LINK]
|
29
|
+
if link_header && link_header =~ /(next|first|last|prev)/
|
30
|
+
extract_links(link_header)
|
31
|
+
else
|
32
|
+
# When on the first page
|
33
|
+
self.next = response_headers[HEADER_NEXT]
|
34
|
+
self.last = response_headers[HEADER_LAST]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def extract_links(link_header)
|
41
|
+
link_header.split(DELIM_LINKS).each do |link|
|
42
|
+
LINK_REGEX.match(link.strip) do |match|
|
43
|
+
url_part, meta_part = match[1], match[2]
|
44
|
+
next if !url_part || !meta_part
|
45
|
+
assign_url_part(meta_part, url_part)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def assign_url_part(meta_part, url_part)
|
51
|
+
case meta_part
|
52
|
+
when META_FIRST
|
53
|
+
self.first = url_part
|
54
|
+
when META_LAST
|
55
|
+
self.last = url_part
|
56
|
+
when META_NEXT
|
57
|
+
self.next = url_part
|
58
|
+
when META_PREV
|
59
|
+
self.prev = url_part
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end # PageLinks
|
63
|
+
end # Github
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative 'constants'
|
4
|
+
|
5
|
+
module Github
|
6
|
+
|
7
|
+
# A module that adds http get request to response pagination
|
8
|
+
module PagedRequest
|
9
|
+
include Github::Constants
|
10
|
+
|
11
|
+
FIRST_PAGE = 1 # Default request page if none provided
|
12
|
+
|
13
|
+
PER_PAGE = 30 # Default number of items as specified by API
|
14
|
+
|
15
|
+
NOT_FOUND = -1 # Either page or per_page parameter not present
|
16
|
+
|
17
|
+
# Check if current api instance has default per_page param set,
|
18
|
+
# otherwise use global default.
|
19
|
+
#
|
20
|
+
def default_page_size
|
21
|
+
current_api.per_page ? current_api.per_page : PER_PAGE
|
22
|
+
end
|
23
|
+
|
24
|
+
def default_page
|
25
|
+
current_api.page ? current_api.page : FIRST_PAGE
|
26
|
+
end
|
27
|
+
|
28
|
+
# Perform http get request with pagination parameters
|
29
|
+
#
|
30
|
+
def page_request(path, params={})
|
31
|
+
if params[PARAM_PER_PAGE] == NOT_FOUND
|
32
|
+
params[PARAM_PER_PAGE] = default_page_size
|
33
|
+
end
|
34
|
+
if params[PARAM_PAGE] && params[PARAM_PAGE] == NOT_FOUND
|
35
|
+
params[PARAM_PAGE] = default_page
|
36
|
+
end
|
37
|
+
|
38
|
+
current_api.get_request(path, ParamsHash.new(params))
|
39
|
+
end
|
40
|
+
|
41
|
+
end # PagedRequest
|
42
|
+
end # Github
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative 'constants'
|
4
|
+
require_relative 'page_links'
|
5
|
+
require_relative 'page_iterator'
|
6
|
+
|
7
|
+
module Github
|
8
|
+
# A module that decorates response with pagination helpers
|
9
|
+
module Pagination
|
10
|
+
include Github::Constants
|
11
|
+
|
12
|
+
# Return page links
|
13
|
+
def links
|
14
|
+
@links = Github::PageLinks.new(env[:response_headers])
|
15
|
+
end
|
16
|
+
|
17
|
+
# Retrive number of total pages base on current :per_page parameter
|
18
|
+
def count_pages
|
19
|
+
page_iterator.count.to_i
|
20
|
+
end
|
21
|
+
|
22
|
+
# Iterate over results set pages by automatically calling `next_page`
|
23
|
+
# until all pages are exhausted. Caution needs to be exercised when
|
24
|
+
# using this feature - 100 pages iteration will perform 100 API calls.
|
25
|
+
# By default this is off. You can set it on the client, individual API
|
26
|
+
# instances or just per given request.
|
27
|
+
#
|
28
|
+
def auto_paginate(auto=false)
|
29
|
+
if (current_api.auto_pagination? || auto) && self.body.is_a?(Array)
|
30
|
+
resources_bodies = []
|
31
|
+
each_page { |resource| resources_bodies += resource.body }
|
32
|
+
self.body = resources_bodies
|
33
|
+
end
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
# Iterator like each for response pages. If there are no pages to
|
38
|
+
# iterate over this method will return current page.
|
39
|
+
def each_page
|
40
|
+
yield self
|
41
|
+
while page_iterator.next?
|
42
|
+
yield next_page
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Retrives the result of the first page. Returns <tt>nil</tt> if there is
|
47
|
+
# no first page - either because you are already on the first page
|
48
|
+
# or there are no pages at all in the result.
|
49
|
+
def first_page
|
50
|
+
first_request = page_iterator.first
|
51
|
+
self.instance_eval { @env = first_request.env } if first_request
|
52
|
+
first_request
|
53
|
+
end
|
54
|
+
|
55
|
+
# Retrives the result of the next page. Returns <tt>nil</tt> if there is
|
56
|
+
# no next page or no pages at all.
|
57
|
+
def next_page
|
58
|
+
next_request = page_iterator.next
|
59
|
+
self.instance_eval { @env = next_request.env } if next_request
|
60
|
+
next_request
|
61
|
+
end
|
62
|
+
|
63
|
+
# Retrives the result of the previous page. Returns <tt>nil</tt> if there is
|
64
|
+
# no previous page or no pages at all.
|
65
|
+
def prev_page
|
66
|
+
prev_request = page_iterator.prev
|
67
|
+
self.instance_eval { @env = prev_request.env } if prev_request
|
68
|
+
prev_request
|
69
|
+
end
|
70
|
+
alias :previous_page :prev_page
|
71
|
+
|
72
|
+
# Retrives the result of the last page. Returns <tt>nil</tt> if there is
|
73
|
+
# no last page - either because you are already on the last page,
|
74
|
+
# there is only one page or there are no pages at all in the result.
|
75
|
+
def last_page
|
76
|
+
last_request = page_iterator.last
|
77
|
+
self.instance_eval { @env = last_request.env } if last_request
|
78
|
+
last_request
|
79
|
+
end
|
80
|
+
|
81
|
+
# Retrives a specific result for a page given page number.
|
82
|
+
# The <tt>page_number</tt> parameter is not validate, hitting a page
|
83
|
+
# that does not exist will return Github API error. Consequently, if
|
84
|
+
# there is only one page, this method returns nil
|
85
|
+
def page(page_number)
|
86
|
+
request = page_iterator.get_page(page_number)
|
87
|
+
self.instance_eval { @env = request.env } if request
|
88
|
+
request
|
89
|
+
end
|
90
|
+
|
91
|
+
# Returns <tt>true</tt> if there is another page in the result set,
|
92
|
+
# otherwise <tt>false</tt>
|
93
|
+
def has_next_page?
|
94
|
+
page_iterator.next?
|
95
|
+
end
|
96
|
+
|
97
|
+
# Handle pagination params when they are not passed directly
|
98
|
+
#
|
99
|
+
def self.per_page_as_param(per_page_config)
|
100
|
+
params = {}
|
101
|
+
if (per_page_config != Github::Configuration.property_set[:per_page])
|
102
|
+
params[:per_page] = per_page_config unless per_page_config.nil?
|
103
|
+
end
|
104
|
+
params
|
105
|
+
end
|
106
|
+
|
107
|
+
private
|
108
|
+
|
109
|
+
# Internally used page iterator
|
110
|
+
def page_iterator # :nodoc:
|
111
|
+
@page_iterator = Github::PageIterator.new(links, current_api)
|
112
|
+
end
|
113
|
+
|
114
|
+
end # Pagination
|
115
|
+
end # Github
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative 'params_hash'
|
4
|
+
require_relative 'validations'
|
5
|
+
|
6
|
+
module Github
|
7
|
+
# Allows you to specify parameters keys which will be preserved
|
8
|
+
# in parameters hash and its subhashes. Any keys from the nested
|
9
|
+
# hash that do not match will be removed.
|
10
|
+
module ParameterFilter
|
11
|
+
|
12
|
+
# Removes any keys from nested hashes that don't match predefiend keys
|
13
|
+
#
|
14
|
+
def filter!(keys, params, options={:recursive => true}) # :nodoc:
|
15
|
+
case params
|
16
|
+
when Hash, ParamsHash
|
17
|
+
params.keys.each do |k, v|
|
18
|
+
unless (keys.include?(k) or Github::Validations::VALID_API_KEYS.include?(k))
|
19
|
+
params.delete(k)
|
20
|
+
else
|
21
|
+
filter!(keys, params[k]) if options[:recursive]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
when Array
|
25
|
+
params.map! do |el|
|
26
|
+
filter!(keys, el) if options[:recursive]
|
27
|
+
end
|
28
|
+
else
|
29
|
+
params
|
30
|
+
end
|
31
|
+
return params
|
32
|
+
end
|
33
|
+
|
34
|
+
end # Filter
|
35
|
+
end # Github
|