-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
client.rb
146 lines (112 loc) · 3.71 KB
/
client.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
136
137
138
139
140
141
142
143
144
145
146
module Statistics
class Client
class_attribute :debug
self.debug = false
def initialize(endpoint, &block)
@conn = connection_for(endpoint, &block)
end
def get(path, params)
@conn.get(path, params).body
end
private
def connection_for(endpoint)
Faraday.new(endpoint) do |f|
f.response :logger if self.class.debug
f.response :json, :content_type => /\bjson$/
f.response :raise_error
yield f if block_given?
f.adapter Faraday.default_adapter
end
end
class Connpass
ENDPOINT = 'https://connpass.com/api/v1'.freeze
def initialize
@client = Client.new(ENDPOINT)
end
def search(keyword:)
@client.get('event/', { keyword: keyword, count: 100 })
end
def fetch_events(series_id:, yyyymm: nil, yyyymmdd: nil)
params = {
series_id: series_id,
start: 1,
count: 100
}
params[:ym] = yyyymm if yyyymm
params[:ymd] = yyyymmdd if yyyymmdd
events = []
loop do
part = @client.get('event/', params)
break if part['results_returned'].zero?
events.push(*part.fetch('events'))
break if part.size < params[:count]
break if params[:start] + params[:count] > part['results_available']
params[:start] += params[:count]
end
events
end
end
class Doorkeeper
ENDPOINT = 'https://api.doorkeeper.jp'.freeze
def initialize
@client = Client.new(ENDPOINT) do |c|
c.authorization(:Bearer, ENV.fetch('DOORKEEPER_API_TOKEN'))
end
@default_since = Time.zone.parse('2010-07-01')
@default_until = Time.zone.now.end_of_day
end
def search(keyword:)
@client.get('events', q: keyword, since: @default_since, expand: 'group')
end
def fetch_events(group_id:, since_at: @default_since, until_at: @default_until)
begin
params = {
page: 1,
since: since_at,
until: until_at
}
events = []
loop do
part = @client.get("groups/#{group_id}/events", params)
break if part.size.zero?
events.push(*part.map { |e| e['event'] })
break if part.size < 25 # 25 items / 1 request
params[:page] += 1
end
events
rescue Faraday::ClientError => e
raise e unless e.response[:status] == 429
puts 'API rate limit exceeded.'
puts "This task will retry in 60 seconds from now(#{Time.zone.now})."
sleep 60
retry
end
end
end
class Facebook
class_attribute :access_token
def initialize
@client = Koala::Facebook::API.new(self.access_token)
end
def fetch_events(group_id:, since_at: nil, until_at: nil)
params = {
fields: %i(attending_count start_time owner),
limit: 100
}.tap do |h|
# @note FacebookのGraph APIはPDTがタイムゾーンとなっており、
# JST<->PDTのオフセット8時間を追加した時刻をパラメータとする必要がある
# @see https://github.com/coderdojo-japan/coderdojo.jp/pull/182#discussion_r148935458
h[:since] = since_at.since(8.hours).to_i if since_at
h[:until] = until_at.since(8.hours).to_i if until_at
end
events = []
collection = @client.get_object("#{group_id}/events", params)
events.push(*collection.to_a)
while !collection.empty? && collection.paging['next'] do
events.push(*collection.next_page.to_a)
end
events
end
end
end
end