pry-send_tweet.rb 0.10.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4464a0e5487676221d3f0809dab2dcdb2ac192d84ded42dbbae6cb9d8593e459
4
- data.tar.gz: 4944d0716568bb7687a5ca0917cd2df1772b4af5cb2bb25117a51d79a8ea9a45
3
+ metadata.gz: d1b348df95911cd2447ff36cd711b031eb3b874a14b4cf9392182d0a744ec760
4
+ data.tar.gz: ecd6e2c63167b4fca8721422e55daa70712d87e210b5ba5f21953b1576dad480
5
5
  SHA512:
6
- metadata.gz: 39cca7be237772f1ee3e4ad9c60d4db9bd35a7e96a23274c3d1bb852b838879b20dffe8a7c63568980e3ab022d67b2ae04c2615afea4e99f1fb6469c87bc2509
7
- data.tar.gz: 71ffdc012830a01ceb5a03b1b9ada4bb8899e7422a0e74e6824ff19296cea032fabca05d511dbdcbd202baf37816547414c89b01ad68806f2a083be8e46b5156
6
+ metadata.gz: 1d54e4d6756e28910ef90821fe1ac8b6c13e78d496cbe57265f16b040f256bb078eb1f1928c2f8767f68681a14ac4b1dfbbd09bfe569cedc14741b6d25ab2b48
7
+ data.tar.gz: ba04afd09168981415684e4779dbebd83cc34b5c0f5281318bc1503297faa8e624bb70d6bd4b38b914377dc44191820f2b79d83e73f4fa3287ede2c76d8e390b
@@ -1,5 +1,31 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## v0.11.0
4
+
5
+ * Apply a fix to tty-box for the `on-twitter --show-followers`, and
6
+ `on-twitter --show-following` commands.
7
+
8
+ * Add unicode support by applying a fix - as a monkey patch - to the `tty-box`
9
+ gem.
10
+
11
+ * Add `unicode-display_width`, and `unicode-emoji` as runtime dependencies.
12
+
13
+ * Add `read-tweets -x`, `read-tweets --translate` - for translating tweets,
14
+ using the Yandex translation API.
15
+
16
+ * `on-twitter --follow` & `on-twitter --unfollow` understand a string such as
17
+ `https://twitter.com/StephenKing/media` to mean `@StephenKing`.
18
+
19
+ * Include a URL to a users profile when following or unfollowing them.
20
+
21
+ * Remove duplicate username when replying to a tweet. This could happen when the
22
+ tweet author and the person replying to the tweet are the same.
23
+
24
+ * Update `Pry::SendTweet::UserRenderer` to use `config.box_width`, and
25
+ `config.box_height`.
26
+
27
+ * Use `page_ok()` from `send-tweet` command.
28
+
3
29
  ## v0.10.1
4
30
 
5
31
  * Improve forking implementation in `Pry::SendTweet::TweetRenderer`.
data/README.md CHANGED
@@ -52,15 +52,21 @@ Pry.configure do |config|
52
52
  consumer_key: '<consumer key>',
53
53
  consumer_secret: '<consumer secret>',
54
54
  access_token: '<access token>',
55
- access_token_secret: '<access token secret>'
55
+ access_token_secret: '<access token secret>',
56
56
  # Optional configuration
57
57
  # The User-Agent to use when making requests to the Twitter API.
58
58
  # Default is: 'pry-send_tweet.rb vX.Y.Z'.
59
59
  user_agent: 'Custom User-Agent',
60
+ # Optional configuration
60
61
  # The width & height of a box that contains a tweet or user.
61
62
  # The default width is 100 and the default height is 8.
62
63
  box_width: 120,
63
- box_height: 10
64
+ box_height: 10,
65
+ # Optional configuration
66
+ # A Yandex API key for translating tweets when using `read-tweets -x`.
67
+ yandex_key: 'xxx',
68
+ # The language tweets should be translated to, the default is 'en' (English).
69
+ yandex_lang: 'ru'
64
70
  })
65
71
  end
66
72
  ```
@@ -79,6 +85,9 @@ access_token_secret: '<access token secret>'
79
85
  # Optional
80
86
  user_agent: 'Your User-Agent string'
81
87
  line_width: 120
88
+ # Optional
89
+ yandex_key: xxx
90
+ yandex_lang: xx
82
91
  ```
83
92
 
84
93
  ## <a id='usage'>Usage</a>
@@ -165,6 +174,12 @@ feature.
165
174
 
166
175
  [1] pry(main)> read-tweets --replies github
167
176
 
177
+ * Read a translated copy of a tweet, using the
178
+ [Yandex translation API](https://translate.yandex.com/developers).
179
+ The configuration instructions for Yandex are included in the [configuration section](#configuration).
180
+
181
+ [1] pry(main) read-tweets -x https://twitter.com/user/status/1
182
+
168
183
  * Read 100 recent tweets instead of the default 200 recent tweets.
169
184
  **Tip:** `--count` can be combined with all of the options described above.
170
185
 
@@ -28,7 +28,7 @@ class Pry
28
28
  require 'timeout'
29
29
  require 'yaml'
30
30
  require 'time-lord'
31
- require 'tty/box'
31
+ require_relative 'pry/send_tweet/tty-box'
32
32
  require_relative 'pry/send_tweet/renderers/tweet_renderer'
33
33
  require_relative 'pry/send_tweet/renderers/user_renderer'
34
34
  require_relative 'pry/send_tweet/commands/paging/paging_support'
@@ -25,11 +25,12 @@ class Pry::SendTweet::BaseCommand < Pry::ClassCommand
25
25
  def box_width
26
26
  _pry_.config.twitter.box_width || Pry::SendTweet::DEFAULT_BOX_WIDTH
27
27
  end
28
-
28
+
29
29
  def search_str_for_users(*ary)
30
30
  ary.map do |str|
31
31
  if str.start_with?('https://twitter.com')
32
- File.basename URI.parse(URI.escape(str)).path
32
+ path = URI.parse(URI.escape(str)).path
33
+ path.split('/').reject(&:empty?).first
33
34
  elsif str.start_with?('@')
34
35
  str[1..-1]
35
36
  else
@@ -40,6 +41,7 @@ class Pry::SendTweet::BaseCommand < Pry::ClassCommand
40
41
 
41
42
  # With thanks to ActionView for this method
42
43
  def word_wrap(text, options = {})
44
+ line_width = box_width
43
45
  text.split("\n").collect! do |line|
44
46
  line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
45
47
  end * "\n"
@@ -57,11 +59,4 @@ class Pry::SendTweet::BaseCommand < Pry::ClassCommand
57
59
  def twitter
58
60
  @twitter ||= _pry_.binding_stack[-1].eval('_twitter_')
59
61
  end
60
-
61
- # @api private
62
- # TTY::Box doesn't handle unicode well, we remove non-ASCII characters as a
63
- # temporary work around.
64
- def __tty_box_sanitize(str)
65
- CGI.unescapeHTML str.chars.reject {|c| c.ord > 255}.join
66
- end
67
62
  end
@@ -1,4 +1,7 @@
1
1
  class Pry::SendTweet::ReadTweets < Pry::SendTweet::BaseCommand
2
+ require_relative 'read_tweets/translate_actions'
3
+ include TranslateActions
4
+
2
5
  match 'read-tweets'
3
6
  description 'A command for reading tweets.'
4
7
  banner <<-BANNER
@@ -18,12 +21,14 @@ class Pry::SendTweet::ReadTweets < Pry::SendTweet::BaseCommand
18
21
  argument: :optional
19
22
  o.on 'r=', 'replies=', 'A username whose replies you want to read'
20
23
  o.on 'm', 'mentions', 'Read tweets that @mention you.'
24
+ o.on 'x=', 'translate=', 'Translate a tweet'
21
25
  o.on 'w', 'with-retweets', 'Include retweets.'
22
26
  end
23
27
 
24
28
  def process
25
29
  super
26
30
  case
31
+ when opts.present?('translate') then translate_tweet(opts['translate'])
27
32
  when opts.present?('replies') then show_replies(user: opts['replies'])
28
33
  when opts.present?('likes') then show_likes(user: opts['likes'])
29
34
  when opts.present?('mentions') then show_mentions
@@ -0,0 +1,38 @@
1
+ module Pry::SendTweet::ReadTweets::TranslateActions
2
+ YANDEX_ENDPOINT = "https://translate.yandex.net/api/v1.5/tr.json/translate?" \
3
+ "key=%{key}&text=%{text}&lang=%{lang}"
4
+
5
+ def translate_tweet(tweet_url)
6
+ tweet = twitter.status(tweet_url)
7
+ uri_endpoint = __build_yandex_endpoint(tweet)
8
+ res = Net::HTTP.get_response(uri_endpoint)
9
+ case res
10
+ when Net::HTTPOK
11
+ tweet.attrs[:full_text] = JSON.parse(res.body)["text"][0]
12
+ render_tweets [tweet], title: "Translated tweet"
13
+ else
14
+ raise Pry::CommandError, "Bad response from Yandex (#{res.class})"
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ # @api private
21
+ def __build_yandex_endpoint(tweet)
22
+ URI.parse format(YANDEX_ENDPOINT,
23
+ lang: _pry_.config.twitter.yandex_lang || "en",
24
+ key: __yandex_key,
25
+ text: URI.encode_www_form_component(__read_tweet_body(tweet))
26
+ )
27
+ end
28
+
29
+ # @api private
30
+ def __yandex_key
31
+ k = _pry_.config.twitter.yandex_key
32
+ if !k || k.strip.empty?
33
+ raise Pry::CommandError,
34
+ "fatal: _pry_.config.twitter.yandex_key is nil, false or empty"
35
+ end
36
+ URI.encode_www_form_component(k)
37
+ end
38
+ end
@@ -41,10 +41,10 @@ class Pry::SendTweet::SendTweet < Pry::SendTweet::BaseCommand
41
41
  publish_time = (time_obj ? time_obj : Time.now + sleep_seconds)
42
42
  .getlocal
43
43
  .strftime(time_format)
44
- page bold("Tweet will be published at approximately #{publish_time}.")
44
+ page_ok bold("Tweet will be published at approximately #{publish_time}.")
45
45
  else
46
46
  tweet = create_tweet.call
47
- page tweet.url
47
+ page_ok tweet.url
48
48
  end
49
49
  end
50
50
 
@@ -69,8 +69,8 @@ class Pry::SendTweet::SendTweet < Pry::SendTweet::BaseCommand
69
69
 
70
70
  def prepend_username_to_reply!(tweet_url, file)
71
71
  tweet = twitter.status(tweet_url)
72
- mentions = tweet.user_mentions.map{|u| "@#{u.screen_name}"}.join(' ')
73
- file.write "@#{tweet.user.screen_name} #{mentions}"
72
+ mentions = [tweet.user.screen_name].concat tweet.user_mentions.map(&:screen_name)
73
+ file.write mentions.uniq(&:downcase).map{|u| "@#{u}" }.join(' ')
74
74
  end
75
75
 
76
76
  def replying_to_other_tweet?
@@ -51,7 +51,7 @@ module Pry::SendTweet::TwitterAction::FollowActions
51
51
  # @api private
52
52
  def join_usernames(users)
53
53
  users.map do |username|
54
- bold("@#{username}")
54
+ bold("@#{username}") + " ( https://twitter.com/#{username} )"
55
55
  end.join(', ')
56
56
  end
57
57
  end
@@ -1,11 +1,11 @@
1
1
  module Pry::SendTweet::TweetRenderer
2
2
  include Timeout
3
3
 
4
- def render_tweets(tweet_fetcher, title:, timeout: _pry_.config.twitter.refresh_interval)
4
+ def render_tweets(tweet_fetcher, title: nil, timeout: _pry_.config.twitter.refresh_interval)
5
5
  pid = nil
6
6
  timeout(__choose_render_interval(timeout)) {
7
7
  tweets = __fetch_tweets(tweet_fetcher)
8
- pid = __fork_pager(tweets)
8
+ pid = __fork_pager(title, tweets)
9
9
  Process.wait(pid)
10
10
  }
11
11
  rescue Interrupt
@@ -19,12 +19,12 @@ module Pry::SendTweet::TweetRenderer
19
19
 
20
20
  private
21
21
  # @api private
22
- def __fork_pager(tweets)
22
+ def __fork_pager(title, tweets)
23
23
  Kernel.fork do
24
24
  _pry_.pager.open do |pager|
25
25
  __trap_signal_in_fork(pager)
26
26
  tweets.empty? ? pager.write("No tweets to show.") :
27
- pager.write(__render_tweets("Twitter", tweets))
27
+ pager.write(__render_tweets(title || "Twitter", tweets))
28
28
 
29
29
  end
30
30
  end
@@ -38,7 +38,7 @@ module Pry::SendTweet::TweetRenderer
38
38
 
39
39
  # @api private
40
40
  def __render_tweet(tweet)
41
- contents = __get_tweet_contents(tweet)
41
+ contents = __read_tweet_body(tweet)
42
42
  TTY::Box.frame(height: box_height,
43
43
  width: box_width,
44
44
  title: {top_left: __render_tweet_title(tweet)}) do
@@ -58,11 +58,10 @@ module Pry::SendTweet::TweetRenderer
58
58
  end
59
59
 
60
60
  # @api private
61
- def __get_tweet_contents(tweet)
61
+ def __read_tweet_body(tweet)
62
62
  text = tweet.attrs[:full_text] ? tweet.attrs[:full_text] :
63
63
  tweet.full_text
64
- text = __tty_box_sanitize(CGI.unescapeHTML(text)).strip
65
- text.size == 0 || text !~ /[a-z0-9]{1,}/i ? "\n[ Outside ASCII charset ]\n" : text
64
+ CGI.unescapeHTML(text).strip
66
65
  end
67
66
 
68
67
  # @api private
@@ -95,7 +94,8 @@ module Pry::SendTweet::TweetRenderer
95
94
  trap('SIGINT') {
96
95
  if Pry::Pager::SystemPager === pager
97
96
  syspager = pager.send(:pager)
98
- Process.kill('SIGKILL', syspager.pid) rescue nil
97
+ pid = syspager.pid
98
+ Process.kill('SIGKILL', pid) rescue nil
99
99
  end
100
100
  Process.kill('SIGKILL', Process.pid)
101
101
  }
@@ -1,15 +1,16 @@
1
1
  module Pry::SendTweet::UserRenderer
2
2
  def render_user(user)
3
- body = <<-USER
4
- #{bold("Tweets")} #{user.tweets_count}
5
- #{bold("Followers")} #{user.followers_count}
6
- #{bold("Following")} #{user.friends_count}
7
- #{__tty_box_sanitize(user.description)}
8
- USER
3
+ title = "@#{user.screen_name} ( https://twitter.com/#{user.screen_name} )"
4
+ body = CGI.unescapeHTML <<-USER.each_line.map(&:strip).join("\n")
5
+ #{bold("Tweets")} #{user.tweets_count}
6
+ #{bold("Followers")} #{user.followers_count}
7
+ #{bold("Following")} #{user.friends_count}
8
+ #{user.description}
9
+ USER
9
10
  TTY::Box.frame(
10
- height: 8,
11
- width: 80,
12
- title: {top_left: "@#{user.screen_name} ( https://twitter.com/#{user.screen_name} )"}
11
+ height: box_height,
12
+ width: box_width,
13
+ title: {top_left: title}
13
14
  ) { body }.to_s
14
15
  end
15
16
  end
@@ -0,0 +1,74 @@
1
+ #
2
+ # A monkey patch module that implements unicode & emoji support in the gem
3
+ # 'tty-box' when using its `frame` method.
4
+ #
5
+ module Pry::SendTweet::TTYPatch
6
+ require 'tty-box'
7
+ require 'unicode/display_width'
8
+ require 'unicode/emoji'
9
+ # Create a frame
10
+ #
11
+ # @api public
12
+ def frame(top: nil, left: nil, width: 35, height: 3, align: :left, padding: 0,
13
+ title: {}, border: :light, style: {})
14
+ output = []
15
+ content = []
16
+ position = top && left
17
+
18
+ border = TTY::Box::Border.parse(border)
19
+ top_size = border.top? ? 1: 0
20
+ bottom_size = border.bottom? ? 1: 0
21
+ left_size = border.left? ? 1 : 0
22
+ right_size = border.right ? 1 : 0
23
+
24
+ if block_given?
25
+ content = format(yield, width, padding, align)
26
+ end
27
+
28
+ fg, bg = *extract_style(style)
29
+ border_fg, border_bg = *extract_style(style[:border] || {})
30
+
31
+ if border.top?
32
+ output << cursor.move_to(left, top) if position
33
+ output << top_border(title, width, border, style)
34
+ output << "\n" unless position
35
+ end
36
+
37
+ (height - top_size - bottom_size).times do |i|
38
+ output << cursor.move_to(left, top + i + top_size) if position
39
+ if border.left?
40
+ output << border_bg.(border_fg.(pipe_char(border.type)))
41
+ end
42
+
43
+ content_size = width - left_size - right_size
44
+ unless content[i].nil?
45
+ output << bg.(fg.(content[i]))
46
+ plain_content = Pry::Helpers::Text.strip_color(content[i])
47
+ content_size -= Unicode::DisplayWidth.of(plain_content, 1, {}, emoji: true)
48
+ end
49
+ if style[:fg] || style[:bg] || !position # something to color
50
+ output << bg.(fg.(' ' * content_size))
51
+ end
52
+
53
+ if border.right?
54
+ if position
55
+ output << cursor.move_to(left + width - right_size, top + i + top_size)
56
+ end
57
+ output << border_bg.(border_fg.(pipe_char(border.type)))
58
+ end
59
+ output << "\n" unless position
60
+ end
61
+
62
+ if border.bottom?
63
+ output << cursor.move_to(left, top + height - bottom_size) if position
64
+ output << bottom_border(title, width, border, style)
65
+ output << "\n" unless position
66
+ end
67
+ output.join
68
+ end
69
+ end
70
+
71
+ module TTY::Box
72
+ prepend Pry::SendTweet::TTYPatch
73
+ singleton_class.module_eval { prepend Pry::SendTweet::TTYPatch }
74
+ end
@@ -1,5 +1,5 @@
1
1
  class Pry
2
2
  module SendTweet
3
- VERSION = '0.10.1'
3
+ VERSION = '0.11.0'
4
4
  end
5
5
  end
@@ -13,5 +13,7 @@ Gem::Specification.new do |spec|
13
13
  spec.add_runtime_dependency "pry", "~> 0.12"
14
14
  spec.add_runtime_dependency "twitter", "~> 6.0"
15
15
  spec.add_runtime_dependency "time-lord", "~> 1.0"
16
- spec.add_runtime_dependency "tty-box"
16
+ spec.add_runtime_dependency "tty-box", "= 0.3.0"
17
+ spec.add_runtime_dependency "unicode-display_width", "~> 1.4"
18
+ spec.add_runtime_dependency "unicode-emoji", "~> 1.1"
17
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pry-send_tweet.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Gleeson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-14 00:00:00.000000000 Z
11
+ date: 2019-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -56,16 +56,44 @@ dependencies:
56
56
  name: tty-box
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 0.3.0
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - '='
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: 0.3.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: unicode-display_width
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.4'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: unicode-emoji
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.1'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.1'
69
97
  description: A Twitter client for the Pry REPL.
70
98
  email: trebor.g@protonmail.com
71
99
  executables: []
@@ -82,6 +110,7 @@ files:
82
110
  - lib/pry/send_tweet/commands/base_command.rb
83
111
  - lib/pry/send_tweet/commands/paging/paging_support.rb
84
112
  - lib/pry/send_tweet/commands/read_tweets.rb
113
+ - lib/pry/send_tweet/commands/read_tweets/translate_actions.rb
85
114
  - lib/pry/send_tweet/commands/send_tweet.rb
86
115
  - lib/pry/send_tweet/commands/twitter_action.rb
87
116
  - lib/pry/send_tweet/commands/twitter_action/delete_tweet_actions.rb
@@ -93,6 +122,7 @@ files:
93
122
  - lib/pry/send_tweet/commands/twitter_search.rb
94
123
  - lib/pry/send_tweet/renderers/tweet_renderer.rb
95
124
  - lib/pry/send_tweet/renderers/user_renderer.rb
125
+ - lib/pry/send_tweet/tty-box.rb
96
126
  - lib/pry/send_tweet/version.rb
97
127
  - pry-send_tweet.gemspec
98
128
  homepage: https://github.com/r-obert/pry-send_tweet.rb