forked from github/developer.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources.rb
More file actions
165 lines (135 loc) · 5.55 KB
/
resources.rb
File metadata and controls
165 lines (135 loc) · 5.55 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
require 'pp'
require 'stringio'
require 'cgi'
require 'securerandom'
require 'json'
Dir[File.join(File.dirname(__FILE__), 'lib', 'responses', '*.rb')].each { |file| load file }
module GitHub
module Resources
module Helpers
STATUSES ||= {
200 => '200 OK',
201 => '201 Created',
202 => '202 Accepted',
204 => '204 No Content',
205 => '205 Reset Content',
301 => '301 Moved Permanently',
302 => '302 Found',
307 => '307 Temporary Redirect',
304 => '304 Not Modified',
401 => '401 Unauthorized',
403 => '403 Forbidden',
404 => '404 Not Found',
405 => '405 Method not allowed',
409 => '409 Conflict',
422 => '422 Unprocessable Entity',
500 => '500 Server Error',
502 => '502 Bad Gateway'
}
DefaultTimeFormat ||= "%B %-d, %Y".freeze
DATE_REGEXP = Regexp.compile(/(\d{4}-\d{1,2}-\d{1,2})/)
def post_date(item)
strftime item[:created_at]
end
def date_from_filename(filename)
date = DATE_REGEXP.match(filename)
return nil if date.nil?
date[1]
end
def strftime(time, format = DefaultTimeFormat)
return "" if time.nil?
attribute_to_time(time).strftime(format)
end
def avatar_for(login)
%(<img height="16" width="16" src="%s" alt="Avatar for #{login}" data-proofer-ignore/>) % avatar_url_for(login)
end
def avatar_url_for(login)
"https://github.com/#{login}.png"
end
def headers(status, head = {})
lines = ["Status: #{STATUSES[status]}"]
head.each do |key, value|
case key
when :pagination
lines << link_header(value)
else
lines << "#{key}: #{value}"
end
end
lines << "X-RateLimit-Limit: 5000" unless head.has_key?('X-RateLimit-Limit')
lines << "X-RateLimit-Remaining: 4999" unless head.has_key?('X-RateLimit-Remaining')
%(``` headers\n#{lines.join("\n")}\n```\n)
end
def link_header(rels)
formatted_rels = rels.map { |name, url| link_header_rel(name, url) }
lines = ["Link: #{formatted_rels.shift}"]
while formatted_rels.any?
lines.last << ","
lines << " #{formatted_rels.shift}"
end
lines
end
def link_header_rel(name, url)
%Q{<#{url}>; rel="#{name}"}
end
def default_pagination_rels
{
:next => "https://api.github.com/resource?page=2",
:last => "https://api.github.com/resource?page=5"
}
end
def json(key)
hash = get_resource(key)
hash = yield hash if block_given?
"``` json\n" + JSON.pretty_generate(hash) + "\n```\n"
end
def get_resource(key)
hash = case key
when Hash
h = {}
key.each { |k, v| h[k.to_s] = v }
h
when Array
key
else Resources.const_get("GitHub::Resources::Responses::#{key.to_s.upcase}")
end
end
def text_html(response, status, head = {})
hs = headers(status, head.merge('Content-Type' => 'text/html'))
res = CGI.escapeHTML(response)
hs + %(<pre class="body-response"><code>) + res + "</code></pre>"
end
def webhook_headers(event_name)
"<pre><code>" + File.read("lib/webhooks/#{event_name}.headers.txt") + "</code></pre>"
end
def webhook_payload(event_name)
"``` json\n" + File.read("lib/webhooks/#{event_name}.payload.json") + "```"
end
CONTENT ||= {
'IF_SITE_ADMIN' => "If you are an [authenticated](/v3/#authentication) site administrator for your Enterprise instance,",
"PUT_CONTENT_LENGTH" => "Note that you'll need to set `Content-Length` to zero when calling out to this endpoint. For more information, see \"[HTTP verbs](/v3/#http-verbs).\"",
"OPTIONAL_PUT_CONTENT_LENGTH" => "Note that, if you choose not to pass any parameters, you'll need to set `Content-Length` to zero when calling out to this endpoint. For more information, see \"[HTTP verbs](/v3/#http-verbs).\"",
"ORG_HOOK_CONFIG_HASH" =>
'''
Name | Type | Description
-----|------|--------------
`url` | `string` | **Required** The URL to which the payloads will be delivered.
`content_type` | `string` | The media type used to serialize the payloads. Supported values include `json` and `form`. The default is `form`.
`secret` | `string` | If provided, payloads will be delivered with an `X-Hub-Signature` header. The value of this header is computed as the [HMAC hex digest of the body, using the `secret` as the key][hub-signature].
`insecure_ssl` | `string` | Determines whether the SSL certificate of the host for `url` will be verified when delivering payloads. Supported values include `"0"` (verification is performed) and `"1"` (verification is not performed). The default is `"0"`. **We strongly recommend not setting this to "1" as you are subject to man-in-the-middle and other attacks.**
''',
"PRS_AS_ISSUES" =>
'''
{{#tip}}
**Note**: In the past, pull requests and issues were more closely aligned than they are now. As far as the API is concerned, every pull request is an issue, but not every issue is a pull request.
This endpoint may also return pull requests in the response. If an issue *is* a pull request, the object will include a `pull_request` key.
{{/tip}}
'''
}
def fetch_content(key)
CONTENT[key.to_s.upcase]
end
end
end
end
include GitHub::Resources::Helpers