/
blip-twitter-api.rb
154 lines (117 loc) · 3.84 KB
/
blip-twitter-api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
require "rubygems"
require "sinatra"
$:.unshift "vendor/gems/httparty-0.5.0/lib/"
require "vendor/gems/httparty-0.5.0/lib/httparty"
$:.unshift "vendor/gems/bliptwitt-0.0.3/lib/"
require "vendor/gems/bliptwitt-0.0.3/lib/bliptwitt"
require "yajl"
require "yajl/json_gem"
set :port, 80
class Blip
include HTTParty
base_uri "api.blip.pl"
def initialize(username, password)
@auth = { :username => username, :password => password }
@headers = {
"User-Agent" => "Blip-Twitter Conduit",
"X-Blip-API" => "0.02",
"Accept" => "application/json"
}
end
def fetch(url)
options = { :basic_auth => @auth, :headers => @headers }
self.class.get(url, options)
end
def write(url, body)
options = { :basic_auth => @auth, :headers => @headers, :body => body}
self.class.post(url, options)
end
end
before do
@basic_auth = Rack::Auth::Basic::Request.new(request.env)
@blip = Blip.new(@basic_auth.credentials[0], @basic_auth.credentials[1]) if @basic_auth.provided? && @basic_auth.basic? && @basic_auth.credentials
end
get "/" do
%Q{
<html>
<head>
<title>Z Blipowego na Twitterowy</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<pre>
Ta wyrafinowa aplikacja tłumaczy z Blipowego na Twitterowy.
Testowane z Twitterrific, Spaz i Termtter.
Se podaj http://blip-twitter.heroku.com/ jako URL.
</pre>
</body>
</html>
}
end
post "/statuses/update.json" do
status = params["status"]
status = status.gsub(/^@@/, ">>").gsub(/^@/, ">")
posted = @blip.write("/updates", { :update => { :body => status } } )
if status =~ /^>>/
message = @blip.fetch("/private_messages/#{posted["id"]}?include=user,user[avatar],user[background],recipient")
else
message = @blip.fetch("/updates/#{posted["id"]}?include=user,user[avatar],user[background],recipient")
end
BlipTwitt.status(message).to_json
end
post "/direct_messages/new.json" do
status = ">>#{params["user"]} #{params["text"]}"
posted = @blip.write("/updates", { :update => { :body => status } } )
message = @blip.fetch("/private_messages/#{posted["id"]}?include=user,user[avatar],user[background],recipient")
BlipTwitt.status(message).to_json
end
get "/statuses/replies.json" do
directed_messages = @blip.fetch("/directed_messages?include=user,user[avatar],user[background],recipient")
timeline = []
directed_messages.each do |message|
timeline << BlipTwitt.status(message)
end
timeline.to_json
end
get "/direct_messages.json" do
private_messages = @blip.fetch("/private_messages?include=user,user[avatar],user[background],recipient")
timeline = []
private_messages.each do |message|
timeline << BlipTwitt.status(message)
end
timeline.to_json
end
get "/favorites.json" do
"[]"
end
get %r{/statuses/(home|friends)_timeline.json} do
if params[:since_id]
dashboard = @blip.fetch("/dashboard/since/#{params[:since_id]}?include=user,user[avatar],user[background],recipient")
else
dashboard = @blip.fetch("/dashboard?include=user,user[avatar],user[background],recipient")
end
timeline = []
dashboard.each do |message|
timeline << BlipTwitt.status(message)
end
timeline.to_json
end
get "/account/rate_limit_status.json" do
time = Time.now
rate = {
"remaining_hits" => 999,
"hourly_limit" => 999,
"reset_time" => time.utc,
"reset_time_in_seconds" => time.strftime("%y%m%d%H%M%S")
}
rate.to_json
end
get "/account/verify_credentials.json" do
BlipTwitt.user(@blip.fetch("/profile?include=background,avatar,current_status")).to_json
end
get "/users/show/:login.json" do
BlipTwitt.user(@blip.fetch("/users/#{params[:login]}?include=background,avatar,current_status")).to_json
end
get "/statuses/show/:id.json" do
BlipTwitt.status(@blip.fetch("/updates/#{params[:id]}?include=user,user[avatar],recipient")).to_json
end