Class: TwitterConverter

Inherits:
Converter show all
Includes:
Logging
Defined in:
lib/business/converter/twitter_converter.rb

Overview

Twitter Specification from Converter

Author:

  • Daniel Machado Fernandez

Version:

  • 1.0

Constant Summary

Constants included from Logging

Logging::KermitPFC

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Instance Method Details

#to_usmf(status) ⇒ USMF

Field to field parsing to become a tweet into a USMF message

Parameters:

  • status (String)

    the tweet previously retrieved

Returns:

  • (USMF)

    the resultant message



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/business/converter/twitter_converter.rb', line 22

def to_usmf status

	usmf = USMF.new
	user = User.new

	status = JSON.parse(status)
	if status.has_key? 'Error'
		logger.error("tweet malformed")
		raise "status malformed"
	end

	#Retrieving a status from Twitter
	usmf.service = "Twitter"
	usmf.id = status["id_str"]
	

	x = status["coordinates"]
	unless x==nil
		usmf.geo = x["coordinates"]
	end
	
	usmf.application = status["source"]
	

	x = status["place"]
	unless x == nil
		usmf.location = x["full_name"] + " , " + x["country"]
	end

	usmf.date = status["created_at"]
	usmf.text = status["text"]
	usmf.description = status["in_reply_to_status_id_str"]
	usmf.likes = status["retweet_count"]

	#Retrieving user
	x = status["user"]
	unless x == nil
		user.name = x["screen_name"]
		user.real_name = x["name"]
		user.id = x["id_str"]
		user.language = x["lang"]

		unless x["time_zone"] == nil and x["utc_offset"] == nil
			user.utc = x["time_zone"].to_s + " + " + x["utc_offset"].to_s
		end

		user.description = x["description"]
		user.avatar = x["profile_image_url_https"]
		user.location = x["location"]
		user.subscribers = x["followers_count"]
		user.subscriptions = x["friends_count"]
		user.postings = x["statuses_count"]
		user.profile = "https://twitter.com/#!/#{user.name}"
		user.website = x["url"]

		usmf.user = user
		usmf.source = "https://twitter.com/#{usmf.user.name}/status/#{usmf.id}"
	end
	

	usmf.to_users = []
	usmf.links = []

	#Retrieving entities

	entities = status["entities"]
	unless entities == nil
	
	#Retrieving URLs

		x = entities["urls"]
		unless x == nil
			x.each do |item|
				l = Link.new
				l.href = item["url"]
				
				usmf.links << l
			end
		end

	#Retrieving all media content

		x = entities["media"]
		unless x == nil
			x.each do |item|
				l = Link.new
				l.title = item["type"]
				l.thumbnail = item["media_url"]
				l.href = item["url"]
				
				usmf.links << l
			end
		end

		#Retrieving hashtags

		x = entities["hashtags"]
		unless x == nil

			usmf.keywords = ""
			x.each do |h| 

				usmf.keywords += h["text"] + ", "

			end

		end

		#Retrieving mentions

		x = entities["user_mentions"]
		unless x == nil
			x.each do |item|
				tu = ToUser.new

				tu.name = item["screen_name"]
				tu.id = item["id_str"]

				if item["id_str"] == status["in_reply_to_user_id_str"]
					tu.service = "reply"
				else
					tu.service = "mention"
				end
				unless status["in_reply_to_status_id_str"] == nil
					tu.title = status["in_reply_to_status_id_str"]
					tu.href = "https://twitter.com/#{tu.name}/status/#{tu.title}"
				end

				usmf.to_users << tu
			end
		end

	end

	usmf

end