Skip to content

Commit

Permalink
Merge pull request #224 from dlamacchia/tt-221-namespace-conflict
Browse files Browse the repository at this point in the history
[#211] Update top-level namespace to Twitter::TwitterText::
  • Loading branch information
dlamacchia committed Dec 21, 2017
2 parents 36e245b + d0d802d commit ab02a1a
Show file tree
Hide file tree
Showing 24 changed files with 1,498 additions and 1,454 deletions.
25 changes: 25 additions & 0 deletions rb/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Changelog
All notable changes to this project will be documented in this file.

## [Unreleased]

## [2.1] - 2017-12-20
### Added
- This CHANGELOG.md file

### Changed
- Top-level namespace changed from `Twitter` to `Twitter::TwitterText`. This
resolves a namespace collision with the popular
[twitter gem](https://github.com/sferik/twitter). This is considered
a breaking change, so the version has been bumped to 2.1. This fixes
issue [#221](https://github.com/twitter/twitter-text/issues/221),
"NoMethodError Exception: undefined method `[]' for nil:NilClasswhen
using gem in rails app"

## [2.0.2] - 2017-12-18
### Changed
- Resolved issue
[#211](https://github.com/twitter/twitter-text/issues/211), "gem
breaks, asset file is a dangling symlink"
- config files, tld_lib.yml files now copied into the right place
- Rakefile now included `prebuild`, `clean` tasks
10 changes: 5 additions & 5 deletions rb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def parse_tweet(text, options = {}) { ... }
This method takes a string as input and returns a results object that
contains information about the
string. `Twitter::Validation::ParseResults` object includes:
string. `Twitter::TwitterText::Validation::ParseResults` object includes:
* `:weighted_length`: the overall length of the tweet with code points
weighted per the ranges defined in the configuration file.
Expand Down Expand Up @@ -78,7 +78,7 @@ payload see [Tweet updates](https://developer.twitter.com/en/docs/tweets/tweet-u
# Extraction
```ruby
class MyClass
include Twitter::Extractor
include Twitter::TwitterText::Extractor
usernames = extract_mentioned_screen_names("Mentioning @twitter and @jack")
# usernames = ["twitter", "jack"]
end
Expand All @@ -88,7 +88,7 @@ end

```ruby
class MyClass
include Twitter::Extractor
include Twitter::TwitterText::Extractor
extract_reply_screen_name("@twitter are you hiring?").do |username|
# username = "twitter"
end
Expand All @@ -101,7 +101,7 @@ end

```ruby
class MyClass
include Twitter::Autolink
include Twitter::TwitterText::Autolink

html = auto_link("link @user, please #request")
end
Expand All @@ -110,7 +110,7 @@ end
### For Ruby on Rails you want to add this to app/helpers/application_helper.rb
```ruby
module ApplicationHelper
include Twitter::Autolink
include Twitter::TwitterText::Autolink
end
```

Expand Down
771 changes: 386 additions & 385 deletions rb/lib/twitter-text/autolink.rb

Large diffs are not rendered by default.

95 changes: 48 additions & 47 deletions rb/lib/twitter-text/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,54 @@
# 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))
module TwitterText
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 ? self.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::TwitterText::WeightedRange.new(range) } if config.key?(:ranges) && config[:ranges].is_a?(Array)
end

self.default_configuration = self.configuration_from_file(CONFIG_V2)
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

20 changes: 11 additions & 9 deletions rb/lib/twitter-text/deprecation.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
module Twitter
module Deprecation
def deprecate(method, new_method = nil)
deprecated_method = :"deprecated_#{method}"
message = "Deprecation: `#{method}` is deprecated."
message << " Please use `#{new_method}` instead." if new_method
module TwitterText
module Deprecation
def deprecate(method, new_method = nil)
deprecated_method = :"deprecated_#{method}"
message = "Deprecation: `#{method}` is deprecated."
message << " Please use `#{new_method}` instead." if new_method

alias_method(deprecated_method, method)
define_method method do |*args, &block|
warn message unless $TESTING
send(deprecated_method, *args, &block)
alias_method(deprecated_method, method)
define_method method do |*args, &block|
warn message unless $TESTING
send(deprecated_method, *args, &block)
end
end
end
end
Expand Down
Loading

0 comments on commit ab02a1a

Please sign in to comment.