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