All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.messenger4j.webhook.factory.ReferralEventFactory Maven / Gradle / Ivy

package com.github.messenger4j.webhook.factory;

import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_ID;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_RECIPIENT;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_REFERRAL;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_SENDER;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_TIMESTAMP;
import static com.github.messenger4j.internal.gson.GsonUtil.getPropertyAsInstant;
import static com.github.messenger4j.internal.gson.GsonUtil.getPropertyAsJsonObject;
import static com.github.messenger4j.internal.gson.GsonUtil.getPropertyAsString;
import static com.github.messenger4j.internal.gson.GsonUtil.hasProperty;

import com.github.messenger4j.webhook.event.ReferralEvent;
import com.github.messenger4j.webhook.event.common.Referral;
import com.google.gson.JsonObject;
import java.time.Instant;

/**
 * @author Max Grabenhorst
 * @since 1.0.0
 */
final class ReferralEventFactory implements BaseEventFactory {

  @Override
  public boolean isResponsible(JsonObject messagingEvent) {
    return hasProperty(messagingEvent, PROP_REFERRAL);
  }

  @Override
  public ReferralEvent createEventFromJson(JsonObject messagingEvent) {
    final String senderId =
        getPropertyAsString(messagingEvent, PROP_SENDER, PROP_ID)
            .orElseThrow(IllegalArgumentException::new);
    final String recipientId =
        getPropertyAsString(messagingEvent, PROP_RECIPIENT, PROP_ID)
            .orElseThrow(IllegalArgumentException::new);
    final Instant timestamp =
        getPropertyAsInstant(messagingEvent, PROP_TIMESTAMP)
            .orElseThrow(IllegalArgumentException::new);
    final Referral referral =
        getPropertyAsJsonObject(messagingEvent, PROP_REFERRAL)
            .map(this::createReferralFromJson)
            .orElseThrow(IllegalArgumentException::new);

    return new ReferralEvent(senderId, recipientId, timestamp, referral);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy