Sha256: 46adbcae70d70c17bd4f8fb1975b7c9c8954ce9e07ccac59a2a844535cb73ab8

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

module BranchIO
  class LinkProperties
    attr_reader :tags
    attr_reader :channel
    attr_reader :feature
    attr_reader :campaign
    attr_reader :stage
    attr_reader :alias
    attr_reader :type
    attr_reader :data

    def self.wrap(options)
      if options.kind_of?(LinkProperties)
        options
      else
        new(options)
      end
    end

    def initialize(options = {})
      @tags     = options.delete(:tags) || options.delete("tags")
      @channel  = options.delete(:channel)  || options.delete("channel")
      @feature  = options.delete(:feature)  || options.delete("feature")
      @campaign = options.delete(:campaign) || options.delete("campaign")
      @stage    = options.delete(:stage)    || options.delete("stage")
      @alias    = options.delete(:alias)    || options.delete("alias")
      @type     = options.delete(:type)     || options.delete("type")
      @data     = options.delete(:data)     || options.delete("data")

      unless options.empty?
        raise ErrorInvalidParameters, options.keys
      end
    end

    def as_json
      json = {}
      json[:tags]     = tags if tags
      json[:channel]  = channel if channel
      json[:feature]  = feature if feature
      json[:campaign] = campaign if campaign
      json[:stage]    = stage if stage
      json[:alias]    = self.alias if self.alias
      json[:type]     = type if type
      json[:data]     = data if data
      json
    end

    class ErrorInvalidParameters < StandardError
      attr_reader :parameters
      def initialize(parameters)
        @parameters = parameters
        super("Invalid parameters for BranchIO::LinkProperties: \"#{@parameters.join(', ')}\"")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
branch_io-0.3.0 lib/branch_io/link_properties.rb
branch_io-0.2.0 lib/branch_io/link_properties.rb