Class: Oos4ruby::User

Inherits:
Bean::BeanClass show all
Includes:
Bean
Defined in:
lib/oos4ruby/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Bean

append_features

Methods inherited from Bean::BeanClass

#author, #contains?, #delete!, #to_s, #update!

Constructor Details

#initialize(entry, auth, slug = nil) ⇒ User



17
18
19
20
21
# File 'lib/oos4ruby/user.rb', line 17

def initialize(entry, auth, slug = nil)
  @xml = entry
  @auth = auth
  @slug = slug || @xml.child('slug', OosNamespace).text
end

Instance Attribute Details

#about_me=(value) ⇒ Object

Sets the attribute about_me



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

def about_me=(value)
  @about_me = value
end

#name=(value) ⇒ Object (writeonly)

Sets the attribute name



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

def name=(value)
  @name = value
end

#nick=(value) ⇒ Object

Sets the attribute nick



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

def nick=(value)
  @nick = value
end

#no_mails=(value) ⇒ Object (writeonly)

Sets the attribute no_mails



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

def no_mails=(value)
  @no_mails = value
end

#no_newsletter=(value) ⇒ Object (writeonly)

Sets the attribute no_newsletter



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

def no_newsletter=(value)
  @no_newsletter = value
end

#only_contacts=(value) ⇒ Object (writeonly)

Sets the attribute only_contacts



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

def only_contacts=(value)
  @only_contacts = value
end

#slugObject (readonly)

Returns the value of attribute slug.



13
14
15
# File 'lib/oos4ruby/user.rb', line 13

def slug
  @slug
end

#surname=(value) ⇒ Object (writeonly)

Sets the attribute surname



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

def surname=(value)
  @surname = value
end

Class Method Details

.find(auth, slug = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/oos4ruby/user.rb', line 23

def User.find( auth, slug = nil )
  raise Oos4ruby::UnknownUser if auth.method?(:app) && slug.nil?
  
  uri = USERS_URL
  if slug
    if slug.is_a?URI
      uri = slug      
    elsif slug.is_a?String and slug =~ /^http/
      uri = URI.parse(slug)
    else
      uri += "/#{slug}" 
    end
  end
  
  getter = HTTPInvoker.new uri.to_s, auth
  
  worked = getter.get
  
  if worked
    if slug #response is an Entry
      entry = Entry.new getter.body
      return User.new(entry, auth)
    else
      #response is a feed
      feed = Feed.read getter.body        
      rel_edit = feed.entries[0].link('edit')
      
      getter = HTTPInvoker.new rel_edit, auth
      worked = getter.get
      if worked
        entry = Entry.new getter.body
        return User.new(entry, auth)
      else
        raise Oos4ruby::UnknownUser
      end
    end
  end    
end

Instance Method Details

#avatarObject



74
75
76
# File 'lib/oos4ruby/user.rb', line 74

def avatar
  @xml.link('avatar')
end

#avatar!(file_path) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/oos4ruby/user.rb', line 78

def avatar!(file_path)
  getter = HTTPInvoker.new @xml.link('edit-media'), @auth
  worked = getter.get
  
  if worked
    file = File.new(file_path, 'w+')
    file << getter.body
  end
end

#contacts(opts = {}) ⇒ Object



70
71
72
# File 'lib/oos4ruby/user.rb', line 70

def contacts( opts = {} )
  @contacts_collection ||= Contact.find_by_user(@auth, @slug, opts)
end

#sites(opts = {}) ⇒ Object



66
67
68
# File 'lib/oos4ruby/user.rb', line 66

def sites( opts = {} )
  @sites_collection ||= Site.find_by_user(@auth, @slug, opts)
end

#to_xmlObject



62
63
64
# File 'lib/oos4ruby/user.rb', line 62

def to_xml
  @xml.to_s
end

#update_avatar!(file_path) ⇒ Object

Raises:

  • (RuntimeError)


88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/oos4ruby/user.rb', line 88

def update_avatar!(file_path)
  require 'mime/types'
  
  file = File.open(file_path, "rb") { |f| f.read } 
      
  putter = HTTPInvoker.new(@xml.link('edit-media'), @auth)
  
  putter.set_header 'Content-Length', File.size(file_path)
  
  worked = putter.put MIME::Types.type_for(file_path)[0].to_s, file
  
  raise RuntimeError unless worked
end