Sha256: c986587826fb092df3d8af96a0322280157959a274f45638da55bbbba069147f

Contents?: true

Size: 1.9 KB

Versions: 20

Compression:

Stored size: 1.9 KB

Contents

module Twitter
  # A module provides base methods to rewrite usernames, lists, hashtags and URLs.
  module Rewriter extend self
    def rewrite_entities(text, entities)
      chars = text.to_s.to_char_a

      # sort by start index
      entities = entities.sort_by{|entity| entity[:indices].first}

      result = []
      last_index = entities.inject(0) do |last_index, entity|
        result << chars[last_index...entity[:indices].first]
        result << yield(entity, chars)
        entity[:indices].last
      end
      result << chars[last_index..-1]

      result.flatten.join
    end

    # These methods are deprecated, will be removed in future.
    extend Deprecation

    def rewrite(text, options = {})
      [:hashtags, :urls, :usernames_or_lists].inject(text) do |key|
        options[key] ? send(:"rewrite_#{key}", text, &options[key]) : text
      end
    end
    deprecate :rewrite, :rewrite_entities

    def rewrite_usernames_or_lists(text)
      entities = Extractor.extract_mentions_or_lists_with_indices(text)
      rewrite_entities(text, entities) do |entity, chars|
        at = chars[entity[:indices].first]
        list_slug = entity[:list_slug]
        list_slug = nil if list_slug.empty?
        yield(at, entity[:screen_name], list_slug)
      end
    end
    deprecate :rewrite_usernames_or_lists, :rewrite_entities

    def rewrite_hashtags(text)
      entities = Extractor.extract_hashtags_with_indices(text)
      rewrite_entities(text, entities) do |entity, chars|
        hash = chars[entity[:indices].first]
        yield(hash, entity[:hashtag])
      end
    end
    deprecate :rewrite_hashtags, :rewrite_entities

    def rewrite_urls(text)
      entities = Extractor.extract_urls_with_indices(text, :extract_url_without_protocol => false)
      rewrite_entities(text, entities) do |entity, chars|
        yield(entity[:url])
      end
    end
    deprecate :rewrite_urls, :rewrite_entities
  end
end

Version data entries

20 entries across 20 versions & 4 rubygems

Version Path
twitter-text-1.9.2 lib/twitter-text/rewriter.rb
twitter-text-1.9.1 lib/twitter-text/rewriter.rb
twitter-text-1.9.0 lib/twitter-text/rewriter.rb
twitter-text-1.8.0 lib/twitter-text/rewriter.rb
twitter-text-1.7.0 lib/twitter-text/rewriter.rb
twitter-text-1.6.4 lib/twitter-text/rewriter.rb
twitter-text-relative-1.6.2.3 lib/twitter-text-relative/rewriter.rb
twitter-text-relative-1.6.2.pre.3 lib/twitter-text-relative/rewriter.rb
twitter-text-relative-1.6.2.2 lib/twitter-text-relative/rewriter.rb
twitter-text-relative-1.6.2.1 lib/twitter-text/rewriter.rb
twitter-text-1.6.3 lib/twitter-text/rewriter.rb
twitter-text-1.6.2 lib/twitter-text/rewriter.rb
ritter-0.0.91 lib/ritter-text/rewriter.rb
ritter-0.0.90 lib/ritter-text/rewriter.rb
ritter-0.0.89 lib/ritter-text/rewriter.rb
ritter-0.0.88 lib/twitter-text/rewriter.rb
twitter-text-editted-1.0.0 lib/twitter-text/rewriter.rb
twitter-text-1.6.1 lib/twitter-text/rewriter.rb
twitter-text-1.6.0 lib/twitter-text/rewriter.rb
twitter-text-1.5.0 lib/twitter-text/rewriter.rb