# frozen_string_literal: true require 'date' require_relative '../../response/buy_side_interactions_response' module ONEAccess module API module V3_0 class Interactions < Base module Types RESEARCH_READERSHIP = 'Research Readership' end DATE_FORMAT = '%Y-%m-%d %H:%M' DATE_FORMAT_REGEX = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}/ private_constant :DATE_FORMAT private_constant :DATE_FORMAT_REGEX api_path '/interactions' class << self def buy_side_interactions( type:, start_date:, end_date:, user_id:, broker:, company_name:, email: ) validate_date(start_date) validate_date(end_date) response = send_post( 'buysideinteractions', payload(type, start_date, end_date, user_id, broker, company_name, email), ) Response::BuySideInteractionsResponse.from_json(response.body) end private def validate_date(date_str) raise "Invalid format, must be: yyyy-mm-dd hh:mm, was: #{date_str}" unless valid_date_format?(date_str) raise "Invalid date: #{date_str}" unless valid_date?(date_str) nil end def valid_date?(date_str, format = DATE_FORMAT) Date.strptime(date_str, format) true rescue StandardError false end def valid_date_format?(date_str) date_str.match?(DATE_FORMAT_REGEX) end def payload(type, start_date, end_date, user_id, broker, company_name, email) { start_date: start_date, end_date: end_date, user_id: user_id.to_s, interaction_type: type, broker: broker.to_s, investor: company_name.to_s, investor_attendees: [{ email: email }], } end end end end end end