lita-link-library 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5f3499aa974e3120831ea886f7f2d85f55aee13b
4
- data.tar.gz: 8cdcdb9d4436a59d255074e87727a7e797eb7971
3
+ metadata.gz: 68a302ffa5361b5da3a1144f5f083484946e1b81
4
+ data.tar.gz: c00595c6446af5fd19f443df8c67865a03cca65d
5
5
  SHA512:
6
- metadata.gz: faf67df806d43563492f80e678a2e46b5a5dd12e42aa9ca45703e93150e09d16673f659c934e86bea421b3f2b187f55d10c2f0032df5c13109f97100d30b80d0
7
- data.tar.gz: 4ae4bd573c62c934ed3caedad761828e6ec92d2709b094e62ee6bdb51d61d3a1d5ebd895663834e866eb95fa0d2e773b5a998eddcd957291224c6be539ec01c5
6
+ metadata.gz: 88d954267138cea0c4a2260b59a80b4a29704920ba91de321ac5579c52148922d7891ccb7f2f0c34c3fbd7415477b8e80d9a5b9067b93d858583b658fcf5f98e
7
+ data.tar.gz: e319cc49ef5beb964748a738a1202ac55151ee9ecd756164418108ecf3fafdded79c43af605f483b39e5158663d1e4985e206f482bf4b5e59c34b59c2da65526
data/README.md CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  ####Commands restricted to library admins:
26
26
 
27
- - `add read LINK TITLE DESCRIPTION` => Adds a new entry with that LINK, TITLE and DESCRIPTION attributes in the lita link library.
27
+ - `add read LINK|TITLE|DESCRIPTION` => Adds a new entry with that LINK, TITLE and DESCRIPTION attributes in the lita link library. Please use the <|>pipe<|> character as a delimiter!
28
28
 
29
29
  - `remove read TITLE` => Removes the entry with that TITLE from the lita link library.
30
30
 
@@ -1,97 +1,102 @@
1
1
  #this class performs CRUD operations on the library file
2
2
  require 'json'
3
3
  require 'lita'
4
+
4
5
  require_relative 'library_entry'
5
6
 
6
7
  module Lita
7
8
  module Handlers
8
9
  class LinkLibrary < Handler
9
-
10
+
10
11
  TITLE = /[\w\s\,\.\-\/:–]+/
11
12
  LINK = /(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/
12
13
  DESCRIPTION = /[\w\s\,\.\-\/:–]+/
13
-
14
+
14
15
  route(
15
- /^(random read)$/i,
16
- :random_read_command,
17
- command: true,
18
- help: {'random read' => 'Returns a random entry from the lita link library.'}
16
+ /^(random read)$/i,
17
+ :random_read_command,
18
+ command: true,
19
+ help: {'random read' => 'Returns a random entry from the lita link library.'}
19
20
  )
20
-
21
+
21
22
  route(
22
- /^(give me a gooder)$/i,
23
- :read_best_entries_command,
24
- command: true,
25
- help: {'give me a gooder' => 'Returns top 3 entries from the lita link library.'}
23
+ /^(give me a gooder)$/i,
24
+ :read_best_entries_command,
25
+ command: true,
26
+ help: {'give me a gooder' => 'Returns top 3 entries from the lita link library.'}
26
27
  )
27
-
28
+
28
29
  route(
29
- /^(list library)$/i,
30
- :list_library_command,
31
- command: true,
32
- help: {'list library' => 'Returns entire lita link library.'}
30
+ /^(list library)$/i,
31
+ :list_library_command,
32
+ command: true,
33
+ help: {'list library' => 'Returns entire lita link library.'}
33
34
  )
34
-
35
+
35
36
  route(
36
- /^(?:want to read)\s(#{TITLE.source})$/i,
37
- :want_to_read_command,
38
- command: true,
39
- help: {'want to read TITLE' => 'Returns the entry from lita link library with that TITLE.'}
40
- )
41
-
37
+ /^(?:want to read)\s(#{TITLE.source})$/i,
38
+ :want_to_read_command,
39
+ command: true,
40
+ help: {'want to read TITLE' => 'Returns the entry from lita link library, with that TITLE.'}
41
+ )
42
+
42
43
  route(
43
- /^(?:add read)\s(#{LINK.source})\s(#{TITLE.source})\s(#{DESCRIPTION.source})$/i,
44
- :add_read_command,
45
- command: true,
46
- restrict_to: :link_library_admins,
47
- help: {'add read LINK TITLE DESCRIPTION' => 'Adds a new entry in thelita link library '+
44
+ /^(?:add read)\s(#{LINK.source})(\|)(#{TITLE.source})(\|)(#{DESCRIPTION.source})$/i,
45
+ :add_read_command,
46
+ command: true,
47
+ restrict_to: :link_library_admins,
48
+ help: {'add read LINK|TITLE|DESCRIPTION' => 'Adds a new entry in the lita link library, '+
48
49
  'with that LINK, TITLE and DESCRIPTION attributes.'}
49
50
  )
50
51
  route(
51
- /^(remove read)\s(#{TITLE.source})$/i,
52
- :remove_read_command,
53
- command: true,
54
- restrict_to: :link_library_admins,
55
- help: {
56
- 'remove read TITLE' => 'Removes the entry from lita link library with that TITLE.'}
57
- )
58
-
52
+ /^(?:remove read)\s(#{TITLE.source})$/i,
53
+ :remove_read_command,
54
+ command: true,
55
+ restrict_to: :link_library_admins,
56
+ help: {
57
+ 'remove read TITLE' => 'Removes the entry from lita link library, with that TITLE.'}
58
+ )
59
+
59
60
  def random_read_command(response)
60
61
  response.reply random_read_entry
61
62
  end
62
-
63
+
63
64
  def read_best_entries_command(response)
64
65
  response.reply read_best_entries
65
66
  end
66
-
67
+
67
68
  def list_library_command(response)
68
69
  response.reply list_library
69
70
  end
70
-
71
+
71
72
  def want_to_read_command(response)
72
73
  title = response.matches.first[0]
73
74
  response.reply read_entry(title)
74
75
  end
75
-
76
+
76
77
  def add_read_command(response)
77
- link = response.matches.first[0]
78
- title = response.matches.first[5]
79
- description = response.matches.first[6]
80
- response.reply save_new_entry(link, title, description)
78
+ if (response.matches.first[5]!='|' or response.matches.first[7]!='|')
79
+ response.reply('Please use a valid format using the <|>pipe<|> character. <<add read LINK|TITLE|DESCRIPTION>>')
80
+ else
81
+ link = response.matches.first[0]
82
+ title = response.matches.first[6]
83
+ description = response.matches.first[8]
84
+ response.reply save_new_entry(link, title, description)
85
+ end
81
86
  end
82
-
87
+
83
88
  def remove_read_command(response)
84
89
  title = response.matches.first[0]
85
90
  response.reply delete_entry(title)
86
91
  end
87
-
92
+
88
93
  def file_writer (filename, entries)
89
94
  open(filename, 'w') do |file|
90
95
  file.truncate(0)
91
96
  file.write entries.to_json
92
97
  end
93
98
  end
94
-
99
+
95
100
  def save_new_entry (link, title, description) # allows a librarian to save a new entry in the library
96
101
  file = File.read 'library.json'
97
102
  hash_array = JSON.parse(file)
@@ -107,10 +112,10 @@ module Lita
107
112
  new_entry = {"link" => link, "title" => title, "description" => description, "number_of_downloads" => 0}
108
113
  hash_array.push new_entry
109
114
  file_writer 'library.json', hash_array
110
- return "The entry #{title} wass added in the library!"
115
+ return "The entry #{title} was added in the library!"
111
116
  end
112
117
  end
113
-
118
+
114
119
  def delete_entry (title) # allows a librarian to delete an entry from the library
115
120
  file = File.read 'library.json'
116
121
  hash_array = JSON.parse(file)
@@ -118,8 +123,8 @@ module Lita
118
123
  hash_array.each do |entry|
119
124
  if entry.has_value?(title)
120
125
  hash_array.reject! {|entry| entry.has_value?(title)}
121
- return "The entry with the title: #{title} was deleted"
122
126
  file_writer 'library.json', hash_array
127
+ return "The entry with the title #{title} was deleted"
123
128
  else
124
129
  entries_counter +=1
125
130
  end
@@ -128,16 +133,16 @@ module Lita
128
133
  response = "No entry with the title #{title} can be found in the library"
129
134
  end
130
135
  end
131
-
136
+
132
137
  def read_entry (entry_title) # returns an entry with the specified title from the library
133
138
  file = File.read 'library.json'
134
139
  hash_array = JSON.parse(file)
135
140
  entries_counter = 0
136
141
  hash_array.each do |entry|
137
142
  if entry.has_value?(entry_title)
138
- return "#{entry_title} | #{entry['link']} | #{entry['number_of_downloads']} reads | #{entry['description']}"
139
143
  entry['number_of_downloads']+=1
140
144
  file_writer 'library.json', hash_array
145
+ return "#{entry_title} | #{entry['link']} | #{entry['number_of_downloads']} reads | #{entry['description']}"
141
146
  else
142
147
  entries_counter += 1
143
148
  end
@@ -146,34 +151,40 @@ module Lita
146
151
  return "No entry entitled #{entry_title} can be found in the library DB!"
147
152
  end
148
153
  end
149
-
154
+
150
155
  def random_read_entry # returns a random entry from the library
151
156
  file = File.read 'library.json'
152
157
  hash_array = JSON.parse(file)
153
158
  entry = hash_array.sample
159
+ entry['number_of_downloads']+=1
160
+ file_writer 'library.json', hash_array
154
161
  return "Random read: #{entry['title']} | #{entry['link']} | #{entry['number_of_downloads']} reads | #{entry['description']}"
155
162
  end
156
-
163
+
157
164
  def read_best_entries # returns the top 3 downloaded entries from the library
158
165
  file = File.read 'library.json'
159
166
  hash_array = JSON.parse(file)
160
- hash_array.sort! {|a1, a2| a1['number_of_downloads'] <=> a2['number_of_downloads']}
167
+ return_array=[]
168
+ hash_array.sort! {|a1, a2| a2['number_of_downloads'] <=> a1['number_of_downloads']}
161
169
  for i in 0..2
162
- "#{hash_array[i]['title']} | #{hash_array[i]['link']} | #{hash_array[i]['number_of_downloads']} reads | #{hash_array[i]['description']}\n"
170
+ return_array << "#{hash_array[i]['title']} | #{hash_array[i]['link']} | #{hash_array[i]['number_of_downloads']} reads | #{hash_array[i]['description']}\n"
163
171
  end
172
+ return return_array
164
173
  end
165
-
174
+
166
175
  def list_library # returns the entire library
167
176
  file = File.read 'library.json'
168
177
  hash_array = JSON.parse(file)
178
+ return_array=[]
169
179
  hash_array.each do |entry|
170
180
  library_entry = LibraryEntry.new entry['link'], entry['title'], entry['description'], entry['number_of_downloads']
171
- return "#{library_entry['title']} | #{library_entry['link']} | #{library_entry['number_of_downloads']} reads | #{library_entry['description']}\n"
181
+ return_array << "#{library_entry.title} | #{library_entry.link} | #{library_entry.number_of_downloads} reads | #{library_entry.description}\n"
172
182
  end
183
+ return return_array
173
184
  end
174
-
185
+
175
186
  end
176
-
187
+
177
188
  Lita.register_handler(LinkLibrary)
178
189
  end
179
190
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-link-library"
3
- spec.version = "0.0.3"
3
+ spec.version = "0.1.0"
4
4
  spec.authors = ["Tudor Munteanu"]
5
5
  spec.email = ["the.tudor.munteanu@gmail.com"]
6
6
  spec.description = %q{A Lita handler that allows you manage a link library.}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-link-library
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tudor Munteanu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-25 00:00:00.000000000 Z
11
+ date: 2016-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -82,7 +82,6 @@ files:
82
82
  - lib/lita/handlers/library.json
83
83
  - lib/lita/handlers/library_entry.rb
84
84
  - lib/lita/handlers/link-library.rb
85
- - lita-link-library-0.0.1.gem
86
85
  - lita-link-library.gemspec
87
86
  homepage: https://github.com/sijiton/lita-link-library
88
87
  licenses:
@@ -105,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
104
  version: '0'
106
105
  requirements: []
107
106
  rubyforge_project:
108
- rubygems_version: 2.2.3
107
+ rubygems_version: 2.6.0
109
108
  signing_key:
110
109
  specification_version: 4
111
110
  summary: A Lita handler that allows you manage a link library.
Binary file