tweet_validator 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 793588e80427f715a7817bc49999ce10d962504a
4
- data.tar.gz: d11e59328d7a39ec793ed99b08e41b3581c22996
3
+ metadata.gz: 4d165a51c510280185de444ac49a5b0e0dfefc08
4
+ data.tar.gz: f33444cf3603577c84abf248d2b46b5d6bf49d4a
5
5
  SHA512:
6
- metadata.gz: 464178eba9468210e647d43e6d7ec0a550143b23ec95ace0f7f3774367b59625ebf1b67190ab22c2a5c6bfa8e8eb21573a6369e22996c022b11709958f456075
7
- data.tar.gz: dae0fb0131cad2153c11b781228ee51fdbb39357bab3790bdae1956942b78fe192d897170bd4695a6ac251c2b6fe185eda5e6472293e7d22b6ba11e920041133
6
+ metadata.gz: 95037ca7d4df7856813923bb3bbdfb41d63e5bb43416ec75c244e0b17c72f7c9c7c419ccab3c28fa5af5860325360f66d7bef8abc42741693f6bbb7b1785b0da
7
+ data.tar.gz: 88a9ee0ada16a4a2798404bb9aa4d1ea753cb1fd4212b5265740b937c0d8f56c15e1fb68e23d0ed16469221facdceef188d0971e2b5e156447061ec368481f02
@@ -19,5 +19,4 @@ notifications:
19
19
  secure: ZVIAWIIVaTjj1m4CVUtCj8WZH6HlGvkceKIGbyMOYaSDBAF+2veKrfUMfTeiTiQqveV3dXFjfnHSuP6zV3X1qIT2MrFb+MsDF7uKOMY4xUL+4mtk72sZ1jCdgg5LoBtNz/ueDGI6wIR14q1Z+L9JIlgXSiXKZfoSJj6b71JCAKM=
20
20
  matrix:
21
21
  allow_failures:
22
- - rvm: 2.2
23
22
  - rvm: ruby-head
data/README.md CHANGED
@@ -8,6 +8,9 @@ tweet length check validator
8
8
  [![Code Climate](https://codeclimate.com/github/sue445/tweet_validator/badges/gpa.svg)](https://codeclimate.com/github/sue445/tweet_validator)
9
9
  [![Coverage Status](https://img.shields.io/coveralls/sue445/tweet_validator.svg)](https://coveralls.io/r/sue445/tweet_validator)
10
10
 
11
+ ## Requirements
12
+ ruby 2.0+
13
+
11
14
  ## Installation
12
15
 
13
16
  Add this line to your application's Gemfile:
@@ -60,10 +63,34 @@ tweet.valid?
60
63
  tweet.message = "a" * 140 + "%{screen_name}"
61
64
  tweet.valid?
62
65
  # => true
66
+
67
+ # url length is calculated as t.co
68
+ "https://github.com/sue445/tweet_validator".length
69
+ # => 41
70
+
71
+ tweet.message = "a" * 110 + "http://github.com/sue445/tweet_validator"
72
+ tweet.valid?
73
+ # => true
74
+ ```
75
+
76
+ ## Configuration
77
+ ```ruby
78
+ TweetValidator.config.short_url_length = 22
79
+ TweetValidator.config.short_url_length_https = 23
63
80
  ```
64
81
 
82
+ If `short_url_length` and `short_url_length_https` is changed, please set new value.
83
+
84
+ see. https://dev.twitter.com/rest/reference/get/help/configuration
85
+
65
86
  ## Changelog
66
- [full changelog](https://github.com/sue445/tweet_validatorcompare/0.0.1...master)
87
+ [full changelog](https://github.com/sue445/tweet_validator/compare/0.0.2...master)
88
+
89
+ ### 0.0.2
90
+ [full changelog](https://github.com/sue445/tweet_validator/compare/0.0.1...0.0.2)
91
+
92
+ * check `t.co` url length
93
+ * https://github.com/sue445/tweet_validator/pull/5
67
94
 
68
95
  ### 0.0.1
69
96
  * first release
@@ -1,7 +1,10 @@
1
1
  require "tweet_validator/version"
2
+ require "tweet_validator/config"
2
3
  require "tweet_validator/tweet_length_validator"
3
4
  require 'tweet_validator/railtie' if defined?(Rails)
4
5
 
5
6
  module TweetValidator
6
- # Your code goes here...
7
+ def self.config
8
+ TweetValidator::Config.config
9
+ end
7
10
  end
@@ -0,0 +1,16 @@
1
+ module TweetValidator
2
+ require "active_support/configurable"
3
+
4
+ class Config
5
+ include ActiveSupport::Configurable
6
+
7
+ config_accessor :short_url_length
8
+ config_accessor :short_url_length_https
9
+
10
+ configure do |config|
11
+ # via. https://dev.twitter.com/rest/reference/get/help/configuration
12
+ config.short_url_length = 22
13
+ config.short_url_length_https = 23
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,6 @@
1
1
  module TweetValidator
2
2
  require "active_model"
3
+ require "uri"
3
4
 
4
5
  class TweetLengthValidator < ActiveModel::EachValidator
5
6
  TWEET_MAX_LENGTH = 140
@@ -11,7 +12,7 @@ module TweetValidator
11
12
  def self.valid_tweet?(tweet)
12
13
  return false unless tweet
13
14
  return false if tweet.empty?
14
- return false unless sanitize(tweet).length <= TWEET_MAX_LENGTH
15
+ return false unless shorten_url_length(sanitize(tweet)) <= TWEET_MAX_LENGTH
15
16
 
16
17
  true
17
18
  end
@@ -19,5 +20,24 @@ module TweetValidator
19
20
  def self.sanitize(tweet)
20
21
  tweet.gsub(/%<.+?>/, "").gsub(/%{.+?}/, "")
21
22
  end
23
+
24
+ def self.shorten_url_length(tweet)
25
+ shorten_tweet = tweet.gsub(URI.regexp("http"), dummy_http_url).gsub(URI.regexp("https"), dummy_https_url)
26
+ shorten_tweet.length
27
+ end
28
+
29
+ private_class_method
30
+
31
+ def self.dummy_http_url
32
+ dummy_url("http://", TweetValidator.config.short_url_length)
33
+ end
34
+
35
+ def self.dummy_https_url
36
+ dummy_url("https://", TweetValidator.config.short_url_length_https)
37
+ end
38
+
39
+ def self.dummy_url(prefix, max_length)
40
+ prefix + "x" * (max_length - prefix.length)
41
+ end
22
42
  end
23
43
  end
@@ -1,3 +1,3 @@
1
1
  module TweetValidator
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,4 +1,7 @@
1
1
  describe TweetValidator::TweetLengthValidator do
2
+ let(:short_url_length) { TweetValidator.config.short_url_length }
3
+ let(:short_url_length_https){ TweetValidator.config.short_url_length_https }
4
+
2
5
  let(:model_class) do
3
6
  Struct.new(:message_template) do
4
7
  include ActiveModel::Validations
@@ -21,6 +24,8 @@ describe TweetValidator::TweetLengthValidator do
21
24
  ["a"],
22
25
  ["a" * 130 + "%<this_is_variable>"],
23
26
  ["a" * 130 + "%{this_is_variable}"],
27
+ ["a" * 110 + "http://github.com/sue445/tweet_validator"],
28
+ ["a" * 110 + "https://github.com/sue445/tweet_validator"],
24
29
  ]
25
30
  end
26
31
 
@@ -62,4 +67,26 @@ describe TweetValidator::TweetLengthValidator do
62
67
  end
63
68
 
64
69
  end
70
+
71
+ describe "#shorten_url_length" do
72
+ subject{ TweetValidator::TweetLengthValidator.shorten_url_length(tweet) }
73
+
74
+ where(:tweet, :shorten_length) do
75
+ [
76
+ ["github", 6],
77
+
78
+ ["http://github.com/" , short_url_length],
79
+ ["http://github.com/sue445/tweet_validator" , short_url_length],
80
+ ["github http://github.com/" , short_url_length + 7],
81
+
82
+ ["https://github.com/" , short_url_length_https],
83
+ ["https://github.com/sue445/tweet_validator", short_url_length_https],
84
+ ["github https://github.com/" , short_url_length_https + 7],
85
+ ]
86
+ end
87
+
88
+ with_them do
89
+ it { should eq shorten_length }
90
+ end
91
+ end
65
92
  end
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "activemodel"
22
+ spec.add_dependency "activesupport"
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.7"
24
25
  spec.add_development_dependency "codeclimate-test-reporter"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tweet_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - sueyoshi_go
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-04 00:00:00.000000000 Z
11
+ date: 2015-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -139,6 +153,7 @@ files:
139
153
  - README.md
140
154
  - Rakefile
141
155
  - lib/tweet_validator.rb
156
+ - lib/tweet_validator/config.rb
142
157
  - lib/tweet_validator/railtie.rb
143
158
  - lib/tweet_validator/tweet_length_validator.rb
144
159
  - lib/tweet_validator/version.rb
@@ -165,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
180
  version: '0'
166
181
  requirements: []
167
182
  rubyforge_project:
168
- rubygems_version: 2.2.2
183
+ rubygems_version: 2.4.5
169
184
  signing_key:
170
185
  specification_version: 4
171
186
  summary: tweet length check validator