Class: Thimbl::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Initialize the thimbl client.

Use:

Thimbl.new(
  :plan_path  => <path to the plan file>,
  :cache_path => <path to the cache file>
  :user       => <the user@domain>,
  :password   => <the user password>
)


44
45
46
47
48
49
50
51
52
# File 'lib/thimbl/base.rb', line 44

def initialize( opts = {} )
  @plan_path = opts['plan_path']
  @cache_path = opts['cache_path']
  @user = opts['user']
  @password = opts['password']

  @data = nil
  @address = nil
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



32
33
34
# File 'lib/thimbl/base.rb', line 32

def address
  @address
end

#cache_pathObject (readonly)

Returns the value of attribute cache_path.



32
33
34
# File 'lib/thimbl/base.rb', line 32

def cache_path
  @cache_path
end

#dataObject (readonly)

Returns the value of attribute data.



32
33
34
# File 'lib/thimbl/base.rb', line 32

def data
  @data
end

#passwordObject (readonly)

Returns the value of attribute password.



32
33
34
# File 'lib/thimbl/base.rb', line 32

def password
  @password
end

#plan_pathObject (readonly)

Returns the value of attribute plan_path.



32
33
34
# File 'lib/thimbl/base.rb', line 32

def plan_path
  @plan_path
end

#userObject (readonly)

Returns the value of attribute user.



32
33
34
# File 'lib/thimbl/base.rb', line 32

def user
  @user
end

Class Method Details

.parse_time(time) ⇒ Object



219
220
221
# File 'lib/thimbl/base.rb', line 219

def self.parse_time( time )
  Time.utc( time[0,4], time[4,2], time[6,2], time[8,2], time[10,2], time[12,2] )
end

Instance Method Details

#fetchObject

Fetch all the info and timelines of all the users you are following.

Use:

thimbl.fetch


137
138
139
140
141
142
143
144
145
# File 'lib/thimbl/base.rb', line 137

def fetch
  following.map { |f| f['address'] }.each do |followed_address|
    address_finger = Thimbl::Finger.run followed_address
    address_plan = address_finger.match(/Plan:\s*(.*)/m)[1].gsub("\\\n",'')
    data['plans'][followed_address] = JSON.load( address_plan )
  end

  save_data
end

#follow(follow_nick, follow_address) ⇒ Object

Add a new user to follow

Use:

thimbl.follow 'nick', 'address'

To publish your following users you have to call:

thimbl.push


127
128
129
130
# File 'lib/thimbl/base.rb', line 127

def follow( follow_nick, follow_address )
  data['plans'][address]['following'] << { 'nick' => follow_nick, 'address' => follow_address }
  save_data
end

#followingObject

Returns all the info about the users you are following.

Use:

thimbl.following


202
203
204
# File 'lib/thimbl/base.rb', line 202

def following
  data['plans'][address]['following']
end

#load_dataObject

Charge into the ‘thimbl` object all the data into your `cache` file. Use:

thimbl.load_data


181
182
183
184
# File 'lib/thimbl/base.rb', line 181

def load_data
  @data = JSON.load( File.read cache_path )
  @address = data['me']
end

#messagesObject

Returns all the messages of you and all the users you are following in a chronologic order into a json format.

Use:

thimbl.messages


212
213
214
215
216
217
# File 'lib/thimbl/base.rb', line 212

def messages
  _messages = data['plans'].values.map { |e| e['messages'] }.flatten
  _messages = _messages.sort { |a,b| a['time'] <=> b['time'] }

  return _messages
end

#post(text) ⇒ Object

Post a new message in your time-line.

Use:

thimbl.post <message>

To publish your comment you have to call:

thimbl.push


108
109
110
111
112
113
114
115
116
117
# File 'lib/thimbl/base.rb', line 108

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

  data['plans'][address]['messages'] << message
  save_data
end

Print every message of you and all the users you are following.

Use:

thimbl.print

The method doesn’t print anything by it self. It just returns an string with all the comments.



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/thimbl/base.rb', line 154

def print
  result = ""
  messages.each do |message|
    result += Thimbl::Base.parse_time( message['time'] ).strftime( '%Y-%m-%d %H:%M:%S' )
    result += " #{message['address']}"
    result += " > #{message['text']}"
    result += "\n"
  end

  return result
end

#pushObject

Send your actual ‘plan` file to your server

Use:

thimbl.push


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

def push
  Net::SCP.start( user.split('@')[1], user.split('@')[0], :password => password ) do |scp|
    scp.upload!( plan_path, ".plan" )
  end
end

#save_dataObject

Save all the data into the ‘thimbl` objecto into your `cache` file and `plan` file.

Use:

thimbl.save_data


192
193
194
195
# File 'lib/thimbl/base.rb', line 192

def save_data
  File.open( cache_path, 'w' ) { |f| f.write data.to_json }
  File.open( plan_path, 'w' ) { |f| f.write data['plans'][address].to_json }
end

#setup(opts = {}) ⇒ Object

Setup a new configuration, the execution of this method will delete any thing in the ‘thimbl.plan_path` file and `thimbl.cache_path` file.

Use:

thimbl.setup(
   :bio      => 'bio',
   :website  => 'website', 
   :mobile   => 'mobile', 
   :email    => 'email', 
   :address  => 'address', 
   :name     => 'name'
)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/thimbl/base.rb', line 67

def setup( opts = {} )
  opts = { 
    'bio'      => 'bio',
    'website'  => 'website', 
    'mobile'   => 'mobile', 
    'email'    => 'email', 
    'address'  => 'address', 
    'name'     => 'name'
  }.merge( opts )

  @data = {
    'me'    => opts['address'],
    'plans' => {
      opts['address'] => {
        'name' => opts['name'],
        'bio'  => opts['bio'],
        'properties' => {
          'email'   => opts['email'],
          'mobile'  => opts['mobile'],
          'website' => opts['website']
        },
        'following'  => [],
        'messages'   => [],
        'replies'    => {}
      }
    }
  }

  @address = opts['address']

  save_data
end