Class: FriendFeed::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/friendfeed/v1.rb,
lib/friendfeed/unofficial.rb,
bin/tw2ff

Overview

Client library for FriendFeed API.

Constant Summary collapse

API_URI =

Official API

URI.parse("https://friendfeed.com/api/")
ROOT_URI =

Unofficial API

URI.parse("https://friendfeed.com/")
LOGIN_URI =
ROOT_URI + "/account/login?v=2"
EDIT_GROUP_URI =
ROOT_URI + '/a/editprofile'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nicknameObject (readonly)

Returns the value of attribute nickname.



19
20
21
# File 'lib/friendfeed/v1.rb', line 19

def nickname
  @nickname
end

#remote_keyObject (readonly)

Returns the value of attribute remote_key.



53
54
55
# File 'lib/friendfeed/v1.rb', line 53

def remote_key
  @remote_key
end

Instance Method Details

#add_blog(id, url, options = nil) ⇒ Object

Adds a blog feed to the authenticated user, a group or an imaginary friend specified by a unique ID. Specify ‘multiauth’

> ‘on’ when the blog has multiple authors, and ‘author’ =>

‘(name)’ to limit entries to those written by a specific author. [unofficial]



261
262
263
264
265
# File 'lib/friendfeed/unofficial.rb', line 261

def add_blog(id, url, options = nil)
  params = { 'url' => url }
  params.update(options) if options
  add_service(id, 'blog', options)
end

#add_comment(entryid, body) ⇒ Object

Adds a comment to a given entry.



352
353
354
355
356
357
358
# File 'lib/friendfeed/v1.rb', line 352

def add_comment(entryid, body)
  
  call_api('comment', nil, {
      'entry' => entryid,
      'body' => body,
    })
end

#add_entry(title, options = nil) ⇒ Object Also known as: publish, share

Publishes (shares) a given entry.



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/friendfeed/v1.rb', line 274

def add_entry(title, options = nil)
  
  new_options = { 'title' => title }
  if options
    options.each { |key, value|
      case key = key.to_s
      when 'title', 'link', 'comment', 'room'
        new_options[key] = value
      when 'images'
        value.each_with_index { |value, i|
          if url = String.try_convert(value)
            link = nil
          else
            if array = Array.try_convert(value)
              value1, value2 = *array
            elsif hash = Hash.try_convert(value)
              value1, value2 = *hash.values_at('url', 'link')
            else
              raise TypeError, "Each image must be specified by <image URL>, [<image URL>, <link URL>], or {'url' => <image URL>, 'link' => <link URL>}."
            end
            url = String.try_convert(value1) or
              raise TypeError, "can't convert #{value1.class} into String"
            link = String.try_convert(value2) or
              raise TypeError, "can't convert #{value2.class} into String"
          end
          new_options['image%d_url' % i] = url
          new_options['image%d_link' % i] = link if link
        }
      when 'audios'
        value.each_with_index { |value, i|
          if url = String.try_convert(value)
            link = nil
          else
            if array = Array.try_convert(value)
              value1, value2 = *array
            elsif hash = Hash.try_convert(value)
              value1, value2 = *hash.values_at('url', 'link')
            else
              raise TypeError, "Each audio must be specified by <audio URL>, [<audio URL>, <link URL>], or {'url' => <audio URL>, 'link' => <link URL>}."
            end
            url = String.try_convert(value1) or
              raise TypeError, "can't convert #{value1.class} into String"
            link = String.try_convert(value2) or
              raise TypeError, "can't convert #{value2.class} into String"
          end
          new_options['audio%d_url' % i] = url
          new_options['audio%d_link' % i] = link if link
        }
      when 'files'
        value.each_with_index { |value, i|
          if file = IO.try_convert(value)
            link = nil
          else
            if array = Array.try_convert(value)
              value1, value2 = *array
            elsif hash = Hash.try_convert(value)
              value1, value2 = *hash.values_at('file', 'link')
            else
              raise TypeError, "Each file must be specified by <file IO>, [<file IO>, <link URL>], or {'file' => <file IO>, 'link' => <link URL>}."
            end
            file = IO.try_convert(value1) or
              raise TypeError, "can't convert #{value1.class} into IO"
            link = String.try_convert(value2) or
              raise TypeError, "can't convert #{value2.class} into String"
          end
          new_options['file%d' % i] = file
          new_options['file%d_link' % i] = link if link
        }
      end
    }
  end
  call_api('share', nil, new_options)['entries'].first
end

#add_feed(id, url, options = nil) ⇒ Object

Adds a feed to the authenticated user, a group or an imaginary friend specified by a unique ID. Specify ‘isstatus’ => ‘on’ to display entries as messages (no link), and ‘importcomment’ => ‘on’ to include entry description as a comment. [unofficial]



250
251
252
253
254
# File 'lib/friendfeed/unofficial.rb', line 250

def add_feed(id, url, options = nil)
  params = { 'url' => url }
  params.update(options) if options
  add_service(id, 'feed', options)
end

#add_like(entryid) ⇒ Object

Adds a “like” to a given entry.



390
391
392
393
394
395
# File 'lib/friendfeed/v1.rb', line 390

def add_like(entryid)
  
  call_api('like', nil, {
      'entry' => entryid,
    })
end

#add_service(id, service, options = nil) ⇒ Object

Adds a feed to the authenticated user, a group or an imaginary friend specified by a unique ID. Specify ‘isstatus’ => ‘on’ to display entries as messages (no link), and ‘importcomment’ => ‘on’ to include entry description as a comment. [unofficial]



205
206
207
208
209
210
211
212
# File 'lib/friendfeed/unofficial.rb', line 205

def add_service(id, service, options = nil)
  params = {
    'stream' => id,
    'service' => service,
  }
  params.update(options) if options
  post(ROOT_URI + '/a/configureservice', params)
end

#add_twitter(id, twitter_name, options = nil) ⇒ Object

Adds a Twitter service to the authenticated user, a group or an imaginary friend specified by a unique ID. [unofficial]



269
270
271
272
273
# File 'lib/friendfeed/unofficial.rb', line 269

def add_twitter(id, twitter_name, options = nil)
  params = { 'username' => twitter_name }
  params.update(options) if options
  add_service(id, 'twitter', params)
end

#api_login(nickname, remote_key) ⇒ Object

Performs a login with a nickname and remote key and returns self. This enables call of any official API that requires authentication. It is not needed to call this method if you have called login(), which internally obtains a remote key and calls this method. An exception is raised if authentication fails.



61
62
63
64
65
66
67
68
69
# File 'lib/friendfeed/v1.rb', line 61

def (nickname, remote_key)
  @nickname = nickname
  @remote_key = remote_key
  @api_agent = get_api_agent()
  @api_agent.auth(@nickname, @remote_key)
  validate

  self
end

#call_api(path, get_parameters = nil, post_parameters = nil, raw = false) ⇒ Object

Calls an official API specified by a path with optional get_parameters and post_parameters, and returns an object parsed from a JSON response. If post_parameters is given, a POST request is issued. A GET request is issued otherwise.



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
# File 'lib/friendfeed/v1.rb', line 75

def call_api(path, get_parameters = nil, post_parameters = nil, raw = false)
  api_agent = get_api_agent()

  uri = API_URI + path
  if get_parameters
    uri.query = get_parameters.map { |key, value|
      key = key.to_s
      if array = Array.try_convert(value)
        value = array.join(',')
      else
        value = value.to_s
      end
      URI.encode(key) + "=" + URI.encode(value)
    }.join('&')
  end

  if post_parameters
    body = api_agent.post(uri, post_parameters).body
  else
    body = api_agent.get_file(uri)
  end

  if raw
    body
  else
    JSON.parse(body)
  end
end

#change_picture(id, io) ⇒ Object

Changes the picture of the authenticated user, a group or an imaginary friend. [unofficial]



277
278
279
280
# File 'lib/friendfeed/unofficial.rb', line 277

def change_picture(id, io)
  post(ROOT_URI + '/a/changepicture', 'stream' => id,
    'picture' => io)
end

#change_picture_to_url(id, url) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'bin/tw2ff', line 100

def change_picture_to_url(id, url)
  t = Tempfile.open("picture")
  t.write get_file(url)
  t.close
  File.open(t.path) { |f|
    change_picture(id, f)
  }
  t.unlink
end

#create_group(nickname, name, type = 'group') ⇒ Object

Creates a new feed of a given (unique) nickname and display name, and returns a unique ID string on success. The type can be one of “group”, “microblog” and “public”. Like other methods in general, an exception is raised on failure. [unofficial]



134
135
136
# File 'lib/friendfeed/unofficial.rb', line 134

def create_group(nickname, name, type = 'group')
  post(ROOT_URI + '/a/createfeed', 'nickname' => nickname, 'name' => name, 'type' => type).xpath("(//a[@class='l_feedinvite'])[1]/@sid").to_s
end

#create_imaginary_friend(nickname) ⇒ Object

Creates an imaginary friend of a given nickname and returns a unique ID string on success. Like other methods in general, an exception is raised on failure. [unofficial]



291
292
293
# File 'lib/friendfeed/unofficial.rb', line 291

def create_imaginary_friend(nickname)
  post(ROOT_URI + '/a/createimaginary', 'name' => nickname).xpath("//*[@id='serviceseditor']/@streamid").to_s
end

#delete_comment(entryid, commentid) ⇒ Object

Deletes a given comment.



371
372
373
374
375
376
377
# File 'lib/friendfeed/v1.rb', line 371

def delete_comment(entryid, commentid)
  
  call_api('comment/delete', nil, {
      'entry' => entryid,
      'comment' => commentid,
    })
end

#delete_entry(entryid) ⇒ Object

Deletes an existing entry of a given entryid.



406
407
408
409
410
411
# File 'lib/friendfeed/v1.rb', line 406

def delete_entry(entryid)
  
  call_api('entry/delete', nil, {
      'entry' => entryid,
    })
end

#delete_like(entryid) ⇒ Object

Deletes an existing “like” from a given entry.



398
399
400
401
402
403
# File 'lib/friendfeed/v1.rb', line 398

def delete_like(entryid)
  
  call_api('like/delete', nil, {
      'entry' => entryid,
    })
end

#edit_comment(entryid, commentid, body) ⇒ Object

Edits a given comment.



361
362
363
364
365
366
367
368
# File 'lib/friendfeed/v1.rb', line 361

def edit_comment(entryid, commentid, body)
  
  call_api('comment', nil, {
      'entry' => entryid,
      'comment' => commentid,
      'body' => body,
    })
end

#edit_group(id, options) ⇒ Object

Edits profile information of a group specified by a unique ID. Supported fields are ‘nickname’, ‘name’, ‘description’, ‘access’ (‘private’, ‘semipublic’ or ‘public’), and ‘anyoneinvite’ (none or ‘1’). [unofficial]



169
170
171
172
173
# File 'lib/friendfeed/unofficial.rb', line 169

def edit_group(id, options)
  params = get_group(id)
  params.update(options)
  post(EDIT_GROUP_URI, params)
end

#edit_profile(hash) ⇒ Object

Edits profile information of the authenticated user. The fields “name” and “picture” are supported.



114
115
116
117
# File 'lib/friendfeed/v1.rb', line 114

def edit_profile(hash)
  nickname or 
  call_api('user/%s/profile' % URI.encode(nickname), nil, hash)
end

#edit_service(id, serviceid, options = nil) ⇒ Object

Edits a service of the authenticated user, a group or an imaginary friend specified by a unique ID. [unofficial]



216
217
218
219
220
# File 'lib/friendfeed/unofficial.rb', line 216

def edit_service(id, serviceid, options = nil)
  params = get_service(id, serviceid)
  params.update(options) if options
  post(ROOT_URI + '/a/configureservice', params)
end

#follow_twitter_user(twitter_name, display_name, picture_url = nil) ⇒ Object



110
111
112
113
114
# File 'bin/tw2ff', line 110

def follow_twitter_user(twitter_name, display_name, picture_url = nil)
  id = create_imaginary_friend(display_name)
  add_twitter(id, twitter_name, 'includeatreplies' => 'on')
  change_picture_to_url(id, picture_url) if picture_url
end

#get_entries(entryids) ⇒ Object

Gets an array of entries of given entryids. An exception is raised when it fails.



248
249
250
# File 'lib/friendfeed/v1.rb', line 248

def get_entries(entryids)
  call_api('feed/entry', 'entry_id' => entryids)['entries']
end

#get_entry(entryid) ⇒ Object

Gets an entry of a given entryid. An exception is raised when it fails.



242
243
244
# File 'lib/friendfeed/v1.rb', line 242

def get_entry(entryid)
  call_api('feed/entry/%s' % URI.encode(entryid))['entries'].first
end

#get_group(id) ⇒ Object

Gets profile information of a group specified by a unique ID. [unofficial]



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/friendfeed/unofficial.rb', line 142

def get_group(id)
  parser = post(ROOT_URI + '/a/profiledialog', 'stream' => id)['html_parser']
  form = parser.xpath("//form[1]")
  hash = { 'stream' => id }
  form.xpath(".//input").each { |input|
    case input['type'].downcase
    when 'text', 'hidden'
      hash[input['name']] = input['value']
    when 'radio', 'checkbox'
      if input['checked']
        value = input['value'] || 'on'
        if value && !value.empty?
          hash[input['name']] = value
        end
      end
    end
  }
  form.xpath(".//textarea").each { |input|
    hash[input['name']] = input.text
  }
  hash
end

#get_home_entries(options = nil) ⇒ Object

Gets an array of the entries the authenticated user would see on their home page.



166
167
168
169
# File 'lib/friendfeed/v1.rb', line 166

def get_home_entries(options = nil)
  
  call_api('feed/home', options)['entries']
end

#get_imaginary_friend(id) ⇒ Object

Gets profile information of one of the authenticated user’s imaginary friends.



155
156
157
# File 'lib/friendfeed/v1.rb', line 155

def get_imaginary_friend(id)
  get_profile(id)
end

#get_imaginary_friendsObject

Gets an array of profile information of the authenticated user’s imaginary friends.



142
143
144
145
146
147
148
149
150
151
# File 'lib/friendfeed/v1.rb', line 142

def get_imaginary_friends
  nickname or 
  profiles = []
  get_profile(@nickname)['subscriptions'].each { |subscription|
    if subscription['nickname'].nil?
      profiles << get_profile(subscription['id'])
    end
  }
  profiles
end

#get_list_entries(nickname, options = nil) ⇒ Object

Gets an array of the entries for the authenticated user’s list of a given nickname



173
174
175
176
# File 'lib/friendfeed/v1.rb', line 173

def get_list_entries(nickname, options = nil)
  
  call_api('feed/list/%s' % URI.encode(nickname), options)['entries']
end

#get_list_profile(nickname) ⇒ Object

Gets profile information of the authenticated user’s list of a given nickname in hash.



462
463
464
# File 'lib/friendfeed/v1.rb', line 462

def get_list_profile(nickname)
  call_api('list/%s/profile' % URI.encode(nickname))
end

#get_multi_user_entries(nicknames, options = nil) ⇒ Object

Gets an array of the most recent entries from users of given nicknames.



188
189
190
191
192
# File 'lib/friendfeed/v1.rb', line 188

def get_multi_user_entries(nicknames, options = nil)
  new_options = { 'nickname' => nicknames }
  new_options.merge!(options) if options
  call_api('feed/user', new_options)['entries']
end

#get_picture(nickname = nil, size = 'small') ⇒ Object

Gets a picture of a user of a given nickname (defaulted to the authenticated user) in blob. Size can be ‘small’ (default), ‘medium’ or ‘large’,



442
443
444
445
446
# File 'lib/friendfeed/v1.rb', line 442

def get_picture(nickname = nil, size = 'small')
  nickname ||= @nickname
  nickname or 
  call_api('/%s/picture' % URI.escape(nickname), { 'size' => size }, nil, true)
end

#get_profile(nickname = nil) ⇒ Object

Gets profile information of a user of a given nickname, defaulted to the authenticated user, in hash.



106
107
108
109
110
# File 'lib/friendfeed/v1.rb', line 106

def get_profile(nickname = nil)
  nickname ||= @nickname
  nickname or 
  call_api('user/%s/profile' % URI.encode(nickname))
end

#get_profiles(nicknames) ⇒ Object

Gets an array of profile information of users of given nicknames.



121
122
123
# File 'lib/friendfeed/v1.rb', line 121

def get_profiles(nicknames)
  call_api('profiles', 'nickname' => nicknames)['profiles']
end

#get_public_entries(options = nil) ⇒ Object

Gets an array of the most recent public entries.



160
161
162
# File 'lib/friendfeed/v1.rb', line 160

def get_public_entries(options = nil)
  call_api('feed/public', options)['entries']
end

#get_real_friends(nickname = nil) ⇒ Object

Gets an array of profile information of friends of a user of a given nickname (defaulted to the authenticated user) is subscribing to.



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/friendfeed/v1.rb', line 128

def get_real_friends(nickname = nil)
  nickname ||= @nickname
  nickname or 
  nicknames = []
  get_profile(@nickname)['subscriptions'].each { |subscription|
    if nickname = subscription['nickname']
      nicknames << nickname
    end
  }
  get_profiles(nicknames)
end

#get_room_entries(nickname, options = nil) ⇒ Object

Gets an array of the most recent entries in a room of a given nickname.



230
231
232
# File 'lib/friendfeed/v1.rb', line 230

def get_room_entries(nickname, options = nil)
  call_api('feed/room/%s' % URI.encode(nickname), options)['entries']
end

#get_room_picture(nickname, size = 'small') ⇒ Object

Gets a picture of a room of a given nickname in blob. Size can be ‘small’ (default), ‘medium’ or ‘large’,



450
451
452
# File 'lib/friendfeed/v1.rb', line 450

def get_room_picture(nickname, size = 'small')
  call_api('/rooms/%s/picture' % URI.escape(nickname), { 'size' => size }, nil, true)
end

#get_room_profile(nickname) ⇒ Object

Gets profile information of a room of a given nickname in hash.



456
457
458
# File 'lib/friendfeed/v1.rb', line 456

def get_room_profile(nickname)
  call_api('room/%s/profile' % URI.encode(nickname))
end

#get_rooms_entries(options = nil) ⇒ Object

Gets an array of the entries the authenticated user would see on their rooms page.



236
237
238
# File 'lib/friendfeed/v1.rb', line 236

def get_rooms_entries(options = nil)
  call_api('feed/rooms', options)['entries']
end

#get_service(id, serviceid) ⇒ Object

Gets information of a service specified by a unique ID. [unofficial]



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/friendfeed/unofficial.rb', line 177

def get_service(id, serviceid)
  parser = post(ROOT_URI + '/a/servicedialog',
    'serviceid' => serviceid, 'stream' => id)['html_parser']
  form = parser.at("//form[1]")
  hash = { 'stream' => id }
  form.xpath(".//input").each { |input|
    case input['type'].downcase
    when 'text', 'hidden'
      hash[input['name']] = input['value']
    when 'radio', 'checkbox'
      if input['checked']
        value = input['value'] || 'on'
        if value && !value.empty?
          hash[input['name']] = value
        end
      end
    end
  }
  form.xpath(".//textarea").each { |input|
    hash[input['name']] = input.text
  }
  hash
end

#get_services(nickname = nil) ⇒ Object

Gets a list of services of a user or a room of a given nickname, defaulted to the authenticated user.



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
# File 'lib/friendfeed/unofficial.rb', line 92

def get_services(nickname = nil)
  nickname ||= @nickname
  agent = ()

  services_uri = ROOT_URI + ("/%s/services" % URI.encode(nickname))
  parser = agent.get(services_uri).parser

  active_servicelist = parser.xpath("//*[@class='active']//ul[@class='servicelist']")

  if !active_servicelist.empty?
    services = active_servicelist.xpath("./li/a").map { |a|
      {
        'service' => a['class'].split.find { |a_class|
          a_class != 'l_editservice' && a_class != 'service'
        },
        'serviceid' => a['serviceid'].to_s,
      }
    }
    profile_uri = ROOT_URI + ("/%s" % URI.encode(nickname))
    agent.get(profile_uri).parser.xpath("//div[@class='servicespreview']/a").each_with_index { |a, i|
      href = (profile_uri + a['href'].to_s).to_s
      break if profile_uri.route_to(href).relative?
      services[i]['profileUrl'] = href
    }
  else
    services = parser.xpath("//ul[@class='servicelist']/li/a").map { |a|
      {
        'service' => a['class'].split.find { |a_class|
          a_class != 'service'
        },
        'profileUrl' => (services_uri + a['href'].to_s).to_s,
      }
    }
  end
  services
end

#get_user_commented_entries(nickname = nil, options = nil) ⇒ Object

Gets an array of the most recent entries a user of a given nickname (defaulted to the authenticated user) has commented on.



197
198
199
200
201
# File 'lib/friendfeed/v1.rb', line 197

def get_user_commented_entries(nickname = nil, options = nil)
  nickname ||= @nickname
  nickname or 
  call_api('feed/user/%s/comments' % URI.encode(nickname), options)['entries']
end

#get_user_discussed_entries(nickname = nil, options = nil) ⇒ Object

Gets an array of the most recent entries a user of a given nickname (defaulted to the authenticated user) has commented on or like’d.



214
215
216
217
218
# File 'lib/friendfeed/v1.rb', line 214

def get_user_discussed_entries(nickname = nil, options = nil)
  nickname ||= @nickname
  nickname or 
  call_api('feed/user/%s/discussion' % URI.encode(nickname), options)['entries']
end

#get_user_entries(nickname = nil, options = nil) ⇒ Object

Gets an array of the most recent entries from a user of a given nickname (defaulted to the authenticated user).



180
181
182
183
184
# File 'lib/friendfeed/v1.rb', line 180

def get_user_entries(nickname = nil, options = nil)
  nickname ||= @nickname
  nickname or 
  call_api('feed/user/%s' % URI.encode(nickname), options)['entries']
end

#get_user_friend_entries(nickname = nil, options = nil) ⇒ Object

Gets an array of the most recent entries from friends of a user of a given nickname (defaulted to the authenticated user).



222
223
224
225
226
# File 'lib/friendfeed/v1.rb', line 222

def get_user_friend_entries(nickname = nil, options = nil)
  nickname ||= @nickname
  nickname or 
  call_api('feed/user/%s/friends' % URI.encode(nickname), options)['entries']
end

#get_user_liked_entries(nickname = nil, options = nil) ⇒ Object

Gets an array of the most recent entries a user of a given nickname (defaulted to the authenticated user) has like’d.



205
206
207
208
209
# File 'lib/friendfeed/v1.rb', line 205

def get_user_liked_entries(nickname = nil, options = nil)
  nickname ||= @nickname
  nickname or 
  call_api('feed/user/%s/likes' % URI.encode(nickname), options)['entries']
end

#hide_entry(entryid) ⇒ Object

Hides an existing entry of a given entryid.



423
424
425
426
427
428
# File 'lib/friendfeed/v1.rb', line 423

def hide_entry(entryid)
  
  call_api('entry/hide', nil, {
      'entry' => entryid,
    })
end

#login(nickname, password) ⇒ Object

Performs a login with a nickname and password and returns self. This enables call of any API, including both official API and unofficial API.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/friendfeed/unofficial.rb', line 36

def (nickname, password)
  @nickname = nickname
  @password = password
  @login_agent = Mechanize.new

  page = @login_agent.get(LOGIN_URI)

   = page.forms.find { |form|
    LOGIN_URI + form.action == LOGIN_URI
  } or raise 'Cannot locate a login form'

  .set_fields(:email => @nickname, :password => @password)

  page = .submit

   = page.forms.find { |form|
    LOGIN_URI + form.action == LOGIN_URI
  } and raise 'Login failed'

  page = @login_agent.get(ROOT_URI + "/account/api")
  remote_key = page.parser.xpath("(//table[@class='remotekey']//td[@class='value'])[2]/text()").to_s

  (nickname, remote_key)
end

#post(uri, query = {}) ⇒ Object

Posts a request to an internal API of FriendFeed and returns either a parser object for an HTML response or an object parsed from a JSON response). [unofficial]



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
# File 'lib/friendfeed/unofficial.rb', line 64

def post(uri, query = {})
  agent = ()

  page = agent.post(uri, {
      'at' => agent.cookies.find { |cookie|
        cookie.domain == 'friendfeed.com' && cookie.name == 'AT'
      }.value
    }.update(query))
  if page.respond_to?(:parser)
    parser = page.parser
    messages = parser.xpath("//div[@id='errormessage']/text()")
    messages.empty? or
      raise messages.map { |message| message.to_s }.join(" ")
    parser
  else
    json = JSON.parse(page.body)
    message = json['error'] and
      raise message
    if html_frag = json['html']
      html_body = '<html><body>' << html_frag << '</body></html>'
      json['html_parser'] = Mechanize.html_parser.parse(html_body)
    end
    json
  end
end

#refresh_service(id, serviceid, service, options = nil) ⇒ Object

Refreshes a feed of the authenticated user, a group or an imaginary friend specified by a unique ID. [unofficial]



237
238
239
240
241
242
243
244
# File 'lib/friendfeed/unofficial.rb', line 237

def refresh_service(id, serviceid, service, options = nil)
  params = {
    'stream' => id,
    'serviceid' => serviceid,
  }
  params.update(options) if options
  post(ROOT_URI + '/a/crawlservice', params)
end

#remove_service(id, serviceid, service, options = nil) ⇒ Object

Removes a service of the authenticated user, a group or an imaginary friend specified by a unique ID. Specify ‘deleteentries’ => ‘on’ to delete entries also. [unofficial]



225
226
227
228
229
230
231
232
233
# File 'lib/friendfeed/unofficial.rb', line 225

def remove_service(id, serviceid, service, options = nil)
  params = {
    'stream' => id,
    'service' => service,
    'serviceid' => serviceid,
  }
  params.update(options) if options
  post(ROOT_URI + '/a/removeservice', params)
end

#rename_imaginary_friend(id, nickname) ⇒ Object

Renames an imaginary friend specified by a unique ID to a given nickname. [unofficial]



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/friendfeed/unofficial.rb', line 297

def rename_imaginary_friend(id, nickname)
  parser = post(ROOT_URI + '/a/profiledialog', 'stream' => id)['html_parser']
  form = parser.xpath("//form[1]")
  hash = { 'stream' => id }
  form.xpath(".//input").each { |input|
    case input['type'].downcase
    when 'text'
      hash[input['name']] = input['value']
    end
  }
  form.xpath(".//textarea").each { |input|
    hash[input['name']] = input.text
  }
  hash['name'] = nickname
  post(ROOT_URI + '/a/editprofile', hash)
end

#search(query, options = nil) ⇒ Object

Gets an array of entries that match a given query.



253
254
255
256
257
# File 'lib/friendfeed/v1.rb', line 253

def search(query, options = nil)
  new_options = { 'q' => query }
  new_options.merge!(options) if options
  call_api('feed/search', 'q' => new_options)['entries']
end

#search_for_domain(url, options = nil) ⇒ Object

Gets an array of entries that link to a given domain.



267
268
269
270
271
# File 'lib/friendfeed/v1.rb', line 267

def search_for_domain(url, options = nil)
  new_options = { 'url' => url }
  new_options.merge!(options) if options
  call_api('feed/domain', new_options)['entries']
end

#search_for_url(url, options = nil) ⇒ Object

Gets an array of entries that link to a given url.



260
261
262
263
264
# File 'lib/friendfeed/v1.rb', line 260

def search_for_url(url, options = nil)
  new_options = { 'url' => url }
  new_options.merge!(options) if options
  call_api('feed/url', new_options)['entries']
end

#subscribe_to_room(nickname) ⇒ Object

Subscribes to a room of a given nickname and returns a status string.



480
481
482
# File 'lib/friendfeed/v1.rb', line 480

def subscribe_to_room(nickname)
  call_subscription_api('room/%s/subscribe' % URI.encode(nickname))['status']
end

#subscribe_to_user(nickname) ⇒ Object

Subscribes to a user of a given nickname and returns a status string.



468
469
470
# File 'lib/friendfeed/v1.rb', line 468

def subscribe_to_user(nickname)
  call_subscription_api('user/%s/subscribe' % URI.encode(nickname))['status']
end

#undelete_comment(entryid, commentid) ⇒ Object

Undeletes a given comment that is already deleted.



380
381
382
383
384
385
386
387
# File 'lib/friendfeed/v1.rb', line 380

def undelete_comment(entryid, commentid)
  
  call_api('comment/delete', nil, {
      'entry' => entryid,
      'comment' => commentid,
      'undelete' => 'on',
    })
end

#undelete_entry(entryid) ⇒ Object

Undeletes a given entry that is already deleted.



414
415
416
417
418
419
420
# File 'lib/friendfeed/v1.rb', line 414

def undelete_entry(entryid)
  
  call_api('entry/delete', nil, {
      'entry' => entryid,
      'undelete' => 'on',
    })
end

#unhide_entry(entryid) ⇒ Object

Unhides a given entry that is already hidden.



431
432
433
434
435
436
437
# File 'lib/friendfeed/v1.rb', line 431

def unhide_entry(entryid)
  
  call_api('entry/hide', nil, {
      'entry' => entryid,
      'unhide' => 'on',
    })
end

#unsubscribe_from(id) ⇒ Object

Unsubscribe from a friend, a group or an imaginary friend specified by a unique ID. [unofficial]



284
285
286
# File 'lib/friendfeed/unofficial.rb', line 284

def unsubscribe_from(id)
  post(ROOT_URI + '/a/unsubscribe', 'stream' => id)
end

#unsubscribe_from_room(nickname) ⇒ Object

Unsubscribes from a room of a given nickname and returns a status string.



486
487
488
# File 'lib/friendfeed/v1.rb', line 486

def unsubscribe_from_room(nickname)
  call_subscription_api('room/%s/subscribe?unsubscribe=1' % URI.encode(nickname))['status']
end

#unsubscribe_from_user(nickname) ⇒ Object

Unsubscribes from a user of a given nickname and returns a status string.



474
475
476
# File 'lib/friendfeed/v1.rb', line 474

def unsubscribe_from_user(nickname)
  call_subscription_api('user/%s/subscribe?unsubscribe=1' % URI.encode(nickname))['status']
end