Improved error handling.

This commit is contained in:
Martin Edenhofer 2012-06-20 11:40:22 +02:00
parent 08736f8556
commit 6e9f9f4eee

View file

@ -15,10 +15,10 @@ class Channel::Twitter2
def fetch (channel) def fetch (channel)
puts "fetching tweets (oauth_token#{channel[:options][:oauth_token]})" puts "fetching tweets (oauth_token#{channel[:options][:oauth_token]})"
@client = connect(channel)
# search results # search results
if channel[:options][:search] if channel[:options][:search]
@client = connect(channel)
channel[:options][:search].each { |search| channel[:options][:search].each { |search|
puts " - searching for #{search[:item]}" puts " - searching for #{search[:item]}"
tweets = @client.search( search[:item] ) tweets = @client.search( search[:item] )
@ -29,6 +29,7 @@ class Channel::Twitter2
# mentions # mentions
if channel[:options][:mentions] if channel[:options][:mentions]
@client = connect(channel)
puts " - searching for mentions" puts " - searching for mentions"
tweets = @client.mentions tweets = @client.mentions
@article_type = 'twitter status' @article_type = 'twitter status'
@ -37,6 +38,7 @@ class Channel::Twitter2
# direct messages # direct messages
if channel[:options][:direct_messages] if channel[:options][:direct_messages]
@client = connect(channel)
puts " - searching for direct_messages" puts " - searching for direct_messages"
tweets = @client.direct_messages tweets = @client.direct_messages
@article_type = 'twitter direct-message' @article_type = 'twitter direct-message'
@ -105,6 +107,7 @@ class Channel::Twitter2
# create stuff # create stuff
user = fetch_user_create(tweet, sender) user = fetch_user_create(tweet, sender)
if !ticket if !ticket
puts 'create new ticket...'
ticket = fetch_ticket_create(user, tweet, sender, channel, group) ticket = fetch_ticket_create(user, tweet, sender, channel, group)
end end
article = fetch_article_create(user, ticket, tweet, sender) article = fetch_article_create(user, ticket, tweet, sender)
@ -136,6 +139,7 @@ class Channel::Twitter2
:roles => roles, :roles => roles,
:created_by_id => 1 :created_by_id => 1
) )
puts 'autentication create...'
authentication = Authorization.create( authentication = Authorization.create(
:uid => sender.id, :uid => sender.id,
:username => sender.screen_name, :username => sender.screen_name,
@ -164,27 +168,43 @@ class Channel::Twitter2
return article.ticket return article.ticket
end end
end end
# find if record already exists # find if record already exists
article = Ticket::Article.where( :message_id => tweet.id.to_s ).first article = Ticket::Article.where( :message_id => tweet.id.to_s ).first
if article if article
return article.ticket return article.ticket
end end
ticket = nil ticket = nil
if @article_type == 'twitter direct-message' if @article_type == 'twitter direct-message'
ticket = Ticket.where( :customer_id => user.id ).first ticket = Ticket.where( :customer_id => user.id ).first
end end
if !ticket if !ticket
group = Group.where( :name => group ).first
group_id = 1
if group
group_id = group.id
end
state = Ticket::State.where( :name => 'new' ).first
state_id = 1
if state
state_id = state.id
end
priority = Ticket::Priority.where( :name => '2 normal' ).first
priority_id = 1
if priority
priority_id = priority.id
end
ticket = Ticket.create( ticket = Ticket.create(
:group_id => Group.where( :name => group ).first.id, :group_id => group_id,
:customer_id => user.id, :customer_id => user.id,
:title => tweet.text[0,40], :title => tweet.text[0,40],
:ticket_state_id => Ticket::State.where( :name => 'new' ).first.id, :ticket_state_id => state_id,
:ticket_priority_id => Ticket::Priority.where( :name => '2 normal' ).first.id, :ticket_priority_id => priority_id,
:created_by_id => user.id :created_by_id => user.id
) )
end end
return ticket return ticket
end end