lita-link-library 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5f3499aa974e3120831ea886f7f2d85f55aee13b
4
+ data.tar.gz: 8cdcdb9d4436a59d255074e87727a7e797eb7971
5
+ SHA512:
6
+ metadata.gz: faf67df806d43563492f80e678a2e46b5a5dd12e42aa9ca45703e93150e09d16673f659c934e86bea421b3f2b187f55d10c2f0032df5c13109f97100d30b80d0
7
+ data.tar.gz: 4ae4bd573c62c934ed3caedad761828e6ec92d2709b094e62ee6bdb51d61d3a1d5ebd895663834e866eb95fa0d2e773b5a998eddcd957291224c6be539ec01c5
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ source "https://rubygems.org"
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2016 Tudor Munteanu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ ###A Lita handler that allows you manage a JSON link library.
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/lita-link-library.svg)](https://badge.fury.io/rb/lita-link-library)
4
+
5
+ ####List of commands:
6
+
7
+ - `random read` => Returns a random entry from the lita link library.
8
+
9
+ - `give me a gooder` => Returns top 3 entries from the lita link library.
10
+
11
+ - `list library` => Returns the entire lita link library.
12
+
13
+ - `want to read TITLE` => Returns the entry with that TITLE from the lita link library.
14
+
15
+
16
+ ######The link library itself can be managed by an authorized group of admins. This group is managed by a user defined via the `config.robot.admins` configuration attribute.
17
+
18
+ - `Lita auth add USER link_library_admins` => Adds USER as a link library admin.
19
+
20
+ - `Lita auth remove USER link_library_admins` => Removes USER from the link library admins group.
21
+
22
+ - `Lita auth list` => Displays all the current authorization groups and their members.
23
+
24
+
25
+ ####Commands restricted to library admins:
26
+
27
+ - `add read LINK TITLE DESCRIPTION` => Adds a new entry with that LINK, TITLE and DESCRIPTION attributes in the lita link library.
28
+
29
+ - `remove read TITLE` => Removes the entry with that TITLE from the lita link library.
30
+
31
+
32
+ #### License
33
+
34
+ [MIT](http://opensource.org/licenses/MIT)
@@ -0,0 +1,13 @@
1
+ # this class represents the Model for the entries available in the librarian.json
2
+
3
+ class Librarian
4
+
5
+ attr_accessor :username, :permissions
6
+
7
+ def initialize(*args)
8
+ #@id = args[0]
9
+ @username = args[0]
10
+ @permissions = args[1]
11
+ end
12
+
13
+ end
@@ -0,0 +1 @@
1
+ [{"username":"cam","permissions":"master"},{"username":"tudormi","permissions":"admin"}]
@@ -0,0 +1 @@
1
+ [{"link":"www.google.com","title":"google","description":"search engine","number_of_downloads":0}]
@@ -0,0 +1,14 @@
1
+ # this class represents the Model for the entries available in the library.json
2
+
3
+ class LibraryEntry
4
+
5
+ attr_accessor :link, :title, :description, :number_of_downloads
6
+
7
+ def initialize(*args)
8
+ @link = args[0]
9
+ @title = args[1]
10
+ @description = args[2]
11
+ @number_of_downloads = args[3]
12
+ end
13
+
14
+ end
@@ -0,0 +1,179 @@
1
+ #this class performs CRUD operations on the library file
2
+ require 'json'
3
+ require 'lita'
4
+ require_relative 'library_entry'
5
+
6
+ module Lita
7
+ module Handlers
8
+ class LinkLibrary < Handler
9
+
10
+ TITLE = /[\w\s\,\.\-\/:–]+/
11
+ LINK = /(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/
12
+ DESCRIPTION = /[\w\s\,\.\-\/:–]+/
13
+
14
+ 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.'}
19
+ )
20
+
21
+ 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.'}
26
+ )
27
+
28
+ route(
29
+ /^(list library)$/i,
30
+ :list_library_command,
31
+ command: true,
32
+ help: {'list library' => 'Returns entire lita link library.'}
33
+ )
34
+
35
+ 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
+
42
+ 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 '+
48
+ 'with that LINK, TITLE and DESCRIPTION attributes.'}
49
+ )
50
+ 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
+
59
+ def random_read_command(response)
60
+ response.reply random_read_entry
61
+ end
62
+
63
+ def read_best_entries_command(response)
64
+ response.reply read_best_entries
65
+ end
66
+
67
+ def list_library_command(response)
68
+ response.reply list_library
69
+ end
70
+
71
+ def want_to_read_command(response)
72
+ title = response.matches.first[0]
73
+ response.reply read_entry(title)
74
+ end
75
+
76
+ 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)
81
+ end
82
+
83
+ def remove_read_command(response)
84
+ title = response.matches.first[0]
85
+ response.reply delete_entry(title)
86
+ end
87
+
88
+ def file_writer (filename, entries)
89
+ open(filename, 'w') do |file|
90
+ file.truncate(0)
91
+ file.write entries.to_json
92
+ end
93
+ end
94
+
95
+ def save_new_entry (link, title, description) # allows a librarian to save a new entry in the library
96
+ file = File.read 'library.json'
97
+ hash_array = JSON.parse(file)
98
+ entry_exists = false
99
+ hash_array.each do |entry|
100
+ if entry.has_value?(title)
101
+ entry_exists = true
102
+ end
103
+ end
104
+ if entry_exists
105
+ return "That title already exists in the DB. Please choose another one"
106
+ else
107
+ new_entry = {"link" => link, "title" => title, "description" => description, "number_of_downloads" => 0}
108
+ hash_array.push new_entry
109
+ file_writer 'library.json', hash_array
110
+ return "The entry #{title} wass added in the library!"
111
+ end
112
+ end
113
+
114
+ def delete_entry (title) # allows a librarian to delete an entry from the library
115
+ file = File.read 'library.json'
116
+ hash_array = JSON.parse(file)
117
+ entries_counter = 0
118
+ hash_array.each do |entry|
119
+ if entry.has_value?(title)
120
+ hash_array.reject! {|entry| entry.has_value?(title)}
121
+ return "The entry with the title: #{title} was deleted"
122
+ file_writer 'library.json', hash_array
123
+ else
124
+ entries_counter +=1
125
+ end
126
+ end
127
+ if entries_counter==hash_array.length
128
+ response = "No entry with the title #{title} can be found in the library"
129
+ end
130
+ end
131
+
132
+ def read_entry (entry_title) # returns an entry with the specified title from the library
133
+ file = File.read 'library.json'
134
+ hash_array = JSON.parse(file)
135
+ entries_counter = 0
136
+ hash_array.each do |entry|
137
+ if entry.has_value?(entry_title)
138
+ return "#{entry_title} | #{entry['link']} | #{entry['number_of_downloads']} reads | #{entry['description']}"
139
+ entry['number_of_downloads']+=1
140
+ file_writer 'library.json', hash_array
141
+ else
142
+ entries_counter += 1
143
+ end
144
+ end
145
+ if entries_counter==hash_array.length
146
+ return "No entry entitled #{entry_title} can be found in the library DB!"
147
+ end
148
+ end
149
+
150
+ def random_read_entry # returns a random entry from the library
151
+ file = File.read 'library.json'
152
+ hash_array = JSON.parse(file)
153
+ entry = hash_array.sample
154
+ return "Random read: #{entry['title']} | #{entry['link']} | #{entry['number_of_downloads']} reads | #{entry['description']}"
155
+ end
156
+
157
+ def read_best_entries # returns the top 3 downloaded entries from the library
158
+ file = File.read 'library.json'
159
+ hash_array = JSON.parse(file)
160
+ hash_array.sort! {|a1, a2| a1['number_of_downloads'] <=> a2['number_of_downloads']}
161
+ 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"
163
+ end
164
+ end
165
+
166
+ def list_library # returns the entire library
167
+ file = File.read 'library.json'
168
+ hash_array = JSON.parse(file)
169
+ hash_array.each do |entry|
170
+ 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"
172
+ end
173
+ end
174
+
175
+ end
176
+
177
+ Lita.register_handler(LinkLibrary)
178
+ end
179
+ end
@@ -0,0 +1 @@
1
+ require "lita/handlers/link-library"
Binary file
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-link-library"
3
+ spec.version = "0.0.3"
4
+ spec.authors = ["Tudor Munteanu"]
5
+ spec.email = ["the.tudor.munteanu@gmail.com"]
6
+ spec.description = %q{A Lita handler that allows you manage a link library.}
7
+ spec.summary = %q{A Lita handler that allows you manage a link library.}
8
+ spec.homepage = "https://github.com/sijiton/lita-link-library"
9
+ spec.license = "[MIT]"
10
+ spec.metadata = { "lita_plugin_type" => "handler" }
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.require_paths = ["lib"]
15
+
16
+ spec.add_runtime_dependency "lita", ">= 2.7"
17
+
18
+ spec.add_development_dependency "simplecov"
19
+ spec.add_development_dependency "coveralls"
20
+ spec.add_dependency "json"
21
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-link-library
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Tudor Munteanu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lita
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: simplecov
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A Lita handler that allows you manage a link library.
70
+ email:
71
+ - the.tudor.munteanu@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - Gemfile
77
+ - LICENSE
78
+ - README.md
79
+ - lib/lita-link-library.rb
80
+ - lib/lita/handlers/librarian.rb
81
+ - lib/lita/handlers/librarians.json
82
+ - lib/lita/handlers/library.json
83
+ - lib/lita/handlers/library_entry.rb
84
+ - lib/lita/handlers/link-library.rb
85
+ - lita-link-library-0.0.1.gem
86
+ - lita-link-library.gemspec
87
+ homepage: https://github.com/sijiton/lita-link-library
88
+ licenses:
89
+ - "[MIT]"
90
+ metadata:
91
+ lita_plugin_type: handler
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A Lita handler that allows you manage a link library.
112
+ test_files: []