social-avatar-proxy 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -7
- data/lib/social_avatar_proxy/app.rb +6 -1
- data/lib/social_avatar_proxy/path_helpers.rb +27 -0
- data/lib/social_avatar_proxy/routes.rb +13 -0
- data/lib/social_avatar_proxy/version.rb +1 -1
- data/spec/social_avatar_proxy/app_spec.rb +9 -0
- data/spec/social_avatar_proxy/path_helpers_spec.rb +62 -0
- data/spec/social_avatar_proxy/routes_spec.rb +25 -0
- metadata +10 -4
data/README.md
CHANGED
@@ -30,26 +30,26 @@ This usage is based on Rails, however it should be easily adaptable for other la
|
|
30
30
|
In your `config/routes.rb` file:
|
31
31
|
|
32
32
|
```ruby
|
33
|
-
mount SocialAvatarProxy::App, at: "/avatars"
|
33
|
+
mount SocialAvatarProxy::App, at: "/avatars", as: :sap
|
34
34
|
```
|
35
35
|
|
36
|
-
|
36
|
+
Note: you must specify an `as` option above, otherwise the path helpers below won't work.
|
37
37
|
|
38
38
|
In your views:
|
39
39
|
|
40
40
|
```ruby
|
41
41
|
# for a Twitter user, by username:
|
42
|
-
image_tag(twitter_avatar_path("username"))
|
42
|
+
image_tag(sap.twitter_avatar_path("username"))
|
43
43
|
# by ID:
|
44
|
-
image_tag(twitter_avatar_path(12345))
|
44
|
+
image_tag(sap.twitter_avatar_path(12345))
|
45
45
|
|
46
46
|
# for a Facebook user, by username:
|
47
|
-
image_tag(facebook_avatar_path("username"))
|
47
|
+
image_tag(sap.facebook_avatar_path("username"))
|
48
48
|
# by ID:
|
49
|
-
image_tag(facebook_avatar_path(12345))
|
49
|
+
image_tag(sap.facebook_avatar_path(12345))
|
50
50
|
```
|
51
51
|
|
52
|
-
The above helper methods are provided by the `SocialAvatarProxy::
|
52
|
+
The above path helper methods are provided by the `SocialAvatarProxy::PathHelpers` module. Note: `image_tag` is a Rails helper, replace with your framework's equivalent. You'll need to call `avatar_base_path=` with the mounted path prefix (in the example above we were mounted at `/avatars`, however Rails handles it automatically)
|
53
53
|
|
54
54
|
## Contributing
|
55
55
|
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require "social_avatar_proxy/facebook_avatar"
|
2
2
|
require "social_avatar_proxy/twitter_avatar"
|
3
|
+
require "social_avatar_proxy/routes"
|
3
4
|
require "rack"
|
4
5
|
|
5
6
|
module SocialAvatarProxy
|
@@ -28,7 +29,7 @@ module SocialAvatarProxy
|
|
28
29
|
end
|
29
30
|
|
30
31
|
def path_prefix
|
31
|
-
request.env["SCRIPT_NAME"].gsub(/\/$/, "")
|
32
|
+
request && request.env["SCRIPT_NAME"].gsub(/\/$/, "")
|
32
33
|
end
|
33
34
|
|
34
35
|
def response
|
@@ -72,6 +73,10 @@ module SocialAvatarProxy
|
|
72
73
|
Rack::Response.new("Not Found", 404)
|
73
74
|
end
|
74
75
|
|
76
|
+
def routes
|
77
|
+
@routes ||= Routes.new(self)
|
78
|
+
end
|
79
|
+
|
75
80
|
private
|
76
81
|
def load_avatar(service, id)
|
77
82
|
# titleize the service name
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module SocialAvatarProxy
|
2
|
+
module PathHelpers
|
3
|
+
def avatar_base_path
|
4
|
+
@avatar_base_path ||= ""
|
5
|
+
end
|
6
|
+
|
7
|
+
def avatar_base_path=(path)
|
8
|
+
# strip leading / trailing slashes
|
9
|
+
path = path.gsub(/^\/|\/$/, "")
|
10
|
+
if path.empty?
|
11
|
+
# reset to an empty string
|
12
|
+
@avatar_base_path = ""
|
13
|
+
else
|
14
|
+
# set with prepended slash
|
15
|
+
@avatar_base_path = "/#{path}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def twitter_avatar_path(identifier)
|
20
|
+
File.join(avatar_base_path, "twitter", identifier.to_s)
|
21
|
+
end
|
22
|
+
|
23
|
+
def facebook_avatar_path(identifier)
|
24
|
+
File.join(avatar_base_path, "facebook", identifier.to_s)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -37,6 +37,15 @@ describe SocialAvatarProxy::App do
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
+
describe "#routes" do
|
41
|
+
subject { app }
|
42
|
+
|
43
|
+
it "should return a object providing the path helpers" do
|
44
|
+
expect(subject.routes.facebook_avatar_path("ryandtownsned")).to match(/^\/.*$/)
|
45
|
+
expect(subject.routes.twitter_avatar_path(2202971)).to match(/^\/.*$/)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
40
49
|
context "given an invalid path" do
|
41
50
|
it "should return a 404 response" do
|
42
51
|
path = "/unknown"
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe SocialAvatarProxy::PathHelpers do
|
4
|
+
subject do
|
5
|
+
Class.new do
|
6
|
+
include SocialAvatarProxy::PathHelpers
|
7
|
+
end.new
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#avatar_base_path" do
|
11
|
+
it "should return empty string as default" do
|
12
|
+
expect(subject.avatar_base_path).to eq("")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#avatar_base_path=" do
|
17
|
+
it "should update the #avatar_base_path" do
|
18
|
+
subject.avatar_base_path = "/test"
|
19
|
+
expect(subject.avatar_base_path).to eq("/test")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should remove trailing slashes" do
|
23
|
+
subject.avatar_base_path = "/test/"
|
24
|
+
expect(subject.avatar_base_path).to eq("/test")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should prepend a slash if missing" do
|
28
|
+
subject.avatar_base_path = "test"
|
29
|
+
expect(subject.avatar_base_path).to eq("/test")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#twitter_avatar_path" do
|
34
|
+
context "with no #avatar_base_path" do
|
35
|
+
it "should join the service and identifier" do
|
36
|
+
expect(subject.twitter_avatar_path("dave")).to eq "/twitter/dave"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "with a custom #avatar_base_path" do
|
41
|
+
it "should join the base path, service and identifier" do
|
42
|
+
subject.avatar_base_path = "avatars"
|
43
|
+
expect(subject.twitter_avatar_path("dave")).to eq "/avatars/twitter/dave"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "#facebook_avatar_path" do
|
49
|
+
context "with no #avatar_base_path" do
|
50
|
+
it "should join the service and identifier" do
|
51
|
+
expect(subject.facebook_avatar_path("dave")).to eq "/facebook/dave"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "with a custom #avatar_base_path" do
|
56
|
+
it "should join the base path, service and identifier" do
|
57
|
+
subject.avatar_base_path = "avatars"
|
58
|
+
expect(subject.facebook_avatar_path("dave")).to eq "/avatars/facebook/dave"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe SocialAvatarProxy::Routes do
|
4
|
+
subject do
|
5
|
+
SocialAvatarProxy::Routes.new(app)
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:path_prefix) { "" }
|
9
|
+
let(:app) { mock("app", path_prefix: path_prefix) }
|
10
|
+
|
11
|
+
describe "#avatar_base_path" do
|
12
|
+
context "without a path_prefix" do
|
13
|
+
it "should return empty string" do
|
14
|
+
expect(subject.avatar_base_path).to eq("")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "with a path_prefix" do
|
19
|
+
let(:path_prefix) { "/avatars" }
|
20
|
+
it "should return the path prefix" do
|
21
|
+
expect(subject.avatar_base_path).to eq("/avatars")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: social-avatar-proxy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -110,14 +110,18 @@ files:
|
|
110
110
|
- lib/social_avatar_proxy/app.rb
|
111
111
|
- lib/social_avatar_proxy/avatar.rb
|
112
112
|
- lib/social_avatar_proxy/facebook_avatar.rb
|
113
|
+
- lib/social_avatar_proxy/path_helpers.rb
|
113
114
|
- lib/social_avatar_proxy/remote_file_resolver.rb
|
115
|
+
- lib/social_avatar_proxy/routes.rb
|
114
116
|
- lib/social_avatar_proxy/twitter_avatar.rb
|
115
117
|
- lib/social_avatar_proxy/version.rb
|
116
118
|
- social-avatar-proxy.gemspec
|
117
119
|
- spec/social_avatar_proxy/app_spec.rb
|
118
120
|
- spec/social_avatar_proxy/avatar_spec.rb
|
119
121
|
- spec/social_avatar_proxy/facebook_avatar_spec.rb
|
122
|
+
- spec/social_avatar_proxy/path_helpers_spec.rb
|
120
123
|
- spec/social_avatar_proxy/remote_file_resolver_spec.rb
|
124
|
+
- spec/social_avatar_proxy/routes_spec.rb
|
121
125
|
- spec/social_avatar_proxy/twitter_avatar_spec.rb
|
122
126
|
- spec/spec_helper.rb
|
123
127
|
- spec/support/env.rb
|
@@ -136,7 +140,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
136
140
|
version: '0'
|
137
141
|
segments:
|
138
142
|
- 0
|
139
|
-
hash:
|
143
|
+
hash: -420987875933585136
|
140
144
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
145
|
none: false
|
142
146
|
requirements:
|
@@ -145,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
145
149
|
version: '0'
|
146
150
|
segments:
|
147
151
|
- 0
|
148
|
-
hash:
|
152
|
+
hash: -420987875933585136
|
149
153
|
requirements: []
|
150
154
|
rubyforge_project:
|
151
155
|
rubygems_version: 1.8.24
|
@@ -156,7 +160,9 @@ test_files:
|
|
156
160
|
- spec/social_avatar_proxy/app_spec.rb
|
157
161
|
- spec/social_avatar_proxy/avatar_spec.rb
|
158
162
|
- spec/social_avatar_proxy/facebook_avatar_spec.rb
|
163
|
+
- spec/social_avatar_proxy/path_helpers_spec.rb
|
159
164
|
- spec/social_avatar_proxy/remote_file_resolver_spec.rb
|
165
|
+
- spec/social_avatar_proxy/routes_spec.rb
|
160
166
|
- spec/social_avatar_proxy/twitter_avatar_spec.rb
|
161
167
|
- spec/spec_helper.rb
|
162
168
|
- spec/support/env.rb
|