trabajo-afectivo/app/models/channel/driver/twitter.rb

102 lines
2.2 KiB
Ruby
Raw Normal View History

2015-07-02 15:13:04 +00:00
# Copyright (C) 2012-2015 Zammad Foundation, http://zammad-foundation.org/
2015-08-28 00:53:14 +00:00
class Channel::Driver::Twitter
2015-07-02 15:13:04 +00:00
2015-08-28 01:02:21 +00:00
def fetch (_adapter_options, channel)
2015-07-02 15:13:04 +00:00
@channel = channel
@tweet = Tweet.new( @channel[:options][:auth] )
@sync = @channel[:options][:sync]
Rails.logger.debug 'twitter fetch started'
2015-07-02 15:13:04 +00:00
fetch_search
fetch_mentions
fetch_direct_messages
disconnect
Rails.logger.debug 'twitter fetch completed'
end
def send(article, _notification = false)
2015-08-28 00:53:14 +00:00
@channel = Channel.find_by( area: 'Twitter::Account', active: true )
2015-07-02 15:13:04 +00:00
@tweet = Tweet.new( @channel[:options][:auth] )
tweet = @tweet.from_article(article)
disconnect
tweet
end
def disconnect
@tweet.disconnect
end
private
def fetch_search
return if !@sync[:search]
2015-07-03 06:55:59 +00:00
return if @sync[:search].empty?
2015-07-02 15:13:04 +00:00
# search results
@sync[:search].each { |search|
result_type = search[:type] || 'mixed'
Rails.logger.debug " - searching for '#{search[:term]}'"
counter = 0
@tweet.client.search( search[:term], result_type: result_type ).collect { |tweet|
2015-07-02 15:13:04 +00:00
break if search[:limit] && search[:limit] <= counter
break if Ticket::Article.find_by( message_id: tweet.id.to_s )
@tweet.to_group( tweet, search[:group_id] )
2015-07-02 15:13:04 +00:00
counter += 1
}
}
end
def fetch_mentions
return if !@sync[:mentions]
2015-07-03 06:55:59 +00:00
return if @sync[:mentions].empty?
2015-07-02 15:13:04 +00:00
Rails.logger.debug ' - searching for mentions'
counter = 0
@tweet.client.mentions_timeline.each { |tweet|
break if @sync[:mentions][:limit] && @sync[:mentions][:limit] <= counter
break if Ticket::Article.find_by( message_id: tweet.id.to_s )
@tweet.to_group( tweet, @sync[:mentions][:group_id] )
2015-07-02 15:13:04 +00:00
counter += 1
}
end
def fetch_direct_messages
return if !@sync[:direct_messages]
2015-07-03 06:55:59 +00:00
return if @sync[:direct_messages].empty?
2015-07-02 15:13:04 +00:00
Rails.logger.debug ' - searching for direct_messages'
counter = 0
@tweet.client.direct_messages.each { |tweet|
break if @sync[:direct_messages][:limit] && @sync[:direct_messages][:limit] <= counter
break if Ticket::Article.find_by( message_id: tweet.id.to_s )
@tweet.to_group( tweet, @sync[:direct_messages][:group_id] )
2015-07-02 15:13:04 +00:00
counter += 1
}
end
end