Class: Thimbl::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, opts = {}) ⇒ Base

Initialize a new configuration

Use:

thimbl =
  Thimbl::Base.new(
    '[email protected]', 
    {  
      :bio      => 'bio',
      :website  => 'website', 
      :mobile   => 'mobile', 
      :email    => 'email', 
      :name     => 'name'
    }
  )

or just:

thimbl = Thimbl::Base.new( '[email protected]' )


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/thimbl/base.rb', line 48

def initialize( address, opts = {} )
  @address = address
  @data = {
    'name' => opts[:name],
    'bio'  => opts[:bio],
    'properties' => {
      'email'   => opts[:email],
      'mobile'  => opts[:mobile],
      'website' => opts[:website]
    },
    'following'  => [],
    'messages'   => [],
    'replies'    => {}
  }
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



28
29
30
# File 'lib/thimbl/base.rb', line 28

def address
  @address
end

#dataObject

Returns the value of attribute data.



28
29
30
# File 'lib/thimbl/base.rb', line 28

def data
  @data
end

Instance Method Details

#fetchObject

Updating cached .plan Any not pushed modification will be deleted



108
109
110
# File 'lib/thimbl/base.rb', line 108

def fetch
  @data = JSON.load( fetch_plan )
end

#follow(follow_nick, follow_address) ⇒ Object

Add a new user to user’s following _This method doesn’t push the modifications to de server._



83
84
85
86
# File 'lib/thimbl/base.rb', line 83

def follow( follow_nick, follow_address )
  return  if following.count { |e| e.address == follow_address } != 0
  data['following'] << { 'nick' => follow_nick, 'address' => follow_address }
end

#follow!(follow_nick, follow_address, password) ⇒ Object

Add a new user to user’s following and push the modifications to the server.



89
90
91
92
# File 'lib/thimbl/base.rb', line 89

def follow!( follow_nick, follow_address, password )
  follow follow_nick, follow_address
  push password
end

#followingObject

Returns all the info about the users this user is following.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/thimbl/base.rb', line 142

def following
  return []  if data['following'].nil?
  
  result = []
  
  data['following'].each do |chased|
    result << OpenStruct.new({
      :nick    => chased['nick'],
      :address => chased['address']
    })
  end
  
  return result
end

#messagesObject

Returns all this user’s messages in a chronologic order.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/thimbl/base.rb', line 123

def messages
  return []  if data['messages'].nil?
  
  result = []
  
  data['messages'].each do |message|
    result << OpenStruct.new({
      :address => address,
      :time    => Thimbl::Utils.parse_time( message['time'] ),
      :text    => message['text']
    })
  end
  
  result = result.sort { |a,b| a.time <=> b.time }

  return result
end

#post(text) ⇒ Object

Post a new message in user’s time-line. _This method doesn’t push the modifications to de server._



66
67
68
69
70
71
72
73
# File 'lib/thimbl/base.rb', line 66

def post( text )
  message = {
    'time' => Time.now.strftime('%Y%m%d%H%M%S'),
    'text' => text
  }

  data['messages'] << message
end

#post!(text, password) ⇒ Object

Post a new message in user’s time-line and push the modifications to the server.



76
77
78
79
# File 'lib/thimbl/base.rb', line 76

def post!( text, password )
  post text
  push password
end

#propertiesObject

Returns all the user properties



158
159
160
161
162
163
164
165
166
# File 'lib/thimbl/base.rb', line 158

def properties
  OpenStruct.new({
    :bio      => data['bio'],
    :name     => data['name'],
    :email    => data['properties']['email'],
    :mobile   => data['properties']['mobile'],
    :website  => data['properties']['website']
  })
end

#properties=(opts = {}) ⇒ Object

Update all the user properties _This method doesn’t push the modifications to de server._



170
171
172
173
174
175
176
# File 'lib/thimbl/base.rb', line 170

def properties=( opts = {} )
  data['bio']                   = opts[:bio]      unless opts[:bio].nil?
  data['name']                  = opts[:name]     unless opts[:name].nil?
  data['properties']['email']   = opts[:email]    unless opts[:email].nil?
  data['properties']['mobile']  = opts[:mobile]   unless opts[:mobile].nil?
  data['properties']['website'] = opts[:website]  unless opts[:website].nil?
end

#push(password) ⇒ Object

Send user’s cached .plan to user’s server It requires the password of user’s thimbl user



114
115
116
117
118
119
# File 'lib/thimbl/base.rb', line 114

def push( password )
  tmp_path = Thimbl::Utils.to_file( data.to_json )
  Net::SCP.start( address.split('@')[1], address.split('@')[0], :password => password ) do |scp|
    scp.upload!( tmp_path, ".plan" )
  end
end

#unfollow(follow_address) ⇒ Object

Remove a user from the user’s following _This method doesn’t push the modifications to de server._



96
97
98
# File 'lib/thimbl/base.rb', line 96

def unfollow( follow_address )
  data['following'].delete_if { |e| e['address'] == follow_address }
end

#unfollow!(follow_address, password) ⇒ Object

Remove a new from user’s following and push the modifications to the server.



101
102
103
104
# File 'lib/thimbl/base.rb', line 101

def unfollow!( follow_address, password )
  unfollow follow_address
  push password
end