jugyo-rubytter 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rubytter.rb +25 -24
- metadata +1 -1
data/lib/rubytter.rb
CHANGED
@@ -7,7 +7,8 @@ require 'rubytter/connection'
|
|
7
7
|
require 'rubytter/hash_extension'
|
8
8
|
|
9
9
|
class Rubytter
|
10
|
-
APP_NAME =
|
10
|
+
APP_NAME = 'Rubytter'
|
11
|
+
HOMEPAGE = 'http://github.com/jugyo/rubytter'
|
11
12
|
|
12
13
|
def initialize(login, password, options = {})
|
13
14
|
@login = login
|
@@ -67,49 +68,49 @@ class Rubytter
|
|
67
68
|
send_direct_message(params.merge({:user => user, :text => text}))
|
68
69
|
end
|
69
70
|
|
70
|
-
# TODO: define some alias for commonly used methods
|
71
|
-
|
72
71
|
def get(path, params = {})
|
73
72
|
path += '.json'
|
74
73
|
param_str = '?' + params.to_a.map{|i| i[0].to_s + '=' + CGI.escape(i[1]) }.join('&')
|
75
74
|
path = path + param_str unless param_str.empty?
|
76
75
|
req = prepare_request(Net::HTTP::Get.new(path))
|
77
|
-
|
76
|
+
res_body = @connection.start(@host) do |http|
|
78
77
|
http.request(req).body
|
79
78
|
end
|
80
|
-
|
81
|
-
return case json
|
82
|
-
when Array
|
83
|
-
json.map do |i|
|
84
|
-
if i.is_a?(Hash)
|
85
|
-
i.to_struct
|
86
|
-
else
|
87
|
-
i
|
88
|
-
end
|
89
|
-
end
|
90
|
-
when Hash
|
91
|
-
json.to_struct
|
92
|
-
else
|
93
|
-
json
|
94
|
-
end
|
79
|
+
json_to_struct(JSON.parse(res_body))
|
95
80
|
end
|
96
81
|
|
97
82
|
def post(path, params = {})
|
98
83
|
path += '.json'
|
99
84
|
param_str = params.to_a.map{|i| i[0].to_s + '=' + CGI.escape(i[1]) }.join('&')
|
100
85
|
req = prepare_request(Net::HTTP::Post.new(path))
|
101
|
-
@connection.start(@host) do |http|
|
86
|
+
res_body = @connection.start(@host) do |http|
|
102
87
|
http.request(req, param_str).body
|
103
88
|
end
|
89
|
+
json_to_struct(JSON.parse(res_body))
|
104
90
|
end
|
105
91
|
|
106
|
-
|
107
|
-
post(path, params)
|
108
|
-
end
|
92
|
+
alias delete post
|
109
93
|
|
110
94
|
def prepare_request(req)
|
111
|
-
req.add_field('User-Agent',
|
95
|
+
req.add_field('User-Agent', "#{APP_NAME} #{HOMEPAGE}")
|
112
96
|
req.basic_auth(@login, @password)
|
113
97
|
return req
|
114
98
|
end
|
99
|
+
|
100
|
+
def json_to_struct(json)
|
101
|
+
case json
|
102
|
+
when Array
|
103
|
+
json.map do |i|
|
104
|
+
if i.is_a?(Hash)
|
105
|
+
i.to_struct
|
106
|
+
else
|
107
|
+
i
|
108
|
+
end
|
109
|
+
end
|
110
|
+
when Hash
|
111
|
+
json.to_struct
|
112
|
+
else
|
113
|
+
json
|
114
|
+
end
|
115
|
+
end
|
115
116
|
end
|