Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik authored Feb 12, 2020
2 parents 9603adb + b6dc83a commit 51444e9
Show file tree
Hide file tree
Showing 68 changed files with 1,086 additions and 71 deletions.
1 change: 1 addition & 0 deletions .jrubyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
debug.fullTrace=true
8 changes: 4 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ AllCops:
Layout/AccessModifierIndentation:
EnforcedStyle: outdent

Layout/LineLength:
AllowURI: true
Enabled: false

Layout/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space

Expand All @@ -18,10 +22,6 @@ Metrics/BlockLength:
Metrics/BlockNesting:
Max: 2

Metrics/LineLength:
AllowURI: true
Enabled: false

Metrics/MethodLength:
CountComments: false
Max: 10
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ cache: bundler
language: ruby

rvm:
- 2.3
- 2.4
- 2.5
- jruby-9.1.9.0
- 2.6
- 2.7
- jruby-head
- ruby-head

Expand Down
21 changes: 16 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
6.2.0
7.0.0
------
* [Add `Twitter::DirectMessageEvent`](https://github.com/sferik/twitter/commit/38f6aaa482dcd5f4982abd811dbe6e21e36c2ae8) ([@FabienChaynes](https://twitter.com/FabienChaynes))
* [Create `Twitter::DirectMessageEvent` with media](https://github.com/sferik/twitter/commit/21478530ec6f8a798717a5ab8b197b895db3bc28) ([@FabienChaynes](https://twitter.com/FabienChaynes))
* [Support for closing `Twitter::Streaming::Connection`](https://github.com/sferik/twitter/commit/89e3543aa06e00eeab7eaf5bdd3a33a6112356b1) ([@okkez](https://twitter.com/okkez))
* [Add `Twitter::REST:Client#create_direct_message_event`](https://github.com/sferik/twitter/commit/b45d545c7ebfd28e4d908037dd3fde04e3c307cf) ([@cyu](https://twitter.com/cyu))
* [Update all direct message methods to return `Twitter::DirectMessageEvent`](https://github.com/sferik/twitter/commit/0833471366a33657cd2920850e3928db010eecab) ([@flikglick](https://twitter.com/flikglick))
* [Correctly handle different `Twitter::Error::AlreadyRetweeted` error messages](https://github.com/sferik/twitter/commit/c9bf100eedc89aee43689c17f160025e0f40cfb4) ([@knu](https://twitter.com/knu))
* [Fix proxy setting sample](https://github.com/sferik/twitter/commit/91c037cfb26ac0c9d3099cdeec42d65fe8716b78) ([@nicklegr](https://twitter.com/nicklegr))
* [Add Active Support `presence` methods on `Twitter::NullObject`](https://github.com/sferik/twitter/commit/07a5d7b60f335aefd583dae85ba959235bc9f55f) ([@davebrace](https://twitter.com/davebrace))
* [Upload GIFs over 5MB in chunks](https://github.com/sferik/twitter/commit/ba6a3062782b37cf83b9813c5128c87d4971ab9c) ([@wild_dmitry](https://twitter.com/wild_dmitry))
* [Track rate limit when searching tweets](https://github.com/sferik/twitter/commit/067e751d58b7bb0bfbe8a5531a4288f4c966b301) ([@dsalahutdinov1](https://twitter.com/dsalahutdinov1))
* [Add `quote_count` and `reply_count` attributes to `Twitter::Tweet`](https://github.com/sferik/twitter/commit/844818cad07ce490ccb9d8542ebb6b4fc7a61cb4)
* [Drop support for Ruby 2.0, 2.1, and 2.2](https://github.com/sferik/twitter/commit/95861ec83e582c2a88499b97d9d5388a96d0abf0)


6.2.0
------
Not yet documented, sorry. For now, please use git to compare tags:
https://github.com/sferik/twitter/compare/v6.1.0...v6.2.0

6.1.0
------

Not yet documented, sorry. For now, please use git to compare tags:
https://github.com/sferik/twitter/compare/v6.0.0...v6.1.0


6.0.0
------

* [Drop support for Ruby 1.8.7 and Ruby 1.9.3](https://github.com/sferik/twitter/commit/27980f45fb357e34b86e46cb9134d86ed29b3ce3)

5.16.0
------

* [Add Twitter::Trend#tweet_volume](https://github.com/sferik/twitter/commit/e797b62e5e6a768e8aeafde6186e1f5310e6bfc6)
* [Add new settings to Twitter::Settings](https://github.com/sferik/twitter/commit/d047ce00034d26a99927076c28679ce08fd69308)
* [Fix `Version.to_a`](https://github.com/sferik/twitter/commit/0cd119abf64e6c2b7fd861b0df65b7cb41d892b4)
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ group :development do
end

group :test do
gem 'coveralls'
gem 'coveralls', '>= 0.8.23'
gem 'rspec', '>= 2.14'
gem 'rubocop', '>= 0.46'
gem 'simplecov', '>= 0.9'
gem 'simplecov', '>= 0.16'
gem 'timecop'
gem 'webmock'
gem 'yardstick'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ command:
This library aims to support and is [tested against][travis] the following Ruby
versions:

* Ruby 2.3
* Ruby 2.4
* Ruby 2.5
* JRuby 9.1.9.0
* Ruby 2.6
* Ruby 2.7

If something doesn't work on one of these versions, it's a bug.

Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ end

require 'yardstick/rake/verify'
Yardstick::Rake::Verify.new do |verify|
verify.threshold = 58.7
verify.threshold = 58.2
end

task default: %i[spec rubocop verify_measurements]
16 changes: 16 additions & 0 deletions examples/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ You can use this client to make any Twitter REST API request. For example:
client.update("I'm tweeting with @gem!")
```

## Premium Search API

```ruby
client = Twitter::REST::Client.new do |config|
config.consumer_key = "YOUR_CONSUMER_KEY"
config.consumer_secret = "YOUR_CONSUMER_SECRET"
config.dev_environment = "YOUR_DEV_ENVIRONMENT"
end
```

You can use this client to make a REST 30-Day or Fullarchive Search API request. For example:

```ruby
client.premium_search("#ruby", { maxResults: 100 }, { product: 'fullarchive' })
```

## Streaming Clients

Streaming clients are initialized just like single-user authenticated REST
Expand Down
5 changes: 5 additions & 0 deletions lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require 'twitter/rest/client'
require 'twitter/saved_search'
require 'twitter/search_results'
require 'twitter/premium_search_results'
require 'twitter/settings'
require 'twitter/size'
require 'twitter/source_user'
Expand All @@ -29,3 +30,7 @@
require 'twitter/trend'
require 'twitter/tweet'
require 'twitter/user'
require 'twitter/direct_messages/welcome_message'
require 'twitter/direct_messages/welcome_message_rule'
require 'twitter/direct_messages/welcome_message_rule_wrapper'
require 'twitter/direct_messages/welcome_message_wrapper'
2 changes: 1 addition & 1 deletion lib/twitter/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Twitter
class Client
include Twitter::Utils
attr_accessor :access_token, :access_token_secret, :consumer_key, :consumer_secret, :proxy, :timeouts
attr_accessor :access_token, :access_token_secret, :consumer_key, :consumer_secret, :proxy, :timeouts, :dev_environment
attr_writer :user_agent

# Initializes a new Client object
Expand Down
1 change: 1 addition & 0 deletions lib/twitter/creatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Creatable
def created_at
time = @attrs[:created_at]
return if time.nil?

time = Time.parse(time) unless time.is_a?(Time)
time.utc
end
Expand Down
1 change: 1 addition & 0 deletions lib/twitter/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def next_cursor
def last?
return false if next_cursor.is_a?(String)
return true if next_cursor.nil?

next_cursor.zero?
end

Expand Down
17 changes: 17 additions & 0 deletions lib/twitter/direct_messages/welcome_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'twitter/creatable'
require 'twitter/entities'
require 'twitter/identity'

module Twitter
module DirectMessages
class WelcomeMessage < Twitter::Identity
include Twitter::Creatable
include Twitter::Entities
# @return [String]
attr_reader :text
# @return [String]
attr_reader :name
alias full_text text
end
end
end
12 changes: 12 additions & 0 deletions lib/twitter/direct_messages/welcome_message_rule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'twitter/creatable'
require 'twitter/identity'

module Twitter
module DirectMessages
class WelcomeMessageRule < Twitter::Identity
include Twitter::Creatable
# @return [Integer]
attr_reader :welcome_message_id
end
end
end
35 changes: 35 additions & 0 deletions lib/twitter/direct_messages/welcome_message_rule_wrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'twitter/creatable'
require 'twitter/identity'

module Twitter
module DirectMessages
class WelcomeMessageRuleWrapper < Twitter::Identity
attr_reader :created_timestamp

object_attr_reader 'DirectMessages::WelcomeMessageRule', :welcome_message_rule

def initialize(attrs)
attrs = read_from_response(attrs)

attrs[:welcome_message_rule] = build_welcome_message_rule(attrs)
super
end

private

# @return [Hash] Normalized hash of attrs
def read_from_response(attrs)
return attrs[:welcome_message_rule] unless attrs[:welcome_message_rule].nil?
attrs
end

def build_welcome_message_rule(attrs)
{
id: attrs[:id].to_i,
created_at: Time.at(attrs[:created_timestamp].to_i / 1000.0),
welcome_message_id: attrs[:welcome_message_id].to_i,
}
end
end
end
end
41 changes: 41 additions & 0 deletions lib/twitter/direct_messages/welcome_message_wrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'twitter/creatable'
require 'twitter/identity'

module Twitter
module DirectMessages
class WelcomeMessageWrapper < Twitter::Identity
attr_reader :created_timestamp

object_attr_reader 'DirectMessages::WelcomeMessage', :welcome_message

def initialize(attrs)
attrs = read_from_response(attrs)
text = attrs.dig(:message_data, :text)
urls = attrs.dig(:message_data, :entities, :urls)

text.gsub!(urls[0][:url], urls[0][:expanded_url]) if urls.any?

attrs[:welcome_message] = build_welcome_message(attrs, text)
super
end

private

# @return [Hash] Normalized hash of attrs
def read_from_response(attrs)
return attrs[:welcome_message] unless attrs[:welcome_message].nil?
attrs
end

def build_welcome_message(attrs, text)
{
id: attrs[:id].to_i,
created_at: Time.at(attrs[:created_timestamp].to_i / 1000.0),
text: text,
name: attrs[:name],
entities: attrs.dig(:message_data, :entities),
}
end
end
end
end
5 changes: 3 additions & 2 deletions lib/twitter/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ module Enumerable
include ::Enumerable

# @return [Enumerator]
def each(start = 0)
def each(start = 0, &block)
return to_enum(:each, start) unless block_given?

Array(@collection[start..-1]).each do |element|
yield(element)
end
unless finished?
start = [@collection.size, start].max
fetch_next_page
each(start, &Proc.new)
each(start, &block)
end
self
end
Expand Down
35 changes: 34 additions & 1 deletion lib/twitter/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ class Error < StandardError
# Raised when Twitter returns the HTTP status code 504
GatewayTimeout = Class.new(ServerError)

# Raised when Twitter returns a media related error
MediaError = Class.new(self)

# Raised when Twitter returns an InvalidMedia error
InvalidMedia = Class.new(MediaError)

# Raised when Twitter returns a media InternalError error
MediaInternalError = Class.new(MediaError)

# Raised when Twitter returns an UnsupportedMedia error
UnsupportedMedia = Class.new(MediaError)

# Raised when an operation subject to timeout takes too long
TimeoutError = Class.new(self)

ERRORS = {
400 => Twitter::Error::BadRequest,
401 => Twitter::Error::Unauthorized,
Expand Down Expand Up @@ -90,7 +105,13 @@ class Error < StandardError
end
end

# If error code is missing see https://dev.twitter.com/overview/api/response-codes
MEDIA_ERRORS = {
'InternalError' => Twitter::Error::MediaInternalError,
'InvalidMedia' => Twitter::Error::InvalidMedia,
'UnsupportedMedia' => Twitter::Error::UnsupportedMedia,
}.freeze

# If error code is missing see https://developer.twitter.com/en/docs/basics/response-codes
module Code
AUTHENTICATION_PROBLEM = 32
RESOURCE_NOT_FOUND = 34
Expand Down Expand Up @@ -131,6 +152,18 @@ def from_response(body, headers)
new(message, headers, code)
end

# Create a new error from a media error hash
#
# @param error [Hash]
# @param headers [Hash]
# @return [Twitter::MediaError]
def from_processing_response(error, headers)
klass = MEDIA_ERRORS[error[:name]] || self
message = error[:message]
code = error[:code]
klass.new(message, headers, code)
end

private

def parse_error(body)
Expand Down
2 changes: 2 additions & 0 deletions lib/twitter/null_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ def respond_to?(*)

def instance_of?(klass)
raise(TypeError, 'class or module required') unless klass.is_a?(Class)

self.class == klass
end

def kind_of?(mod)
raise(TypeError, 'class or module required') unless mod.is_a?(Module)

self.class.ancestors.include?(mod)
end

Expand Down
Loading

0 comments on commit 51444e9

Please sign in to comment.