adnruby 0.1 → 0.2

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 +143 -44
  2. metadata +2 -2
data/lib/adnruby.rb CHANGED
@@ -1,8 +1,34 @@
1
+ #
2
+ # ADNRuby - A simple and easy to use App.net Ruby library
3
+ #
4
+ # Copyright (c) 2012 Kishyr Ramdial
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+
1
25
  require 'net/https'
2
26
  require 'uri'
3
27
  require 'json'
4
28
 
5
29
  API = "alpha-api.app.net"
30
+ ADNHTTP = Net::HTTP.new(API, 443)
31
+ ADNHTTP.use_ssl = true
6
32
 
7
33
  module ADN
8
34
  def self.token=(token)
@@ -14,50 +40,124 @@ module ADN
14
40
  end
15
41
 
16
42
  class User
17
- def self.retrieve(user_id)
18
- ADN.get("/stream/0/users/#{user_id}")
43
+ attr_accessor :user_id
44
+ 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
+ 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|
51
+ self.instance_variable_set "@#{k}", v
52
+ end
53
+ end
19
54
  end
20
55
 
21
- def self.follow(user_id)
22
- ADN.post("/stream/0/users/#{user_id}/follow")
56
+ def details
57
+ if self.id
58
+ h = {}
59
+ self.instance_variables.each { |iv| h[iv.to_s.gsub(/[^a-zA-Z0-9_]/, '')] = self.instance_variable_get(iv) }
60
+ h
61
+ else
62
+ ADN::Users.retrieve(@user_id)
63
+ end
23
64
  end
24
65
 
25
- def self.unfollow(user_id)
26
- ADN.delete("/stream/0/users/#{user_id}/follow")
66
+
67
+ # Followers/Users
68
+
69
+ def follow(user)
70
+ user_id = user.is_a?(ADN::User) ? user.id : user
71
+ result = ADN.post("/stream/0/users/#{user_id}/follow")
72
+ result["data"] unless result.has_error?
27
73
  end
28
74
 
29
- def self.following(user_id)
30
- ADN.get("/stream/0/users/#{user_id}/following")
75
+ def unfollow(user)
76
+ user_id = user.is_a?(ADN::User) ? user.id : user
77
+ result = ADN.delete("/stream/0/users/#{user_id}/follow")
78
+ result["data"] unless result.has_error?
31
79
  end
32
80
 
33
- def self.followers(user_id)
34
- ADN.get("/stream/0/users/#{user_id}/followers")
81
+ def followers
82
+ result = ADN::Users.followers(@user_id)
83
+ result["data"] unless result.has_error?
84
+ end
85
+
86
+ def following
87
+ result = ADN::Users.following(@user_id)
88
+ result["data"] unless result.has_error?
89
+ end
90
+
91
+
92
+ # Mute
93
+
94
+ def mute(user)
95
+ user_id = user.is_a?(ADN::User) ? user.id : user
96
+ result = ADN.post("/stream/0/users/#{user_id}/mute")
97
+ result["data"] unless result.has_error?
98
+ end
99
+
100
+ def unmute(user)
101
+ user_id = user.is_a?(ADN::User) ? user.id : user
102
+ result = ADN.delete("/stream/0/users/#{user_id}/mute")
103
+ result["data"] unless result.has_error?
104
+ end
105
+
106
+ def mute_list
107
+ result = ADN.get("/stream/0/users/me/muted")
108
+ result["data"] unless result.has_error?
35
109
  end
36
110
 
37
- def self.mute(user_id)
38
- ADN.post("/stream/0/users/#{user_id}/mute")
111
+
112
+ # Posts
113
+
114
+ def posts(params = nil)
115
+ result = ADN::Post.by_user(@user_id, params)
116
+ result["data"] unless result.has_error?
117
+ end
118
+
119
+ def stream(params = nil)
120
+ result = ADN::Post.stream(params)
121
+ result["data"] unless result.has_error?
39
122
  end
40
123
 
41
- def self.unmute(user_id)
42
- ADN.delete("/stream/0/users/#{user_id}/mute")
124
+ def mentions(params = nil)
125
+ result = ADN::Post.mentioning_user(@user_id, params)
126
+ result["data"] unless result.has_error?
127
+ end
128
+
129
+ # Errors
130
+
131
+ def has_error?
132
+ self.id.nil?
43
133
  end
44
134
 
45
- def self.mute_list
46
- ADN.get("/stream/0/users/me/muted")
135
+ end
136
+
137
+
138
+ # Modules
139
+
140
+ module Users
141
+
142
+ def self.retrieve(user_id)
143
+ ADN.get("/stream/0/users/#{user_id}")
47
144
  end
48
145
 
49
- # def self.posts(user_id)
50
- # ADN::Post.by_user(user_id)
51
- # end
146
+ def self.by_id(user_id)
147
+ self.retrieve(user_id)
148
+ end
52
149
 
53
- # def self.mentions(user_id)
54
- # ADN::Post.mentioning_user(user_id)
55
- # end
150
+ def self.following(user_id)
151
+ ADN.get("/stream/0/users/#{user_id}/following")
152
+ end
56
153
 
154
+ def self.followers(user_id)
155
+ ADN.get("/stream/0/users/#{user_id}/followers")
156
+ end
57
157
 
58
158
  end
59
159
 
60
- class Post
160
+ module Post
61
161
  def self.new(params)
62
162
  ADN.post("/stream/0/posts", params)
63
163
  end
@@ -100,67 +200,66 @@ module ADN
100
200
 
101
201
  end
102
202
 
103
- class Stream
203
+ module Stream
104
204
  # Not Yet Implemented
105
205
  # https://github.com/appdotnet/api-spec/blob/master/resources/streams.md
106
206
  end
107
207
 
108
- class Subscription
208
+ module Subscription
109
209
  # Not Yet Implemented
110
210
  # https://github.com/appdotnet/api-spec/blob/master/resources/subscriptions.md
111
211
  end
112
212
 
113
- class Filter
213
+ module Filter
114
214
  # Not Yet Implemented
115
215
  # https://github.com/appdotnet/api-spec/blob/master/resources/filters.md
116
216
  end
117
217
 
118
- class Token
218
+ module Token
119
219
  def self.current
120
- ADN.get("/stream/0/token")
220
+ result = ADN.get("/stream/0/token")
221
+ result["data"] unless result.has_error?
121
222
  end
122
223
  end
123
224
 
124
- def self.get(url, params = nil)
125
- http = Net::HTTP.new(API, 443)
126
- http.use_ssl = true
127
225
 
226
+
227
+ private
228
+
229
+ def self.get(url, params = nil)
128
230
  get_url = params.nil? ? url : "#{url}?#{URI.encode_www_form(params)}"
129
231
  request = Net::HTTP::Get.new(get_url)
130
232
  request.add_field("Authorization", "Bearer #{ADN.token}")
131
- response = http.request(request)
233
+ response = ADNHTTP.request(request)
132
234
  return JSON.parse(response.body)
133
235
  end
134
236
 
135
237
  def self.post(url, params = nil)
136
- http = Net::HTTP.new(API, 443)
137
- http.use_ssl = true
138
-
139
238
  request = Net::HTTP::Post.new(url)
140
239
  request.set_form_data(params) if params
141
240
  request.add_field("Authorization", "Bearer #{ADN.token}")
142
- response = http.request(request)
241
+ response = ADNHTTP.request(request)
143
242
  return JSON.parse(response.body)
144
243
  end
145
244
 
146
245
  def self.put(url, params = nil)
147
- http = Net::HTTP.new(API, 443)
148
- http.use_ssl = true
149
-
150
246
  request = Net::HTTP::Put.new(url)
151
247
  request.set_form_data(params) if params
152
248
  request.add_field("Authorization", "Bearer #{ADN.token}")
153
- response = http.request(request)
249
+ response = ADNHTTP.request(request)
154
250
  return JSON.parse(response.body)
155
251
  end
156
252
 
157
253
  def self.delete(url, params = nil)
158
- http = Net::HTTP.new(API, 443)
159
- http.use_ssl = true
160
-
161
254
  request = Net::HTTP::Delete.new(url)
162
255
  request.add_field("Authorization", "Bearer #{ADN.token}")
163
- response = http.request(request)
256
+ response = ADNHTTP.request(request)
164
257
  return JSON.parse(response.body)
165
258
  end
166
259
  end
260
+
261
+ class Hash
262
+ def has_error?
263
+ self.has_key? "error"
264
+ end
265
+ end
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.1'
4
+ version: '0.2'
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-25 00:00:00.000000000 Z
12
+ date: 2012-08-26 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