jitpipe 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/jitpipe.rb +104 -0
  2. data/test/test.rb +16 -0
  3. metadata +47 -0
@@ -0,0 +1,104 @@
1
+ require 'cgi'
2
+ require 'openssl'
3
+
4
+ class JitPipe
5
+ attr_writer :key, :secret, :account, :width, :height, :fit, :quality
6
+
7
+ @key = ''
8
+ @secret = ''
9
+ @account = ''
10
+
11
+ @width = 0
12
+ @height = 0
13
+ @fit = ''
14
+ @quality = ''
15
+
16
+
17
+ # config
18
+
19
+ def size(w, h)
20
+ check_size(w)
21
+ check_size(h)
22
+
23
+ jp = clone
24
+ jp.width = w
25
+ jp.height = h
26
+ jp
27
+ end
28
+
29
+ def low_quality
30
+ jp = clone
31
+ jp.quality = '/lo'
32
+ jp
33
+ end
34
+
35
+ def high_quality
36
+ jp = clone
37
+ jp.quality = '/hi'
38
+ jp
39
+ end
40
+
41
+ def default_quality
42
+ jp = clone
43
+ jp.quality = ''
44
+ jp
45
+ end
46
+
47
+ def crop
48
+ jp = clone
49
+ jp.fit = ''
50
+ jp
51
+ end
52
+
53
+ def fit
54
+ jp = clone
55
+ jp.fit = '/fit'
56
+ jp
57
+ end
58
+
59
+
60
+ # cred
61
+
62
+ def self.auth(cred)
63
+ self.new.auth(cred)
64
+ end
65
+
66
+ def auth(cred)
67
+ cred = parse_cred(cred) if cred.is_a? String
68
+ raise 'jitpipe: Missing key/secret/account.' if !(cred[:key] && cred[:secret] && cred[:account])
69
+
70
+ jp = clone
71
+ jp.key = cred[:key]
72
+ jp.secret = cred[:secret]
73
+ jp.account = cred[:account]
74
+ jp
75
+ end
76
+
77
+
78
+ # bakery
79
+
80
+ def url(src)
81
+ raise 'jitpipe: Credentials not set.' if !@key
82
+ raise 'jitpipe: Size not set.' if !@width
83
+
84
+ unsigned = "/v2/#{@account}/#{@key}/#{@width}x#{@height}#{@fit}#{@quality}/#{CGI::escape(src)}"
85
+ signature = OpenSSL::HMAC.hexdigest('md5', @secret, unsigned)
86
+
87
+ "http://jpeg.jitpipe.com#{unsigned}/#{signature}.jpg"
88
+ end
89
+
90
+
91
+ private
92
+
93
+ def check_size(x)
94
+ raise "jitpipe: bad size = #{x}" if !(x.is_a? Integer) || x < 16 || x > 1024
95
+ end
96
+
97
+ def parse_cred(str)
98
+ match = /([a-z]+):([a-f0-9]+)@([a-z0-9]+)/.match(str)
99
+ raise 'jitpipe: Invalid cred string.' if !match
100
+ { :key => match[1], :secret => match[2], :account => match[3] }
101
+ end
102
+
103
+ end
104
+
@@ -0,0 +1,16 @@
1
+ require 'test/unit'
2
+ require 'jitpipe'
3
+
4
+ class Basics < Test::Unit::TestCase
5
+
6
+ def test_sign
7
+ jitpipe = JitPipe \
8
+ .auth('myproject:0123456789abcdef0123456789abcdef@0123456789abcdef') \
9
+ .size(320, 320)
10
+
11
+ url = jitpipe.url('http://myserver.com/my-original-image.jpg')
12
+ assert_equal url,
13
+ 'http://jpeg.jitpipe.com/v2/0123456789abcdef/myproject/320x320/http%3A%2F%2Fmyserver.com%2Fmy-original-image.jpg/596a7cfe9a46f87c911d3c40d8af0268.jpg'
14
+ end
15
+
16
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jitpipe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - hdachev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-23 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Helper for building and signing JitPipe image urls
15
+ email:
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/jitpipe.rb
21
+ - test/test.rb
22
+ homepage: http://github.com/jitpipe/jitpipe-ruby
23
+ licenses:
24
+ - MIT
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.7.2
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: JitPipe
47
+ test_files: []