Class: Rflak::User

Inherits:
Object
  • Object
show all
Defined in:
lib/user.rb

Overview

Class represents single user of flaker.pl.

Constant Summary collapse

ATTR_LIST =
[:action, :url, :sex, :avatar, :login, :api_key]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ User

Returns a new instance of User.



14
15
16
17
18
# File 'lib/user.rb', line 14

def initialize(options = {})
  options.each_pair do |key, value|
    send("#{ key }=", value)
  end
end

Class Method Details

.auth(login, api_key) ⇒ Object

Try to authorize user with login and api_key. Method returns User object for good credentails or nil for bad.

login

String

api_key

String



27
28
29
30
31
# File 'lib/user.rb', line 27

def self.auth(, api_key)
  user = User.new(:login => , :api_key => api_key)
  user.auth
  user.authorized? ? user : nil
end

Instance Method Details

#authObject

Authorize User instance with login and api_key. Returns true for valid credentaials of false if not.

returns

True of False



38
39
40
41
42
43
# File 'lib/user.rb', line 38

def auth
  Flaker.basic_auth(@login, @api_key)
  resp = Flaker.get('/type:auth')
  Flaker.basic_auth('', '')
  @authorized = (resp['status']['code'] == "200")
end

#authorized?Boolean

Returns true if User instance is authorized

returns

True or False

Returns:

  • (Boolean)


49
50
51
# File 'lib/user.rb', line 49

def authorized?
  @authorized || false
end

#bookmarksObject

Returns array of favourited entries. Raises Rflak::NotAuthorized if not authorized user

returns

Array



68
69
70
71
72
73
74
75
# File 'lib/user.rb', line 68

def bookmarks
  resp = Flaker.auth_connection(self) do |connection|
    connection.get("/type:list/source:bookmarks/login:#{ @login }")
  end
  resp['bookmarks'].map do |id|
    Flaker.fetch('show') { |flak| flak.entry_id(id) }
  end
end

#followers(extended = false) ⇒ Object

Return login list of users that follow user. Pass true as parameter if you want to get extended data (avatar, fiest name, url, sex, …)

extended

Boolean, default false

returns

Array



83
84
85
86
87
88
89
90
# File 'lib/user.rb', line 83

def followers(extended = false)
  request_path = "/type:list/source:followedby/login:#{ @login }"
  request_path << "/extended:true" if extended
  resp = Flaker.auth_connection(self) do |connection|
    connection.get(request_path)
  end
  resp['followedby']
end

#following(extended = false) ⇒ Object

Returns login list of users following by user. Pass true as parameter if you want to get extended data (avatar, fiest name, url, sex, …)

extended

Boolean, default false

returns

Array



98
99
100
101
102
103
104
105
# File 'lib/user.rb', line 98

def following(extended = false)
  request_path = "/type:list/source:following/login:#{ @login }"
  request_path << "/extended:true" if extended
  resp = Flaker.auth_connection(self) do |connection|
    connection.get(request_path)
  end
  resp['following']
end

#tagsObject

Returns array of watched tags. Raises Rflak::NotAuthorized if not authorized user

returns

Array



57
58
59
60
61
62
# File 'lib/user.rb', line 57

def tags
  resp = Flaker.auth_connection(self) do |connection|
    connection.get("/type:list/source:tags/login:#{ @login }")
  end
  return resp['tags']
end