respond with actual JSON

okor 10 年 前
コミット
4be839c472
共有3 個のファイルを変更した70 個の追加25 個の削除を含む
  1. 1 0
      Gemfile
  2. 12 0
      Gemfile.lock
  3. 57 25
      main.rb

+ 1 - 0
Gemfile

@@ -2,6 +2,7 @@ source 'http://rubygems.org'
2 2
 
3 3
 gem 'unicorn'
4 4
 gem 'sinatra'
5
+gem 'sinatra-contrib'
5 6
 gem 'whois'
6 7
 gem 'haml'
7 8
 gem 'json'

+ 12 - 0
Gemfile.lock

@@ -1,7 +1,9 @@
1 1
 GEM
2 2
   remote: http://rubygems.org/
3 3
   specs:
4
+    backports (3.6.4)
4 5
     coderay (1.1.0)
6
+    eventmachine (1.0.6)
5 7
     haml (3.1.6)
6 8
     json (1.7.3)
7 9
     kgio (2.7.4)
@@ -13,12 +15,21 @@ GEM
13 15
     rack (1.4.1)
14 16
     rack-protection (1.2.0)
15 17
       rack
18
+    rack-test (0.6.3)
19
+      rack (>= 1.0)
16 20
     raindrops (0.10.0)
17 21
     rb-readline (0.5.2)
18 22
     sinatra (1.3.2)
19 23
       rack (~> 1.3, >= 1.3.6)
20 24
       rack-protection (~> 1.2)
21 25
       tilt (~> 1.3, >= 1.3.3)
26
+    sinatra-contrib (1.3.2)
27
+      backports (>= 2.0)
28
+      eventmachine
29
+      rack-protection
30
+      rack-test
31
+      sinatra (~> 1.3.0)
32
+      tilt (~> 1.3)
22 33
     slop (3.6.0)
23 34
     tilt (1.3.3)
24 35
     unicorn (4.3.1)
@@ -36,5 +47,6 @@ DEPENDENCIES
36 47
   pry
37 48
   rb-readline
38 49
   sinatra
50
+  sinatra-contrib
39 51
   unicorn
40 52
   whois

+ 57 - 25
main.rb

@@ -3,48 +3,80 @@ require 'whois'
3 3
 require 'haml'
4 4
 require 'json'
5 5
 require 'ostruct'
6
+require "sinatra/reloader" if development?
7
+
8
+# ruby crimes
9
+class Whois::Record
10
+  def to_h
11
+    who_dat_hash = {}
12
+    self.properties.each_pair do |k, v|
13
+      if v.is_a?(Array)
14
+        who_dat_hash[k.to_s] = []
15
+        v.each do |m|
16
+          if m.respond_to?(:members)
17
+            who_dat_hash[k.to_s].push( Hash[m.members.zip(m.to_a)] )
18
+          else
19
+            if m.split(' ').length > 1
20
+              who_dat_hash[k.to_s].push( [m.split(' ')].to_h )
21
+            else
22
+              who_dat_hash[k.to_s].push(m)
23
+            end
24
+          end
25
+        end
26
+      elsif v.respond_to?(:members)
27
+        who_dat_hash[k.to_s] = Hash[v.members.zip(v.to_a)]
28
+      else
29
+        who_dat_hash[k.to_s] = v
30
+      end
31
+    end
32
+    who_dat_hash
33
+  end
34
+end
6 35
 
7 36
 before do
8
-	response['Access-Control-Allow-Origin'] = '*'
37
+  response['Access-Control-Allow-Origin'] = '*'
9 38
 end
10 39
 
11 40
 helpers do
41
+  def whois_structs_hash(whois)
42
+    who_dat_is = whois.clone
43
+    who_dat_is.properties.each do |p|
44
+      # if
45
+    end
46
+  end
12 47
 
13 48
   def cache_for_day
14 49
     response['Cache-Control'] = 'public, max-age=86400'
15 50
   end
16 51
 
17
-	def whois_lookup
18
-		lookup_info = Whois.query(params[:url])
19
-	end
20
-
52
+  def whois_lookup
53
+    lookup_info = Whois.query(params[:url])
54
+  end
21 55
 end
22 56
 
23
-
24 57
 get '/' do
25
-	# cache_for_day
26
-	haml :index
58
+  cache_for_day
59
+  haml :index
27 60
 end
28 61
 
29
-
30 62
 get '/lookup' do
31
-	begin
32
-		cache_for_day
33
-		@whois = whois_lookup
34
-		haml :lookup
35
-	rescue Exception => e
36
-		@error = e
37
-		haml :error
38
-	end
63
+  begin
64
+    cache_for_day
65
+    @whois = whois_lookup
66
+    haml :lookup
67
+  rescue Exception => e
68
+    @error = e
69
+    haml :error
70
+  end
39 71
 end
40 72
 
41
-
42 73
 get '/lookup.json' do
43
-	begin
44
-		cache_for_day
45
-		whois_lookup.to_s.force_encoding('utf-8').encode.to_json
46
-	rescue Exception => e
47
-		@error = e
48
-		{:Error => @error}.to_json
49
-	end
74
+  content_type 'application/json'
75
+  begin
76
+    cache_for_day
77
+    JSON.pretty_generate(whois_lookup.to_h)
78
+  rescue Exception => e
79
+    @error = e
80
+    { :error => @error }.to_json
81
+  end
50 82
 end