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

103 lines
2.3 KiB
Ruby
Raw Normal View History

2015-07-02 15:13:04 +00:00
# Copyright (C) 2012-2015 Zammad Foundation, http://zammad-foundation.org/
class Channel::Twitter
def fetch (channel)
@channel = channel
@tweet = Tweet.new( @channel[:options][:auth] )
@sync = @channel[:options][:sync]
Rails.logger.debug "twitter fetch started"
fetch_search
fetch_mentions
fetch_direct_messages
disconnect
Rails.logger.debug 'twitter fetch completed'
end
def send(article, _notification = false)
@channel = Channel.find_by( area: 'Twitter::Inbound', active: true )
@tweet = Tweet.new( @channel[:options][:auth] )
@sync = @channel[:options][:sync]
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|
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] )
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] )
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] )
counter += 1
}
end
end