adnruby 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/adnruby.rb +148 -79
  2. metadata +2 -2
data/lib/adnruby.rb CHANGED
@@ -25,6 +25,7 @@
25
25
  require 'net/https'
26
26
  require 'uri'
27
27
  require 'json'
28
+ require 'date'
28
29
 
29
30
  API = "alpha-api.app.net"
30
31
  ADNHTTP = Net::HTTP.new(API, 443)
@@ -43,13 +44,20 @@ module ADN
43
44
  attr_accessor :user_id
44
45
  attr_accessor :avatar_image, :counts, :cover_image, :created_at, :description, :follows_you, :id, :is_follower, :is_following, :is_muted, :locale, :name, :timezone, :type, :username, :you_follow, :you_muted
45
46
 
46
- def initialize(user_id)
47
- @user_id = user_id
48
- details = self.details
49
- if details.has_key? "data"
50
- details["data"].each do |k, v|
47
+ def initialize(user)
48
+ if user.is_a? Hash
49
+ user.each do |k, v|
51
50
  self.instance_variable_set "@#{k}", v
52
51
  end
52
+ @user_id = self.id
53
+ else
54
+ @user_id = user
55
+ details = self.details
56
+ if details.has_key? "data"
57
+ details["data"].each do |k, v|
58
+ self.instance_variable_set "@#{k}", v
59
+ end
60
+ end
53
61
  end
54
62
  end
55
63
 
@@ -59,7 +67,7 @@ module ADN
59
67
  self.instance_variables.each { |iv| h[iv.to_s.gsub(/[^a-zA-Z0-9_]/, '')] = self.instance_variable_get(iv) }
60
68
  h
61
69
  else
62
- ADN::Users.retrieve(@user_id)
70
+ ADN::API::User.retrieve(@user_id)
63
71
  end
64
72
  end
65
73
 
@@ -69,23 +77,23 @@ module ADN
69
77
  def follow(user)
70
78
  user_id = user.is_a?(ADN::User) ? user.id : user
71
79
  result = ADN.post("/stream/0/users/#{user_id}/follow")
72
- result["data"] unless result.has_error?
80
+ User.new(result["data"]) unless result.has_error?
73
81
  end
74
82
 
75
83
  def unfollow(user)
76
84
  user_id = user.is_a?(ADN::User) ? user.id : user
77
85
  result = ADN.delete("/stream/0/users/#{user_id}/follow")
78
- result["data"] unless result.has_error?
86
+ User.new(result["data"]) unless result.has_error?
79
87
  end
80
88
 
81
89
  def followers
82
- result = ADN::Users.followers(@user_id)
83
- result["data"] unless result.has_error?
90
+ result = ADN::API::User.followers(@user_id)
91
+ result["data"].collect { |u| User.new(u) } unless result.has_error?
84
92
  end
85
93
 
86
94
  def following
87
- result = ADN::Users.following(@user_id)
88
- result["data"] unless result.has_error?
95
+ result = ADN::API::User.following(@user_id)
96
+ result["data"].collect { |u| User.new(u) } unless result.has_error?
89
97
  end
90
98
 
91
99
 
@@ -94,36 +102,36 @@ module ADN
94
102
  def mute(user)
95
103
  user_id = user.is_a?(ADN::User) ? user.id : user
96
104
  result = ADN.post("/stream/0/users/#{user_id}/mute")
97
- result["data"] unless result.has_error?
105
+ User.new(result["data"]) unless result.has_error?
98
106
  end
99
107
 
100
108
  def unmute(user)
101
109
  user_id = user.is_a?(ADN::User) ? user.id : user
102
110
  result = ADN.delete("/stream/0/users/#{user_id}/mute")
103
- result["data"] unless result.has_error?
111
+ User.new(result["data"]) unless result.has_error?
104
112
  end
105
113
 
106
114
  def mute_list
107
115
  result = ADN.get("/stream/0/users/me/muted")
108
- result["data"] unless result.has_error?
116
+ result["data"].collect { |u| User.new(u) } unless result.has_error?
109
117
  end
110
118
 
111
119
 
112
120
  # Posts
113
121
 
114
122
  def posts(params = nil)
115
- result = ADN::Post.by_user(@user_id, params)
116
- result["data"] unless result.has_error?
123
+ result = ADN::API::Post.by_user(@user_id, params)
124
+ result["data"].collect { |p| Post.new(p) } unless result.has_error?
117
125
  end
118
126
 
119
127
  def stream(params = nil)
120
- result = ADN::Post.stream(params)
121
- result["data"] unless result.has_error?
128
+ result = ADN::API::Post.stream(params)
129
+ result["data"].collect { |p| Post.new(p) } unless result.has_error?
122
130
  end
123
131
 
124
132
  def mentions(params = nil)
125
- result = ADN::Post.mentioning_user(@user_id, params)
126
- result["data"] unless result.has_error?
133
+ result = ADN::API::Post.mentioning_user(@user_id, params)
134
+ result["data"].collect { |p| Post.new(p) } unless result.has_error?
127
135
  end
128
136
 
129
137
  # Errors
@@ -134,94 +142,155 @@ module ADN
134
142
 
135
143
  end
136
144
 
145
+ class Post
146
+ attr_accessor :post_id, :created_at, :entities, :html, :id, :num_replies, :reply_to, :source, :text, :thread_id, :user
137
147
 
138
- # Modules
139
-
140
- module Users
141
-
142
- def self.retrieve(user_id)
143
- ADN.get("/stream/0/users/#{user_id}")
148
+ def self.send(params)
149
+ result = ADN::API::Post.new(params)
150
+ Post.new(result["data"]) unless result.has_error?
144
151
  end
145
152
 
146
- def self.by_id(user_id)
147
- self.retrieve(user_id)
153
+ def initialize(raw_post)
154
+ if raw_post.is_a? Hash
155
+ raw_post.each do |k, v|
156
+ self.instance_variable_set "@#{k}", v
157
+ end
158
+ @post_id = @id
159
+ else
160
+ @post_id = raw_post
161
+ details = self.details
162
+ if details.has_key? "data"
163
+ details["data"].each do |k, v|
164
+ self.instance_variable_set "@#{k}", v
165
+ end
166
+ end
167
+ end
148
168
  end
149
169
 
150
- def self.following(user_id)
151
- ADN.get("/stream/0/users/#{user_id}/following")
170
+ def details
171
+ if self.id
172
+ h = {}
173
+ self.instance_variables.each { |iv| h[iv.to_s.gsub(/[^a-zA-Z0-9_]/, '')] = self.instance_variable_get(iv) }
174
+ h
175
+ else
176
+ ADN::API::Post.by_id(@post_id)
177
+ end
152
178
  end
153
179
 
154
- def self.followers(user_id)
155
- ADN.get("/stream/0/users/#{user_id}/followers")
180
+ def created_at
181
+ DateTime.parse(@created_at)
156
182
  end
157
183
 
158
- end
159
-
160
- module Post
161
- def self.new(params)
162
- ADN.post("/stream/0/posts", params)
184
+ def user
185
+ ADN::User.new @user
163
186
  end
164
187
 
165
- def self.retrieve(post_id)
166
- ADN.get("/stream/0/posts/#{post_id}")
188
+ def reply_to_post
189
+ result = ADN::API::Post.by_id @reply_to
190
+ Post.new(result["data"]) unless result.has_error?
167
191
  end
168
192
 
169
- def self.by_id(post_id)
170
- self.retrieve(post_id)
193
+ def replies(params = nil)
194
+ result = ADN::API::Post.replies(@id, params)
195
+ result["data"].collect { |p| Post.new(p) } unless result.has_error?
171
196
  end
172
197
 
173
- def self.delete(post_id)
174
- ADN.delete("/stream/0/posts/#{post_id}")
198
+ def delete
199
+ result = ADN::API::Post.delete(@id)
200
+ Post.new(result["data"]) unless result.has_error?
175
201
  end
176
202
 
177
- def self.replies(post_id, params = nil)
178
- ADN.get("/stream/0/posts/#{post_id}/replies", params)
179
- end
203
+ end
180
204
 
181
- def self.by_user(user_id, params = nil)
182
- ADN.get("/stream/0/users/#{user_id}/posts", params)
183
- end
184
205
 
185
- def self.mentioning_user(user_id, params = nil)
186
- ADN.get("/stream/0/users/#{user_id}/mentions", params)
187
- end
206
+ # API Modules
188
207
 
189
- def self.stream(params = nil)
190
- ADN.get("/stream/0/posts/stream", params)
191
- end
208
+ module API
209
+ module User
192
210
 
193
- def self.global_stream(params = nil)
194
- ADN.get("/stream/0/posts/stream/global", params)
195
- end
211
+ def self.retrieve(user_id)
212
+ ADN.get("/stream/0/users/#{user_id}")
213
+ end
214
+
215
+ def self.by_id(user_id)
216
+ self.retrieve(user_id)
217
+ end
218
+
219
+ def self.following(user_id)
220
+ ADN.get("/stream/0/users/#{user_id}/following")
221
+ end
222
+
223
+ def self.followers(user_id)
224
+ ADN.get("/stream/0/users/#{user_id}/followers")
225
+ end
196
226
 
197
- def self.by_hashtag(hashtag, params = nil)
198
- ADN.get("/stream/0/posts/tag/#{hashtag}", params)
199
227
  end
200
228
 
201
- end
229
+ module Post
230
+ def self.new(params)
231
+ ADN.post("/stream/0/posts", params)
232
+ end
202
233
 
203
- module Stream
204
- # Not Yet Implemented
205
- # https://github.com/appdotnet/api-spec/blob/master/resources/streams.md
206
- end
234
+ def self.retrieve(post_id)
235
+ ADN.get("/stream/0/posts/#{post_id}")
236
+ end
207
237
 
208
- module Subscription
209
- # Not Yet Implemented
210
- # https://github.com/appdotnet/api-spec/blob/master/resources/subscriptions.md
211
- end
238
+ def self.by_id(post_id)
239
+ self.retrieve(post_id)
240
+ end
212
241
 
213
- module Filter
214
- # Not Yet Implemented
215
- # https://github.com/appdotnet/api-spec/blob/master/resources/filters.md
216
- end
242
+ def self.delete(post_id)
243
+ ADN.delete("/stream/0/posts/#{post_id}")
244
+ end
245
+
246
+ def self.replies(post_id, params = nil)
247
+ ADN.get("/stream/0/posts/#{post_id}/replies", params)
248
+ end
249
+
250
+ def self.by_user(user_id, params = nil)
251
+ ADN.get("/stream/0/users/#{user_id}/posts", params)
252
+ end
253
+
254
+ def self.mentioning_user(user_id, params = nil)
255
+ ADN.get("/stream/0/users/#{user_id}/mentions", params)
256
+ end
257
+
258
+ def self.stream(params = nil)
259
+ ADN.get("/stream/0/posts/stream", params)
260
+ end
261
+
262
+ def self.global_stream(params = nil)
263
+ ADN.get("/stream/0/posts/stream/global", params)
264
+ end
265
+
266
+ def self.by_hashtag(hashtag, params = nil)
267
+ ADN.get("/stream/0/posts/tag/#{hashtag}", params)
268
+ end
217
269
 
218
- module Token
219
- def self.current
220
- result = ADN.get("/stream/0/token")
221
- result["data"] unless result.has_error?
222
270
  end
223
- end
224
271
 
272
+ module Stream
273
+ # Not Yet Implemented
274
+ # https://github.com/appdotnet/api-spec/blob/master/resources/streams.md
275
+ end
276
+
277
+ module Subscription
278
+ # Not Yet Implemented
279
+ # https://github.com/appdotnet/api-spec/blob/master/resources/subscriptions.md
280
+ end
281
+
282
+ module Filter
283
+ # Not Yet Implemented
284
+ # https://github.com/appdotnet/api-spec/blob/master/resources/filters.md
285
+ end
286
+
287
+ module Token
288
+ def self.current
289
+ result = ADN.get("/stream/0/token")
290
+ result["data"] unless result.has_error?
291
+ end
292
+ end
293
+ end
225
294
 
226
295
 
227
296
  private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adnruby
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-26 00:00:00.000000000 Z
12
+ date: 2012-08-27 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A simple and easy to use library to interact with App.net's API
15
15
  email: kishyr@gmail.com