hintme 0.1.0 → 0.1.1

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: a49a674b3a187d6bb9f8007074d60efd854cffe6
4
- data.tar.gz: f5e0507426b7833e2276e6f469c336ac468c5a7f
3
+ metadata.gz: da1266243ffffddb17e42321392746cf5e67bfb1
4
+ data.tar.gz: ea29387d3e21e8157e0811f3e37fd4a8e70e5703
5
5
  SHA512:
6
- metadata.gz: 6c445799017c6b1f471a11a7cba0e06170aec268f7821e31389625e8edca719878b1fab9e9acc3b6705b9711d80ee4cd1c57135f8cc4669b6f82ad7f6a23dc51
7
- data.tar.gz: ae29633b53eba1010b52e50314c7573ab8c8c9f616882f42c5e73fd8d35f42493c9447ba2a4df6123a820c10d38912be99449dd816b281395cbee444d9c46258
6
+ metadata.gz: 3116a7b1bd76f8ded3728c962c121c4b33e44d9b599227afe482f283aa150500bfa2c240cb8fd6d7c21ec5a52682821454e7c4f4b8cb6798c846900e64974986
7
+ data.tar.gz: 9f2b82f4d70be720ddfd9f2980777c1f1bb81817f33134c45d5acbb1f2811a7254e021d1ceec2ab601a42ce72d5028c1d404370f82e75c37ceced0661d16b154
data/README.md CHANGED
@@ -1,25 +1,22 @@
1
1
  # Hintme
2
-
2
+ The stupid, simple [Devhints.io](http://devhints.io/) CLI
3
3
 
4
4
  ## Installation
5
5
 
6
- Add this line to your application's Gemfile:
6
+ Ensure you've installed ruby (If you're using OS X/macOS you should be fine)
7
7
 
8
- ```ruby
9
- gem 'hintme'
10
- ```
8
+ install hintme onto your system
11
9
 
12
- And then execute:
10
+ $ gem install hintme
13
11
 
14
- $ bundle
15
12
 
16
- Or install it yourself as:
13
+ Ask for hints:
17
14
 
18
- $ gem install hintme
15
+ $ hintme rails
19
16
 
20
17
  ## Contributing
21
18
 
22
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hintme.
19
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jcambass/hintme
23
20
 
24
21
  ## License
25
22
 
@@ -1,14 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'hintme/version'
2
- require 'hintme/api/client'
3
- require 'hintme/markdown_printer'
4
+ require 'hintme/hinter'
4
5
 
6
+ # HintMe Gem toplevel module
5
7
  module HintMe
8
+ # searches an prints a cheatsheet for a given
9
+ # topic to the console
10
+ #
11
+ # @param topic [String]
12
+ #
6
13
  def self.hint(topic)
7
- markdown = API::Client.new.fetch_topic(topic)
8
- if markdown
9
- MarkdownPrinter.new.print(markdown)
10
- else
11
- puts ":( There isn't a cheatsheet for #{topic}. Maybe you should request one at devhints.io"
12
- end
14
+ Hinter.new.hint(topic)
13
15
  end
14
16
  end
@@ -1,28 +1,67 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'uri'
2
4
  require 'net/http'
3
5
  require 'net/https'
4
6
 
5
7
  module HintMe
6
8
  module API
9
+ # Client for the DevHints API
7
10
  class Client
11
+ # Base URL for the DevHints cheatsheets github repository
8
12
  RAW_REPO_URL = 'https://raw.githubusercontent.com/rstacruz/cheatsheets/master/'
9
13
 
10
- def fetch_topic(topic)
11
- uri = topic_url(topic)
14
+ # searches and downloads the raw markdown representation of cheatsheet
15
+ #
16
+ # @param topic [String] topic to fetch cheatsheet for
17
+ #
18
+ def search_cheatsheet_for_topic(topic)
19
+ download_cheatsheet(topic) if cheatsheet_exists?(topic)
20
+ end
21
+
22
+ private
23
+
24
+ # checks if a cheatsheet for a certain topic exists
25
+ #
26
+ # @param topic [String]
27
+ # @return [true, false]
28
+ #
29
+ def cheatsheet_exists?(topic)
30
+ url = build_topic_url(topic)
31
+ response = Net::HTTP.get_response(url)
12
32
 
13
- case Net::HTTP.get_response(uri)
14
- when Net::HTTPSuccess then Net::HTTP.get(uri).force_encoding('utf-8')
15
- when Net::HTTPNotFound then nil
33
+ case response
34
+ when Net::HTTPSuccess then true
35
+ when Net::HTTPNotFound then false
16
36
  else
17
- response.value
37
+ handle_invalid_response(response)
18
38
  end
19
39
  end
20
40
 
21
- private
41
+ # downloads a cheatsheet for a given topic
42
+ #
43
+ # @param topic [String]
44
+ # @return [String, nil] downloaded cheatsheet
45
+ #
46
+ def download_cheatsheet(topic)
47
+ url = build_topic_url(topic)
48
+ Net::HTTP.get(url)
49
+ .force_encoding('utf-8')
50
+ end
22
51
 
23
- def topic_url(topic)
52
+ # builds the full repository url for a specific cheatsheet
53
+ #
54
+ # @param topic [String] topic for which the full cheatsheet url
55
+ # should be build
56
+ #
57
+ def build_topic_url(topic)
24
58
  URI("#{RAW_REPO_URL}#{topic}.md")
25
59
  end
60
+
61
+ def handle_invalid_response(response)
62
+ raise StandartError, 'An network error occured.' \
63
+ "Error response: #{response.value}"
64
+ end
26
65
  end
27
66
  end
28
67
  end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tty/markdown'
4
+
5
+ module HintMe
6
+ module Cheatsheet
7
+ # prints a cheatsheet to the console
8
+ class Printer
9
+ # prints the content of a cheatsheet on the console
10
+ #
11
+ # @param cheatsheet [String]
12
+ #
13
+ def print(cheatsheet)
14
+ puts ::TTY::Markdown.parse(cheatsheet)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api/client'
4
+ require_relative 'cheatsheet/printer'
5
+
6
+ module HintMe
7
+ # entry point for all HintMe functionality
8
+ class Hinter
9
+ # creates a new {Hinter} instance
10
+ #
11
+ def initialize
12
+ @api_client = API::Client.new
13
+ @cheatsheet_printer = Cheatsheet::Printer.new
14
+ end
15
+
16
+ # searches and prints a cheatsheet for a given topic
17
+ # to the console
18
+ #
19
+ # if no cheatsheet is found for the given topic
20
+ # a error message will be printed to the console
21
+ #
22
+ # @param topic [String]
23
+ #
24
+ def hint(topic)
25
+ cheatsheet = cheatsheet_for_topic(topic)
26
+
27
+ if cheatsheet
28
+ print_cheatsheet(cheatsheet)
29
+ else
30
+ display_missing_cheatsheet_error(topic)
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ # fetches the cheatsheet for a topic
37
+ #
38
+ # @param topic [String]
39
+ # @return [String]
40
+ #
41
+ def cheatsheet_for_topic(topic)
42
+ @api_client.search_cheatsheet_for_topic(topic)
43
+ end
44
+
45
+ # prints a cheatsheet to the console
46
+ #
47
+ # @param cheatsheet [String]
48
+ #
49
+ def print_cheatsheet(cheatsheet)
50
+ @cheatsheet_printer.print(cheatsheet)
51
+ end
52
+
53
+ # displays a error message informing the user about
54
+ # the missing cheatsheet to the console
55
+ #
56
+ def display_missing_cheatsheet_error(topic)
57
+ topic ||= 'escaping the void of notopic-island'
58
+
59
+ puts ":( There isn't a cheatsheet for #{topic}." \
60
+ ' Maybe you should request one at devhints.io'
61
+ end
62
+ end
63
+ end
@@ -1,3 +1,3 @@
1
1
  module HintMe
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hintme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Ambass
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-03 00:00:00.000000000 Z
11
+ date: 2018-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,7 +88,8 @@ files:
88
88
  - hintme.gemspec
89
89
  - lib/hintme.rb
90
90
  - lib/hintme/api/client.rb
91
- - lib/hintme/markdown_printer.rb
91
+ - lib/hintme/cheatsheet/printer.rb
92
+ - lib/hintme/hinter.rb
92
93
  - lib/hintme/version.rb
93
94
  homepage: https://github.com/jcambass/hintme
94
95
  licenses:
@@ -1,10 +0,0 @@
1
- require 'tty/markdown'
2
-
3
- module HintMe
4
- class MarkdownPrinter
5
-
6
- def print(markdown)
7
- puts ::TTY::Markdown.parse(markdown)
8
- end
9
- end
10
- end