|
| 1 | +module Statistics |
| 2 | + class Client |
| 3 | + class_attribute :debug |
| 4 | + self.debug = false |
| 5 | + |
| 6 | + def initialize(endpoint, &block) |
| 7 | + @conn = connection_for(endpoint, &block) |
| 8 | + end |
| 9 | + |
| 10 | + def get(path, params) |
| 11 | + @conn.get(path, params).body |
| 12 | + end |
| 13 | + |
| 14 | + private |
| 15 | + |
| 16 | + def connection_for(endpoint, &block) |
| 17 | + Faraday.new(endpoint) do |f| |
| 18 | + f.response :logger if self.debug |
| 19 | + f.response :json, :content_type => /\bjson$/ |
| 20 | + f.response :raise_error |
| 21 | + |
| 22 | + yield f if block_given? |
| 23 | + |
| 24 | + f.adapter Faraday.default_adapter |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + class Connpass |
| 29 | + ENDPOINT = 'https://connpass.com/api/v1'.freeze |
| 30 | + |
| 31 | + def initialize |
| 32 | + @client = Client.new(ENDPOINT) |
| 33 | + end |
| 34 | + |
| 35 | + def fetch_series_id(**params) |
| 36 | + @client.get('event/', params.merge(count: 1)) |
| 37 | + .fetch('events') |
| 38 | + .first |
| 39 | + .dig('series', 'id') |
| 40 | + end |
| 41 | + |
| 42 | + def fetch_events(series_id:) |
| 43 | + @client.get('event/', series_id: series_id, count: 100) |
| 44 | + .fetch('events') |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + class Doorkeeper |
| 49 | + ENDPOINT = 'https://api.doorkeeper.jp'.freeze |
| 50 | + DEFAULT_SINCE = Date.parse('2010-07-01') |
| 51 | + |
| 52 | + def initialize |
| 53 | + @client = Client.new(ENDPOINT) do |c| |
| 54 | + c.authorization(:Bearer, ENV.fetch('DOORKEEPER_API_TOKEN')) |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + def fetch_group_id(keyword:) |
| 59 | + @client.get('events', q: keyword, since: DEFAULT_SINCE) |
| 60 | + .first |
| 61 | + .dig('event', 'group') |
| 62 | + end |
| 63 | + |
| 64 | + def fetch_events(group_id:, offset: 1, since: DEFAULT_SINCE) |
| 65 | + @client.get("groups/#{group_id}/events", offset: offset, since: since) |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | +end |
0 commit comments