GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Gadget Server
Clone URL: git://github.com/CarlosGabaldon/gadget-server.git
Carlos Gabaldon (author)
Thu May 22 13:03:46 -0700 2008
gadget-server / gadget.rb
100644 235 lines (189 sloc) 5.829 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!ruby
 #!/usr/local/bin/ruby -rubygems
require 'camping'
require 'open-uri'
require 'rexml/document'
require 'uri'
 
#require 'memcache'
 
#= Setup
# $ sudo gem camping
#= Run
# $ cd ./gadget-server
# $ camping gadget.rb
# ..
# $ open http://0.0.0.0:3301/gadget?url=http://doc.examples.googlepages.com/magic-decoder.xml
# $ open http://0.0.0.0:3301/gadget_data?url=http://doc.examples.googlepages.com/magic-decoder.xml&inline=true
# $ open http://0.0.0.0:3301/gadget?url=http://doc.examples.googlepages.com/breakfast-menu.xml&mid=20
 
Camping.goes :Gadget
 
module Cache
  class Store
    class << self
      def put(url, value)
        file = "cache/#{keyify(url)}"
        File.open(file, 'w') do |f|
          f.write(value)
        end
      end
      
      def get(url)
        cache = ""
        file = "cache/#{keyify(url)}"
        return "" unless File.exist? file
        File.open(file, 'r') do |f|
          cache = f.read
        end
      end
      
      def keyify(url)
        uri = URI.parse(url)
        "#{uri.host}#{uri.path.tr('/', '_')}"
      end
    end
  end
end
 
 
module Template
  class Html
    class << self
      def build(content_data)
        content = <<-"CONTENT"
<html>
  <head>
  <style type="text/css"></style>
  </head>
  <body>
   <script src="http://www.google.com/ig/extern_js/f/CgJlbhICdXMrMAE4ACw/LuEUfb0hR1Q.js" />
   <script>
   function sendRequest(iframe_id, service_name, args_list, remote_relay_url,callback, local_relay_url)
   {
   _IFPC.call(iframe_id, service_name, args_list, remote_relay_url, callback,local_relay_url);
   }
var gv = gadgets.views;
gv.requestNavigateTo = gv.getCurrentView = gv.getParams = errFunc;
</script>
<script>_et="";_IG_Prefs._parseURL("0");</script>
<script>
_IG_Prefs._addAll("0", [["up_mycalories","800"],["up_mychoice","0"],["up_.lang","en"],["up_.country","us"],["up_synd","open"]]);
</script>
    <div style="border: 0pt none ; margin: 0pt; padding: 0pt; overflow: hidden; width: 100%; height: auto;">
    #{content_data}
    </div>
    <script>
_IG_TriggerEvent("domload");
</script>
  </body>
</html>
CONTENT
      end
    end
  end
end
 
module Gadget::Controllers
  
  class Server < R '/gadget_data'
     def get
       @url = @input[:url]
       @inline = @input[:inline]
       @nocache = @input[:nocache]
       
       #Hangman variables
       @module_id = @input[:mid]
       
       @content_data = ""
       @content = ""
       
       #1 Fetch content from cache
       @content = Cache::Store.get(@url) unless @nocache == "true"
       
       if @content == nil || @content == ""
         #2 Fetch the xml
         open(@url) do |file|
          @xml = file.read
         end
       
         #3 Parse the xml
         doc = REXML::Document.new(@xml)
         doc.elements.each('Module/Content') do |c|
            c.texts.each do |text|
              @content_data += text.to_s
            end
         end
       
         doc.elements.each('Module/ModulePrefs') do |c|
             @title = c.attributes["title"]
          end
       
       
        #3.1 Hangman variables
        @content_data = @content_data.sub('__MODULE_ID__', @module_id) unless @module_id == nil
       
        # TODO - parse via spec http://code.google.com/apis/gadgets/docs/spec.html#compliance
        # ...
        # ...
       
         #5 Cache the content
         Cache::Store.put(@url, @content_data)
         
         @content = @content_data
       end
       
       #6 Template the content
       unless @inline != nil && @inline == "true"
         @content = Template::Html.build(@content)
       end
       
       #7 Render the content
       if @url
         render :gadget_data
       else
         render :no_gadget
       end
     end
  end
  
  ### Creates a gadget ###
  class Widget < R '/gadget'
    def get
      @url = @input[:url]
      @nocache = @input[:nocache]
      @module_id = @input[:mid]
      @domain = "0.0.0.0:3301"
      #@domain = "10.8.9.35:3301"
      
      render :gadget
    end
  end
  
  class Json < R '/ig/jsonp' #'/ig/feedjson
    def get
      @url = @input[:url]
 
      @json = "throw 1; < don't be evil' >{'#{@url}' : { 'body' : '#{self.fetchXml(@url)}' ,'rc': 200 }}"
       
      render :json
    end
    
    def post
      @url = @input[:url]
      
      @json = "throw 1; < don't be evil' >{'#{@url}' : { 'body' : '#{self.fetchXml(@url)}' ,'rc': 200 }}"
       
      render :json
    end
    
    private
    def fetchXml(url)
      open(url) do |file|
        @xml = file.read
       end
       
       escaped = @xml.gsub('<', '\\x3c').gsub('>', '\\x3e').gsub('=', '\\x3d').gsub('"', '\\x22')
       
       data = ""
       
       escaped.each {|l| data += l.chomp}
       
       return data
    end
  end
 
  class Page < R '/(\w+)'
    def get(page_name)
      @p = page_name
      render :no_page
    end
  end
 
end
 
module Gadget::Views
 
  def gadget
    div :style => "width: 250px; height: 250px" do
      h4 @title
      iframe :src => "http://#{@domain}/gadget_data?url=#{@url}&nocache=#{@nocache}&mid=#{@module_id}",
        :frameborder => 0,
        :style => "border: 0pt none ; margin: 0pt; padding: 0pt; overflow: hidden; width: 100%; height: 100%;"
    end
  end
  
  def gadget_data
    @content
  end
  
  def json
    @json
  end
 
  def no_gadget
    h1 'No Gadget URL specified'
    h2 'Please pass /gadget?url={gadget path}'
  end
  
  def no_page
    h1 "Page '#{@p}' was not found!"
    h2 'Please verify that you have the correct URL'
  end
  
end