firefly 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- firefly (1.3.0)
4
+ firefly (1.3.1)
5
5
  dm-aggregates (~> 1.0.2)
6
6
  dm-core (~> 1.0.2)
7
7
  dm-migrations (~> 1.0.2)
data/HISTORY CHANGED
@@ -1,3 +1,7 @@
1
+ = 1.4.0 - 2011-02-11
2
+
3
+ * 2011-02-21 Added support for 'share to twitter' links [ariejan]
4
+
1
5
  = 1.3.1 / 2011-02-20
2
6
 
3
7
  * 2011-02-20 Fixed issue with escaped characters in URL (ticket #21) [ariejan]
data/README.md CHANGED
@@ -40,6 +40,17 @@ After you have installed the Firefly gem you should create a `config.ru` file th
40
40
  #
41
41
  # Default: Check this out: %short_url%
42
42
  # set :tweet, "I loved this: %short_url% - Go check it out now!"
43
+
44
+ # If you want to enable 'share to twitter'
45
+
46
+ # A secure key to be used with 'share to twitter'
47
+ # set :sharing_key, "set-a-long-secure-key-here"
48
+
49
+ # Currently only twitter is supported
50
+ # set :sharing_targets, [:twitter]
51
+
52
+ # Set the TLDs (in URLs) that are allowed to be shortened
53
+ # set :sharing_domains, ["example.com", "mydomain.com"]
43
54
  end
44
55
 
45
56
  run app
@@ -67,6 +78,13 @@ All configuration is done in `config.ru`.
67
78
  * `:database` a database URI that [DataMapper][4] can understand.
68
79
  * `:recent_urls` sets the number of URLs to show in the overview. Default: 25.
69
80
  * `:tweet` set the template to use for tweets. Default: `"Check this out: %short_url%"`
81
+ * `:sharing_key` set this to something long and secure, used for
82
+ creating 'share to twitter' links.
83
+ * `:sharing_targets` set to `[:twitter]` if you want to enable sharing
84
+ to twitter
85
+ * `:sharing_domains` set to an array of TLDs. Only urls shared in those
86
+ domains will be allowed. Set to an empty array (`[]`) if you want to
87
+ accept all domains.
70
88
 
71
89
  It's possible to use all kinds of backends with DataMapper. Sqlite3 and MySQL have been tested, but others may work as well.
72
90
 
@@ -76,6 +94,8 @@ Simply visit `http://:hostname/` and enter your `:api_key`. You can now shorten
76
94
 
77
95
  ## Advanced usage
78
96
 
97
+ ### Using the API
98
+
79
99
  You may also use the API to automate URL shortening. Here's how.
80
100
 
81
101
  Adding a URL is done by doing a simple POST request that includes the URL and your API key.
@@ -99,6 +119,21 @@ After you restart Terminal.app (or at least reload the `.profile` file) you can
99
119
  -- http://ariejan.net => http://aj.gs/1
100
120
  Short URL copied to clipboard.
101
121
 
122
+ ### Using the 'Share to Twitter' features
123
+
124
+ The share to twitter feature allows you to create custom links on your
125
+ site. When clicked, the specified URL will be shortened and the user
126
+ will be redirect to Twitter to share the new short URL.
127
+
128
+ GET /api/share
129
+ POST /api/share
130
+
131
+ Parameters:
132
+ url - Long URL to share (required)
133
+ key - The sharing key, specified in `config.ru` (required)
134
+ target - Target to share to, currently only `twitter` is supported (required)
135
+ title - Title of text to use in the tweet (optional)
136
+
102
137
  # Bugs, Feature Requests, etc.
103
138
 
104
139
  * [Source][5]
data/config.ru.example CHANGED
@@ -17,6 +17,20 @@ app = Firefly::Server.new do
17
17
  # Make sure to install the do_mysql gem:
18
18
  # sudo gem install do_mysql
19
19
  # set :database, "mysql://root@localhost/firefly"
20
+
21
+ # If you want to enable 'share to twitter'
22
+
23
+ # A secure key to be used with 'share to twitter'
24
+ # set :sharing_key, "set-a-long-secure-key-here"
25
+ set :sharing_key, ""
26
+
27
+ # Currently only twitter is supported
28
+ # set :sharing_targets, [:twitter]
29
+ set :sharing_targets, []
30
+
31
+ # Set the TLDs (in URLs) that are allowed to be shortened
32
+ # set :sharing_domains, ["example.com", "mydomain.com"]
33
+ set :sharing_domain, []
20
34
  end
21
35
 
22
36
  run app
@@ -43,6 +43,31 @@ module Firefly
43
43
  key == Digest::MD5.hexdigest(config[:api_key])
44
44
  end
45
45
 
46
+ def has_valid_share_key?
47
+ return false unless @config.has_key?(:sharing_key)
48
+ @config[:sharing_key] == params[:key]
49
+ end
50
+
51
+ def has_valid_share_domain?
52
+ return false unless @config.has_key?(:sharing_domains)
53
+ return true if @config[:sharing_domains].empty?
54
+ @config[:sharing_domains].any? { |domain| params[:url].include?(domain) }
55
+ end
56
+
57
+ def has_valid_share_target?
58
+ return false unless @config.has_key?(:sharing_domains)
59
+ @config[:sharing_targets].include?(params[:target].downcase.to_sym)
60
+ end
61
+
62
+ def validate_share_permission
63
+ if has_valid_share_key? && has_valid_share_domain? && has_valid_share_target?
64
+ return true
65
+ else
66
+ status 401
67
+ return false
68
+ end
69
+ end
70
+
46
71
  def validate_api_permission
47
72
  if !has_valid_api_cookie? && params[:api_key] != config[:api_key]
48
73
  status 401
@@ -79,8 +104,12 @@ module Firefly
79
104
  end
80
105
 
81
106
  # Format a tweet
82
- def tweet(url)
83
- config[:tweet].gsub('%short_url%', url)
107
+ def tweet(url, message = nil)
108
+ if message.nil?
109
+ config[:tweet].gsub('%short_url%', url)
110
+ else
111
+ "#{message} #{url}"
112
+ end
84
113
  end
85
114
 
86
115
  def store_api_key(key)
@@ -138,6 +167,21 @@ module Firefly
138
167
  get '/api/add', &api_add
139
168
  post '/api/add', &api_add
140
169
 
170
+ api_share = lambda {
171
+ validate_share_permission or return "Cannot share that URL."
172
+
173
+ @url = params[:url]
174
+ @code, @result = generate_short_url(@url, nil)
175
+ invalid = @code.nil?
176
+
177
+ if params[:target].downcase.to_sym == :twitter
178
+ redirect(URI.escape("http://twitter.com/home?status=#{tweet("http://#{config[:hostname]}/#{@code}", params[:title])}"))
179
+ end
180
+ }
181
+
182
+ get '/api/share', &api_share
183
+ post '/api/share', &api_share
184
+
141
185
  # GET /b3d+
142
186
  #
143
187
  # Show info on the URL
@@ -1,3 +1,3 @@
1
1
  module Firefly
2
- VERSION = "1.3.1"
2
+ VERSION = "1.4.0"
3
3
  end
@@ -0,0 +1,65 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Sharing" do
4
+ include Rack::Test::Methods
5
+
6
+ def app
7
+ @@app
8
+ end
9
+
10
+ before(:each) do
11
+ @params = {
12
+ :url => 'http://example.com/test',
13
+ :key => 'asdfasdf',
14
+ :target => 'twitter',
15
+ :title => 'Test post'
16
+ }
17
+ end
18
+
19
+ [:post, :get].each do |verb|
20
+ describe "twitter" do
21
+ it "should create a shortened URL" do
22
+ lambda {
23
+ self.send verb, '/api/share', @params
24
+ }.should change(Firefly::Url, :count).by(1)
25
+ end
26
+
27
+ it "should redirect to twitter with status" do
28
+ self.send verb, '/api/share', @params
29
+ last_response.should be_redirect
30
+ last_response['Location'].should match(/twitter.com/i)
31
+ end
32
+
33
+ it "should post the title and short url to twitter" do
34
+ self.send verb, '/api/share', @params
35
+ url = Firefly::Url.first(:url => @params[:url])
36
+
37
+ last_response['Location'].should include(URI.escape("#{@params[:title]} http://test.host/#{url.code}"))
38
+ end
39
+
40
+ it "should not allow sharing of example.org URL" do
41
+ self.send verb, '/api/share', @params.merge(:url => 'http://example.org/test123')
42
+ last_response.status.should eql(401)
43
+ last_response.body.should match(/cannot share that URL/i)
44
+ end
45
+
46
+ it "should not create a short URL for example.org URL" do
47
+ lambda {
48
+ self.send verb, '/api/share', @params.merge(:url => 'http://example.org/test123')
49
+ }.should_not change(Firefly::Url, :count)
50
+ end
51
+
52
+ it "should not share to facebook" do
53
+ self.send verb, '/api/share', @params.merge(:target => 'facebook')
54
+ last_response.status.should eql(401)
55
+ last_response.body.should match(/cannot share that URL/i)
56
+ end
57
+
58
+ it "should not create a short URL for facebook" do
59
+ lambda {
60
+ self.send verb, '/api/share', @params.merge(:target => 'facebook')
61
+ }.should_not change(Firefly::Url, :count)
62
+ end
63
+ end
64
+ end
65
+ end
data/spec/spec_helper.rb CHANGED
@@ -17,9 +17,13 @@ set :raise_errors, true
17
17
  set :logging, false
18
18
 
19
19
  @@app = Firefly::Server.new do
20
- set :hostname, "test.host"
21
- set :api_key, "test"
22
- set :database, "mysql://root@localhost/firefly_test"
20
+ set :hostname, "test.host"
21
+ set :api_key, "test"
22
+ set :database, "mysql://root@localhost/firefly_test"
23
+
24
+ set :sharing_key, "asdfasdf"
25
+ set :sharing_targets, [:twitter]
26
+ set :sharing_domains, ["example.com", "example.net"]
23
27
  end
24
28
 
25
29
  Spec::Runner.configure do |config|
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: firefly
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 3
9
- - 1
10
- version: 1.3.1
8
+ - 4
9
+ - 0
10
+ version: 1.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ariejan de Vroom
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-20 00:00:00 +01:00
18
+ date: 2011-02-21 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -201,6 +201,7 @@ files:
201
201
  - spec/firefly/base62_spec.rb
202
202
  - spec/firefly/code_factory_spec.rb
203
203
  - spec/firefly/server_spec.rb
204
+ - spec/firefly/sharing_spec.rb
204
205
  - spec/firefly/url_spec.rb
205
206
  - spec/fixtures/urls.yml
206
207
  - spec/spec.opts
@@ -251,6 +252,7 @@ test_files:
251
252
  - spec/firefly/base62_spec.rb
252
253
  - spec/firefly/code_factory_spec.rb
253
254
  - spec/firefly/server_spec.rb
255
+ - spec/firefly/sharing_spec.rb
254
256
  - spec/firefly/url_spec.rb
255
257
  - spec/fixtures/urls.yml
256
258
  - spec/spec.opts