domr 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/lib/domr.rb +15 -15
- metadata +1 -1
data/Gemfile
CHANGED
data/lib/domr.rb
CHANGED
@@ -2,10 +2,9 @@ require 'rainbow'
|
|
2
2
|
require 'uri'
|
3
3
|
require 'net/http'
|
4
4
|
require 'json'
|
5
|
-
|
5
|
+
|
6
6
|
# The main class
|
7
7
|
class Domr
|
8
|
-
|
9
8
|
# Perform a query on domainr API
|
10
9
|
#
|
11
10
|
# Example:
|
@@ -18,33 +17,34 @@ class Domr
|
|
18
17
|
# Arguments:
|
19
18
|
# query: (String)
|
20
19
|
def self.search query
|
21
|
-
|
20
|
+
|
22
21
|
# Form the request string
|
23
22
|
request = Net::HTTP::Get.new("/api/json/search?q=" << URI.escape(query))
|
24
|
-
|
23
|
+
|
25
24
|
# Perform the actual query
|
26
|
-
response = Net::HTTP.start("domai.nr")
|
25
|
+
response = Net::HTTP.start("domai.nr") do |http|
|
27
26
|
http.request request
|
28
|
-
|
29
|
-
|
27
|
+
end
|
28
|
+
|
30
29
|
# Check for errors
|
31
30
|
if response.code != "200"
|
32
|
-
puts "HTTP error".color(:red).bright
|
31
|
+
puts "HTTP error".color(:red).bright
|
33
32
|
return false
|
34
33
|
end
|
35
|
-
|
34
|
+
|
36
35
|
# Output results
|
37
36
|
JSON.parse(response.body)['results'].each do |result|
|
38
37
|
|
39
38
|
# Determine color to show
|
40
|
-
|
39
|
+
case result['availability']
|
40
|
+
when 'available'
|
41
41
|
color = :green
|
42
|
-
|
43
|
-
color = :
|
44
|
-
|
42
|
+
when 'taken', 'unavailable'
|
43
|
+
color = :default
|
44
|
+
when 'maybe'
|
45
45
|
color = :yellow
|
46
46
|
else
|
47
|
-
|
47
|
+
color = :default
|
48
48
|
end
|
49
49
|
|
50
50
|
# Construct final output string
|
@@ -53,7 +53,7 @@ class Domr
|
|
53
53
|
# Output colorized string
|
54
54
|
puts string.color(color).bright
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
return true
|
58
58
|
end
|
59
59
|
end
|