class Fluent::MobileCarrierOutput

mobile carrieroutput

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_mobile_carrier.rb, line 13
def initialize
  super
  require 'yaml'
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_mobile_carrier.rb, line 18
def configure(conf)
  super
  config =  YAML.load_file(@config_yaml)
  fail 'invalid yaml' unless config.is_a? Hash
  @config = parse_config(config)

  if !@tag && !@remove_prefix && !@add_prefix
    fail Fluent::ConfigError, 'missing both of remove_prefix and add_prefix'
  end
  if @tag && (@remove_prefix || @add_prefix)
    fail Fluent::ConfigError, 'both of tag and remove_prefix/add_prefix must not be specified'
  end
  if @remove_prefix
    @removed_prefix_string = @remove_prefix + '.'
    @removed_length = @removed_prefix_string.length
  end
  @added_prefix_string = @add_prefix + '.' if @add_prefix
end
emit(tag, es, chain) click to toggle source
# File lib/fluent/plugin/out_mobile_carrier.rb, line 75
def emit(tag, es, chain)
  tag = tag_mangle(tag)
  es.each do |time, record|
    record.merge!(
      @out_key_mobile_carrier => judge_mobile_carrier(record[@key_name], @config)
    )
    Fluent::Engine.emit(tag, time, record)
  end
  chain.next
end
judge_mobile_carrier(ip_address, config) click to toggle source
# File lib/fluent/plugin/out_mobile_carrier.rb, line 66
def judge_mobile_carrier(ip_address, config)
  config.each do |carrier, ipaddr_list|
    if ipaddr_list.any? { |ipaddr| ipaddr.include? ip_address }
      return carrier
    end
  end
  @unknown_carrier
end
parse_config(raw_config) click to toggle source
# File lib/fluent/plugin/out_mobile_carrier.rb, line 56
def parse_config(raw_config)
  config = {}
  raw_config.each do |carrier, ip_address_list|
    config[carrier] = ip_address_list.map do |ip_address|
      TubeAddress.new(ip_address)
    end
  end
  config
end
tag_mangle(tag) click to toggle source
# File lib/fluent/plugin/out_mobile_carrier.rb, line 37
def tag_mangle(tag)
  if @tag
    @tag
  else
    if @remove_prefix &&
        ( (tag.start_with?(@removed_prefix_string) && tag.length > @removed_length) || tag == @remove_prefix)
      tag = tag[@removed_length..-1]
    end
    if @add_prefix
      tag = if tag && tag.length > 0
              @added_prefix_string + tag
            else
              @add_prefix
            end
    end
    tag
  end
end