Skip to content

Commit e43a707

Browse files
authored
Merge pull request #276 from AnaTofuZ/modified_providers_namespace
Providers名前空間の切り出し
2 parents c3a2f79 + 0f4dcd5 commit e43a707

File tree

21 files changed

+159
-156
lines changed

21 files changed

+159
-156
lines changed

lib/event_service.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module EventService; end
2+
3+
require_relative 'event_service/providers'
4+
require_relative 'event_service/client'

lib/statistics/client.rb renamed to lib/event_service/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Statistics
1+
module EventService
22
class Client
33
class_attribute :debug
44
self.debug = false

lib/statistics/providers.rb renamed to lib/event_service/providers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Statistics
1+
module EventService
22
module Providers
33
end
44
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module EventService
2+
module Providers
3+
class Connpass
4+
ENDPOINT = 'https://connpass.com/api/v1'.freeze
5+
6+
def initialize
7+
@client = EventService::Client.new(ENDPOINT)
8+
end
9+
10+
def search(keyword:)
11+
@client.get('event/', { keyword: keyword, count: 100 })
12+
end
13+
14+
def fetch_events(series_id:, yyyymm: nil, yyyymmdd: nil)
15+
params = {
16+
series_id: series_id,
17+
start: 1,
18+
count: 100
19+
}
20+
params[:ym] = yyyymm if yyyymm
21+
params[:ymd] = yyyymmdd if yyyymmdd
22+
events = []
23+
24+
loop do
25+
part = @client.get('event/', params)
26+
27+
break if part['results_returned'].zero?
28+
29+
events.push(*part.fetch('events'))
30+
31+
break if part.size < params[:count]
32+
33+
break if params[:start] + params[:count] > part['results_available']
34+
35+
params[:start] += params[:count]
36+
end
37+
38+
events
39+
end
40+
end
41+
end
42+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module EventService
2+
module Providers
3+
class Doorkeeper
4+
ENDPOINT = 'https://api.doorkeeper.jp'.freeze
5+
6+
def initialize
7+
@client = EventService::Client.new(ENDPOINT) do |c|
8+
c.authorization(:Bearer, ENV.fetch('DOORKEEPER_API_TOKEN'))
9+
end
10+
@default_since = Time.zone.parse('2010-07-01')
11+
@default_until = Time.zone.now.end_of_day
12+
end
13+
14+
def search(keyword:)
15+
@client.get('events', q: keyword, since: @default_since, expand: 'group')
16+
end
17+
18+
def fetch_events(group_id:, since_at: @default_since, until_at: @default_until)
19+
begin
20+
params = {
21+
page: 1,
22+
since: since_at,
23+
until: until_at
24+
}
25+
events = []
26+
27+
loop do
28+
part = @client.get("groups/#{group_id}/events", params)
29+
30+
break if part.size.zero?
31+
32+
events.push(*part.map { |e| e['event'] })
33+
34+
break if part.size < 25 # 25 items / 1 request
35+
36+
params[:page] += 1
37+
end
38+
39+
events
40+
rescue Faraday::ClientError => e
41+
raise e unless e.response[:status] == 429
42+
43+
puts 'API rate limit exceeded.'
44+
puts "This task will retry in 60 seconds from now(#{Time.zone.now})."
45+
sleep 60
46+
retry
47+
end
48+
end
49+
end
50+
end
51+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module EventService
2+
module Providers
3+
class Facebook
4+
class_attribute :access_token
5+
6+
def initialize
7+
@client = Koala::Facebook::API.new(self.access_token)
8+
end
9+
10+
def fetch_events(group_id:, since_at: nil, until_at: nil)
11+
params = {
12+
fields: %i(attending_count start_time owner),
13+
limit: 100
14+
}.tap do |h|
15+
# @note FacebookのGraph APIはPDTがタイムゾーンとなっており、
16+
# JST<->PDTのオフセット8時間を追加した時刻をパラメータとする必要がある
17+
# @see https://github.com/coderdojo-japan/coderdojo.jp/pull/182#discussion_r148935458
18+
h[:since] = since_at.since(8.hours).to_i if since_at
19+
h[:until] = until_at.since(8.hours).to_i if until_at
20+
end
21+
22+
events = []
23+
24+
collection = @client.get_object("#{group_id}/events", params)
25+
events.push(*collection.to_a)
26+
while !collection.empty? && collection.paging['next'] do
27+
events.push(*collection.next_page.to_a)
28+
end
29+
30+
events
31+
end
32+
end
33+
end
34+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module EventService
2+
module Providers
3+
class StaticYaml
4+
YAML_FILE = Rails.root.join('db', 'static_event_histories.yaml')
5+
6+
def fetch_events
7+
YAML.load_file(YAML_FILE) || []
8+
end
9+
end
10+
end
11+
end

lib/statistics.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module Statistics; end
22

3-
require_relative 'statistics/client'
4-
require_relative 'statistics/providers'
53
require_relative 'statistics/tasks'
64
require_relative 'statistics/aggregation'
5+
require_relative 'event_service'

lib/statistics/providers/connpass.rb

Lines changed: 0 additions & 42 deletions
This file was deleted.

lib/statistics/providers/doorkeeper.rb

Lines changed: 0 additions & 51 deletions
This file was deleted.

lib/statistics/providers/facebook.rb

Lines changed: 0 additions & 34 deletions
This file was deleted.

lib/statistics/providers/static_yaml.rb

Lines changed: 0 additions & 11 deletions
This file was deleted.

lib/statistics/tasks/connpass.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def self.delete_event_histories(period)
66
end
77

88
def initialize(dojos, date, weekly)
9-
@client = Providers::Connpass.new
9+
@client = EventService::Providers::Connpass.new
1010
@dojos = dojos
1111
@params = build_params(date, weekly)
1212
end

lib/statistics/tasks/doorkeeper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def self.delete_event_histories(period)
66
end
77

88
def initialize(dojos, date, weekly)
9-
@client = Providers::Doorkeeper.new
9+
@client = EventService::Providers::Doorkeeper.new
1010
@dojos = dojos
1111
@params = build_params(date, weekly)
1212
end

lib/statistics/tasks/facebook.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def self.delete_event_histories(period)
66
end
77

88
def initialize(dojos, date, weekly)
9-
@client = Providers::Facebook.new
9+
@client = EventService::Providers::Facebook.new
1010
@dojos = dojos
1111
@params = build_params(date, weekly)
1212
end

lib/statistics/tasks/static_yaml.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def self.delete_event_histories(_period)
66
end
77

88
def initialize(dojos, _date, _weekly)
9-
@client = Providers::StaticYaml.new
9+
@client = EventService::Providers::StaticYaml.new
1010
@dojos = dojos
1111
end
1212

lib/tasks/statistics.rake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require_relative '../statistics.rb'
33
namespace :statistics do
44
desc '月次/週次のイベント履歴を集計します'
55
task :aggregation, [:from, :to] => :environment do |tasks, args|
6-
Statistics::Providers::Facebook.access_token = Koala::Facebook::OAuth.new(ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET']).get_app_access_token
6+
EventService::Providers::Facebook.access_token = Koala::Facebook::OAuth.new(ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET']).get_app_access_token
77
Statistics::Aggregation.new(args).run
88
end
99

@@ -14,9 +14,9 @@ namespace :statistics do
1414
require 'pp'
1515

1616
puts 'Searching Connpass'
17-
pp Statistics::Providers::Connpass.new.search(keyword: args[:keyword])
17+
pp EventService::Providers::Connpass.new.search(keyword: args[:keyword])
1818

1919
puts 'Searching Doorkeeper'
20-
pp Statistics::Providers::Doorkeeper.new.search(keyword: args[:keyword])
20+
pp EventService::Providers::Doorkeeper.new.search(keyword: args[:keyword])
2121
end
2222
end

spec/lib/statistics/providers/connpass_spec.rb renamed to spec/lib/event_service/providers/connpass_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'rails_helper'
2-
require 'statistics'
2+
require 'event_service'
33

4-
RSpec.describe Statistics::Providers::Connpass do
4+
RSpec.describe EventService::Providers::Connpass do
55
include_context 'Use stubs for Connpass'
66

77
describe '#search' do

spec/lib/statistics/providers/doorkeeper_spec.rb renamed to spec/lib/event_service/providers/doorkeeper_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'rails_helper'
2-
require 'statistics'
2+
require 'event_service'
33

4-
RSpec.describe Statistics::Providers::Doorkeeper do
4+
RSpec.describe EventService::Providers::Doorkeeper do
55
include_context 'Use stubs for Doorkeeper'
66

77
describe '#search' do

spec/lib/statistics/providers/facebook_spec.rb renamed to spec/lib/event_service/providers/facebook_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'rails_helper'
2-
require 'statistics'
2+
require 'event_service'
33

4-
RSpec.describe Statistics::Providers::Facebook do
4+
RSpec.describe EventService::Providers::Facebook do
55
include_context 'Use stubs for Facebook'
66

77
describe '#fetch_events' do

0 commit comments

Comments
 (0)