This repository has been archived by the owner on Aug 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
githubapi.rb
135 lines (116 loc) · 3.87 KB
/
githubapi.rb
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
#
# Copyright (c) 2013 zunda <zunda at freeshell.org>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
require 'open-uri'
require 'yajl'
# APIs referred from
# http://developer.github.com/v3/activity/events/types/
module GitHubApi
# Error from JSON parser - code needs fixed
class ParseError < StandardError; end
# Error from reading JSON - impossible to fix
class ReadError < StandardError; end
class Base
HOST = 'https://api.github.com'
VERSION = '0.2.0'
AGENT = "[email protected] - GitHubApi - #{VERSION}"
ACCEPT = "application/vnd.github.v3+json" # http://developer.github.com/v3/media/#beta-v3-and-the-future
attr_reader :json_url
attr_reader :js
attr_accessor :timestamp
# From GitHub API
attr_reader :comment
attr_reader :html_url
# From parent JSON from GitHub Archive
attr_accessor :location
attr_accessor :avatar
attr_accessor :type
def initialize(json_url, opts = {auth: []})
@json_url = json_url
@auth = opts[:auth]
end
def read_and_parse
begin
@js = Yajl::Parser.parse(open(@json_url, 'User-Agent' => AGENT, :http_basic_authentication => @auth, 'Accept' => ACCEPT).read)
rescue EOFError, Zlib::BufError, Errno::ECONNRESET, Net::ReadTimeout, Zlib::DataError => e
message = "#{e.message} (#{e.class}) for #{@json_url} as #{self.class}"
raise ReadError.new(message)
rescue Yajl::ParseError => e
message = "#{e.message} (#{e.class}) for #{@json_url} as #{self.class}"
raise ParseError.new(message)
end
@timestamp = Time.parse(@js['created_at']) if @js['created_at']
end
end
class SingleCommitComment < Base
def initialize(owner, repo, comment_id, opts = {auth: []})
super("#{HOST}/repos/#{owner}/#{repo}/comments/#{comment_id}", auth: opts[:auth])
end
def read_and_parse
super
@comment = @js['body']
@html_url = @js['html_url']
end
end
class Download < Base
def initialize(owner, repo, id, opts = {auth: []})
super("#{HOST}/repos/#{owner}/#{repo}/downloads/#{id}", auth: opts[:auth])
end
def read_and_parse
super
@comment = @js['body']
@html_url = @js['html_url']
end
end
class Gist < Base
def initialize(id, opts = {auth: []})
super("#{HOST}/gists/#{id}", auth: opts[:auth])
end
def read_and_parse
super
@comment = @js['description']
@html_url = @js['html_url']
end
end
class SinglePullRequest < Base
def initialize(owner, repo, number, opts = {auth: []})
super("#{HOST}/repos/#{owner}/#{repo}/pulls/#{number}", auth: opts[:auth])
end
def read_and_parse
super
@comment = @js['body']
@html_url = @js['html_url']
end
end
class Commit < Base
def initialize(owner, repo, sha, opts = {auth: []})
super("#{HOST}/repos/#{owner}/#{repo}/commits/#{sha}", auth: opts[:auth])
end
def read_and_parse
super
@timestamp = Time.parse(@js['commit']['author']['date'])
@comment = @js['commit']['message']
@html_url = @js['html_url']
end
end
end