Class: Zooline::Command

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

Class Method Summary collapse

Class Method Details

.delegate(command, params) ⇒ Object

Public: allows main access to most commands.

Returns output based on method calls.



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/zooline/command.rb', line 37

def delegate(command, params)
  if command != 'set' && !Credentials.check?
    Output::displayMessage 'You need to set up your credentials!
$ zooline set <username> <password>'
    return
  end

  return set(params) if command == 'set'
  return help if command == 'help'

  setCredentials

  if command != 'sync' && !storage.check?
    Output::displayMessage "Sync first, there is nothing in the file"
    return
  end

  return sync         if command == 'sync'
  return get(params)  if command == 'get'
  return list(params) if command == 'list'
  return wipeout      if command == 'wipeout'

  return help
end

.execute(*args) ⇒ Object

Public: executes a command.

args - The actual commands to operate on. Can be as few as zero

arguments or as many as three.


18
19
20
21
22
23
24
# File 'lib/zooline/command.rb', line 18

def execute(*args)
  @sess = Session.new
  command = args.shift
  params = args

  delegate(command, params)
end

.get(params) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/zooline/command.rb', line 122

def get(params)
  bks = []
  if !params.empty?
    if params.first.match(/-[a-z]*(c)[a-z]*/)
      copy = true
    end

    if params.first.match(/-[a-z]*(i)[a-z]*/)
      params.shift
      bks << search_items_by_uid(params.first)
    elsif params.first.match(/-[a-z]*(t)[a-z]*/)
      params.shift
      bks = search_items_by_title(params.first)
    else
      params.shift if copy
      bks = search_items_by_title(params.first)
    end
  else
    bks << storage.bookmarks.first
  end

  unless bks.first.nil?
    if bks.count == 1
      Output::displayItem bks.first
      Platform.copy(bks.first.url)
    else
      Output::displayList bks
    end

  else
    t = "No Bookmark found"
  end
end

.helpObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/zooline/command.rb', line 172

def help
  text = %{
    - Zooline: help ---------------------------------------------------

  zooline                       this help text
  zooline help                  this help text

  zooline set <user> <pass>     Set up your credentials
  zooline sync				  Sync bookmarks
  zooline wipout                Purge your credentials
  zooline list <n>              Returns a list of bookmark limited by n
          -t				  Order by title
          -i				  Order by uuid
          -d				  Order by date
  zooline get                   show items for a list
  }.gsub(/^ {8}/, '') # strip the first eight spaces of every line

  Output::displayMessage text
end

.list(params) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/zooline/command.rb', line 104

def list(params)
  if !params.first.nil? && params.first.match(/-[dti]/)
    p = params.shift
    order = {'t' => 'title', 'd' => 'date', 'i' => 'uid' }[p.slice(1,1)]
  end

  if params.first.nil?
    n = 10
  elsif params.first == "all"
    n = storage.bookmarks(order).count
  elsif params.first.match(/\d/)
    n = params.first
  end

  bks = storage.bookmarks(order).slice(0,n.to_i)
  Output::displayList bks
end

.save!Object



168
169
170
# File 'lib/zooline/command.rb', line 168

def save!
  storage.save!
end

.search_items_by_title(params) ⇒ Object



162
163
164
165
166
# File 'lib/zooline/command.rb', line 162

def search_items_by_title(params)
  storage.bookmarks.select do |item|
    item.title.match(/.*#{params.first}.*/i)
  end
end

.search_items_by_uid(param) ⇒ Object



156
157
158
159
160
# File 'lib/zooline/command.rb', line 156

def search_items_by_uid(param)
  storage.bookmarks.detect do |item|
    item.uid == param
  end
end

.set(params) ⇒ Object

Private: Set the credentials



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zooline/command.rb', line 70

def set(params)
  if Credentials.check?
    Output::displayMessage "Credentials already set, use wipeout to reset them"
    return
  end

  unless params.size != 2
    c = Credentials.new(params.first, params.last)
    c.save!
    Output::displayMessage "Credentials set!"
  else
    Output::displayMessage "type your credentials: username password"
  end
end

.setCredentialsObject



26
27
28
29
30
31
32
# File 'lib/zooline/command.rb', line 26

def setCredentials
  @cred = Credentials.new

  #Set the creds in the session object
  @sess.username = @cred.username
  @sess.password = @cred.password
end

.storageObject

Public: accesses the in-memory JSON representation.

Returns a Storage instance.



10
11
12
# File 'lib/zooline/command.rb', line 10

def storage
  Zooline.storage
end

.syncObject

Sync all the bookmarks and save them in the fil Sync all the bookmarks and save them in the filee



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/zooline/command.rb', line 88

def sync
  result = Yajl::Parser.new.parse(@sess.get)

  if result.class == Hash
    Output::displayMessage result['msg']
  end

  @bookmarks = []
  result.each do |bk|
    @bookmarks << Bookmark.new(bk)
  end
  storage.bookmarks = @bookmarks
  save!
  Output::displayMessage "Bookmarks synced !"
end

.wipeoutObject



62
63
64
65
# File 'lib/zooline/command.rb', line 62

def wipeout
		Credentials::wipeout!	
		Output::displayMessage "Credentials deleted !"
end