inkit 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/cache.rb +30 -0
- data/lib/inkit.rb +86 -0
- data/test/Gemfile +7 -0
- data/test/Gemfile.lock +39 -0
- data/test/config.ru +14 -0
- metadata +63 -0
data/lib/cache.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
class Inkit
|
2
|
+
class Cache
|
3
|
+
def initialize(secret)
|
4
|
+
@secret = secret
|
5
|
+
@cache_dir = './.ink-cache'
|
6
|
+
Dir.mkdir @cache_dir unless Dir.exists? @cache_dir
|
7
|
+
@cached_at = {}
|
8
|
+
end
|
9
|
+
def []=(name,data)
|
10
|
+
File.open( filename(name), "w+") do |io|
|
11
|
+
io.write data
|
12
|
+
end
|
13
|
+
end
|
14
|
+
def [](name)
|
15
|
+
File.read filename(name)
|
16
|
+
end
|
17
|
+
def cached_at(name)
|
18
|
+
File.stat(filename(name)).mtime.rfc2822
|
19
|
+
end
|
20
|
+
def cached?(name)
|
21
|
+
File.exists? filename(name)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def filename(name)
|
27
|
+
@cache_dir+"/."+OpenSSL::HMAC::hexdigest("sha256",@secret,name.to_s)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/inkit.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "uri"
|
3
|
+
require 'cgi'
|
4
|
+
require 'mustache'
|
5
|
+
require 'time'
|
6
|
+
|
7
|
+
load File.dirname(__FILE__)+"/cache.rb"
|
8
|
+
|
9
|
+
class Hash
|
10
|
+
def to_query
|
11
|
+
map{|k,v| "#{::CGI.escape(k.to_s)}=#{::CGI.escape(v)}"}.join("&")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class String
|
16
|
+
def indent(n)
|
17
|
+
if n >= 0
|
18
|
+
gsub(/^/, ' ' * n)
|
19
|
+
else
|
20
|
+
gsub(/^ {0,#{-n}}/, "")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Inkit
|
26
|
+
|
27
|
+
attr_accessor :token
|
28
|
+
|
29
|
+
# Constructor
|
30
|
+
def initialize(options)
|
31
|
+
raise 'Please provide your secret key!' unless options[:secret]
|
32
|
+
@secret = options[:secret].to_s
|
33
|
+
@token = options[:token].to_s
|
34
|
+
@cache = Cache.new(@secret)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Pull a view from the inkit api
|
38
|
+
def digest(hash)
|
39
|
+
OpenSSL::HMAC::hexdigest("sha256", @secret, hash.to_query)
|
40
|
+
end
|
41
|
+
|
42
|
+
def pull(view,type = 'haml')
|
43
|
+
data = {:view => view.to_s, :type => type}
|
44
|
+
data[:digest] = digest(data)
|
45
|
+
data[:token] = @token
|
46
|
+
uri = URI("http://inkit.org/api/document?"+data.to_query)
|
47
|
+
req = ::Net::HTTP::Get.new uri.request_uri
|
48
|
+
if @cache.cached? view
|
49
|
+
req['If-Modified-Since'] = @cache.cached_at view
|
50
|
+
end
|
51
|
+
res = ::Net::HTTP.start(uri.host, uri.port) {|http|
|
52
|
+
http.request(req)
|
53
|
+
}
|
54
|
+
if res.is_a?(::Net::HTTPSuccess)
|
55
|
+
@cache[view] = res.body
|
56
|
+
return @cache[view]
|
57
|
+
end
|
58
|
+
if res.is_a?(::Net::HTTPNotModified)
|
59
|
+
return @cache[view]
|
60
|
+
end
|
61
|
+
raise res.code
|
62
|
+
end
|
63
|
+
|
64
|
+
def html(view, options = {})
|
65
|
+
render(view, options)
|
66
|
+
end
|
67
|
+
|
68
|
+
def haml(view, options = {})
|
69
|
+
render(view, options, 'haml')
|
70
|
+
end
|
71
|
+
|
72
|
+
def render(view, options = {}, type = 'html')
|
73
|
+
ret = self.pull(view,type)
|
74
|
+
if options[:layout]
|
75
|
+
layout = render(options[:layout], {}, type)
|
76
|
+
indent = 0
|
77
|
+
if layout =~ /( *)\{{3}yield\}{3}/
|
78
|
+
indent = $1.length
|
79
|
+
end
|
80
|
+
ret = "\n#{ret}"
|
81
|
+
ret = Mustache.render(layout,:yield => Mustache.render(ret.indent(indent)))
|
82
|
+
end
|
83
|
+
ret.gsub /\s*$/, ''
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
data/test/Gemfile
ADDED
data/test/Gemfile.lock
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
GIT
|
2
|
+
remote: git://github.com/gdotdesign/renee.git
|
3
|
+
revision: 86aae1601e90b243222337c9ed94da1bf62d9791
|
4
|
+
specs:
|
5
|
+
renee (0.3.2)
|
6
|
+
renee-core (= 0.3.2)
|
7
|
+
renee-render (= 0.3.2)
|
8
|
+
renee-core (0.3.2)
|
9
|
+
rack (~> 1.3.0)
|
10
|
+
renee-render (0.3.2)
|
11
|
+
callsite (~> 0.0.6)
|
12
|
+
rack (~> 1.3.0)
|
13
|
+
renee-core (= 0.3.2)
|
14
|
+
tilt (~> 1.3.3)
|
15
|
+
|
16
|
+
GEM
|
17
|
+
remote: http://rubygems.org/
|
18
|
+
specs:
|
19
|
+
callsite (0.0.11)
|
20
|
+
faker (1.0.1)
|
21
|
+
i18n (~> 0.4)
|
22
|
+
haml (3.1.4)
|
23
|
+
i18n (0.6.0)
|
24
|
+
json (1.6.5)
|
25
|
+
mustache (0.99.4)
|
26
|
+
rack (1.3.6)
|
27
|
+
sass (3.1.15)
|
28
|
+
tilt (1.3.3)
|
29
|
+
|
30
|
+
PLATFORMS
|
31
|
+
ruby
|
32
|
+
|
33
|
+
DEPENDENCIES
|
34
|
+
faker
|
35
|
+
haml
|
36
|
+
json
|
37
|
+
mustache
|
38
|
+
renee!
|
39
|
+
sass
|
data/test/config.ru
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require 'cgi'
|
3
|
+
require '../lib/inkit'
|
4
|
+
|
5
|
+
|
6
|
+
ink = Inkit.new({
|
7
|
+
:token => "egxM28qoFkP",
|
8
|
+
:secret => "QntGyT3P00T"
|
9
|
+
})
|
10
|
+
|
11
|
+
run lambda { |env|
|
12
|
+
[200, {"Content-Type" => "text/plain"}, [ink.haml(:asd,{:layout => :test01})]]
|
13
|
+
}
|
14
|
+
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: inkit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Szikszai Gusztáv
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mustache
|
16
|
+
requirement: &81191750 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *81191750
|
25
|
+
description: Client library to pull and render INK views.
|
26
|
+
email: ink-support@hotmail.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- ./lib/inkit.rb
|
32
|
+
- ./lib/cache.rb
|
33
|
+
- test/Gemfile.lock
|
34
|
+
- test/Gemfile
|
35
|
+
- test/config.ru
|
36
|
+
homepage: http://inkit.org
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- ./
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.17
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Client library for INK.
|
60
|
+
test_files:
|
61
|
+
- test/Gemfile.lock
|
62
|
+
- test/Gemfile
|
63
|
+
- test/config.ru
|