|
- module Youtube
- class SearchClient
- API_BASE = 'https://www.googleapis.com/youtube/v3'
-
- def initialize api_key: ENV.fetch('YOUTUBE_API_KEY')
- @api_key = api_key
- end
-
- def search_videos query:, published_after: nil, published_before: nil, page_token: nil
- response = connection.get('search', {
- part: 'snippet',
- q: query,
- type: 'video',
- order: 'date',
- maxResults: 50,
- regionCode: 'JP',
- relevanceLanguage: 'ja',
- publishedAfter: published_after&.iso8601,
- publishedBefore: published_before&.iso8601,
- pageToken: page_token,
- key: @api_key }.compact)
-
- JSON.parse(response.body)
- end
-
- private
-
- def connection
- @connection ||= Faraday.new(url: API_BASE) do |faraday|
- faraday.response :raise_error
- end
- end
- end
- end
|