Sha256: 2434665349dc500be80e919db60c7a97d7e3b989e39e2d9ccc96df78a9089515

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

# encoding: UTF-8

module Twitter
  class Configuration
    require 'json'

    PARSER_VERSION_CLASSIC = "v1"
    PARSER_VERSION_DEFAULT = "v2"

    class << self
      attr_accessor :default_configuration
    end

    attr_reader :version, :max_weighted_tweet_length, :scale
    attr_reader :default_weight, :transformed_url_length, :ranges

    CONFIG_V1 = File.join(
      File.expand_path('../../../../config', __FILE__), # project root
      "#{PARSER_VERSION_CLASSIC}.json"
    )

    CONFIG_V2 = File.join(
      File.expand_path('../../../../config', __FILE__), # project root
      "#{PARSER_VERSION_DEFAULT}.json"
    )

    def self.parse_string(string, options = {})
      JSON.parse(string, options.merge(symbolize_names: true))
    end

    def self.parse_file(filename)
      string = File.open(filename, 'rb') { |f| f.read }
      parse_string(string)
    end

    def self.configuration_from_file(filename)
      config = parse_file(filename)
      config ? Twitter::Configuration.new(config) : nil
    end

    def initialize(config = {})
      @version = config[:version]
      @max_weighted_tweet_length = config[:maxWeightedTweetLength]
      @scale = config[:scale]
      @default_weight = config[:defaultWeight]
      @transformed_url_length = config[:transformedURLLength]
      @ranges = config[:ranges].map { |range| Twitter::WeightedRange.new(range) } if config.key?(:ranges) && config[:ranges].is_a?(Array)
    end

    self.default_configuration = Twitter::Configuration.configuration_from_file(Twitter::Configuration::CONFIG_V2)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twitter-text-2.0.0 lib/twitter-text/configuration.rb