Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Enhancement/show available #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/cheatsheet
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ require "cheatsheet"

args = ARGV.clone

Cheatsheet::Client.fetch args
Cheatsheet::Client.start args
1 change: 1 addition & 0 deletions lib/cheatsheet.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "cheatsheet/version"
require "cheatsheet/client"
require "net/http"
require "json"

module Cheatsheet

Expand Down
53 changes: 48 additions & 5 deletions lib/cheatsheet/client.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@

module Cheatsheet
class Client

SOURCE = "https://raw.githubusercontent.com/rstacruz/cheatsheets/gh-pages/"
FILES_SOURCE = "https://api.github.com/repos/rstacruz/cheatsheets/contents/"

def self.start(*args)
begin
self.render(self.fetch(*args))
rescue CheatSheetClientException => e
self.render e
end
end

def self.fetch(*args)
key = args[0].first
uri = URI(SOURCE + key + ".md")

begin
puts self.fetch_raw(uri)
rescue CheatSheetClientException => e
puts e.message
# Show available cheatsheets
if (key === '-a')
files_uri = URI(FILES_SOURCE)
begin
files = JSON.parse(self.fetch_raw(files_uri))
filter = ''

if args[0].size > 1
filter = args[0].last
end

mds = files.select { |elem|
elem['name'].end_with?(".md") && elem['name'].include?(filter)
}.map { |elem|
File.basename(elem['name'],File.extname(elem['name']))
}

if mds.size === 0
raise CheatSheetClientException.new "We don't have any cheatsheet matching your search query"
end

return mds
rescue CheatSheetClientException => e
raise CheatSheetClientException.new e.message
rescue JSON::ParserError
raise CheatSheetClientException.new "Try again later"
end
else
uri = URI(SOURCE + key + ".md")
begin
return self.fetch_raw(uri)
rescue CheatSheetClientException => e
raise CheatSheetClientException.new e.message
end
end
end

Expand All @@ -27,6 +66,10 @@ def self.fetch_raw(uri)
end
end

def self.render(string)
puts string
end

end
end

Expand Down
16 changes: 14 additions & 2 deletions test/cheatsheet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ def test_that_it_has_a_version_number
end

def test_client_success
assert_match /Jollibee/, Cheatsheet::Client.fetch("ph-food-delivery")
assert_match (/Jollibee/), Cheatsheet::Client.fetch(["ph-food-delivery"])
end

def test_client_failure
assert_raises(CheatSheetClientException) { Cheatsheet::Client.fetch("us-food-delivery") }
assert_raises(CheatSheetClientException) { Cheatsheet::Client.fetch(["us-food-delivery"]) }
end

def test_client_search_finds
assert_includes Cheatsheet::Client.fetch(["-a", "xpath"]), 'xpath'
end

def test_client_search_doesnt_find
assert_raises(CheatSheetClientException) { Cheatsheet::Client.fetch(["-a", "xmen"]) }
end

def test_client_search_finds_correctly
refute_includes Cheatsheet::Client.fetch(["-a", "xpath"]), 'react'
end

def test_client_invalid_result
Expand Down