simple_bitlify 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "simple_cache"
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "bundler"
12
+ gem "jeweler"
13
+
14
+ # vcr stuff: this needs psych or else will crash
15
+ gem "vcr"
16
+ gem "webmock"
17
+ gem "psych"
18
+
19
+ gem 'simplecov', :require => false
20
+ gem "ruby-debug19"
21
+ end
22
+
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.2.7)
5
+ archive-tar-minitar (0.5.2)
6
+ columnize (0.3.6)
7
+ crack (0.3.1)
8
+ git (1.2.5)
9
+ jeweler (1.8.3)
10
+ bundler (~> 1.0)
11
+ git (>= 1.2.5)
12
+ rake
13
+ rdoc
14
+ json (1.6.6)
15
+ linecache19 (0.5.12)
16
+ ruby_core_source (>= 0.1.4)
17
+ multi_json (1.2.0)
18
+ psych (1.3.1)
19
+ rake (0.9.2.2)
20
+ rdoc (3.12)
21
+ json (~> 1.4)
22
+ ruby-debug-base19 (0.11.25)
23
+ columnize (>= 0.3.1)
24
+ linecache19 (>= 0.5.11)
25
+ ruby_core_source (>= 0.1.4)
26
+ ruby-debug19 (0.11.6)
27
+ columnize (>= 0.3.1)
28
+ linecache19 (>= 0.5.11)
29
+ ruby-debug-base19 (>= 0.11.19)
30
+ ruby_core_source (0.1.5)
31
+ archive-tar-minitar (>= 0.5.2)
32
+ simple_cache (0.8.1)
33
+ sqlite3
34
+ simplecov (0.6.1)
35
+ multi_json (~> 1.0)
36
+ simplecov-html (~> 0.5.3)
37
+ simplecov-html (0.5.3)
38
+ sqlite3 (1.3.5)
39
+ vcr (2.0.1)
40
+ webmock (1.8.6)
41
+ addressable (>= 2.2.7)
42
+ crack (>= 0.1.7)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ bundler
49
+ jeweler
50
+ psych
51
+ ruby-debug19
52
+ simple_cache
53
+ simplecov
54
+ vcr
55
+ webmock
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 radiospiel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = simple_bitlify
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to simple_bitlify
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 radiospiel. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "simple_bitlify"
18
+ gem.homepage = "http://github.com/radiospiel/simple_bitlify"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A simple bitlifier}
21
+ gem.description = %Q{A simple, yet caching bitlifier.}
22
+ gem.email = "eno@open-lab.org"
23
+ gem.authors = ["radiospiel"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "micro_sql #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.5.0
@@ -0,0 +1,78 @@
1
+ require "cgi"
2
+ require "json"
3
+ require "simple_cache"
4
+
5
+ # A basic, caching, Bitlifier
6
+ module Bitly
7
+ extend self
8
+
9
+ TIME_TO_LIVE = 365 * 24 * 3600 # 1 year
10
+ TIME_TO_LIVE_FAILURE = 3600 # 1 hour
11
+
12
+ attr :cache, true
13
+ attr :config, true
14
+
15
+ def cache
16
+ @cache ||= SimpleCache.new("simple_bitlify")
17
+ end
18
+
19
+ def config
20
+ @config ||= {
21
+ "user" => "bitlyapidemo",
22
+ "key" => "R_0da49e0a9118ff35f52f629d2d71bf07"
23
+ }
24
+ end
25
+ #
26
+ # self.config = {
27
+ # "user" => "radiospiel",
28
+ # "key" => "R_b01d6497ec9a1b2fff03fe942f7c3e1c"
29
+ # }
30
+
31
+ def self.url_base
32
+ @url_base ||= begin
33
+ user, key = (self.config || {}).values_at "user", "key"
34
+
35
+ raise "Missing or invalid bitly configuration: #{config.inspect}" unless user && key
36
+ "https://api-ssl.bitly.com/v3/shorten?login=#{user}&apiKey=#{key}"
37
+ end
38
+ end
39
+
40
+ def self.shorten(url)
41
+ return url if !url || url.index("/bit.ly/")
42
+
43
+ cache.fetch(url) || begin
44
+ bitly_url = "#{self.url_base}&longUrl=#{CGI.escape(url)}"
45
+ parsed = JSON.parse get(bitly_url)
46
+
47
+ shortened = parsed["data"]["url"] if parsed["status_code"] == 200
48
+ cache.store(url, shortened || url, shortened ? TIME_TO_LIVE : TIME_TO_LIVE_FAILURE)
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def get(uri_str, limit = 10)
55
+ raise 'too many redirections' if limit == 0
56
+
57
+ uri = URI.parse(uri_str)
58
+
59
+ http = Net::HTTP.new(uri.host, uri.port)
60
+ if uri.scheme == "https"
61
+ http.use_ssl = true
62
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
63
+ end
64
+ request = Net::HTTP::Get.new(uri.request_uri)
65
+ response = http.request(request)
66
+
67
+ case response
68
+ when Net::HTTPSuccess then
69
+ response.body
70
+ when Net::HTTPRedirection then
71
+ location = response['location']
72
+ App.logger.debug "redirected to #{location}"
73
+ get(location, limit - 1)
74
+ else
75
+ response.value
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative './test_helper'
4
+ require 'vcr'
5
+
6
+ VCR.configure do |c|
7
+ c.cassette_library_dir = "#{File.dirname(__FILE__)}/vcr"
8
+ c.hook_into :webmock
9
+ end
10
+
11
+ class BitlifyTest < Test::Unit::TestCase
12
+ def setup
13
+ Bitly.cache.clear
14
+ end
15
+
16
+ def test_limit_exceeded
17
+ VCR.use_cassette('bitly_limit_exceeded') do
18
+ assert_equal("http://radiospiel.org/foo", Bitly.shorten("http://radiospiel.org/foo"))
19
+ end
20
+ end
21
+
22
+ def test_ok
23
+ VCR.use_cassette('bitly_ok') do
24
+ assert_equal("http://bit.ly/HV7woD", Bitly.shorten("http://radiospiel.org/a=1&b=2"))
25
+ end
26
+ end
27
+
28
+ def test_url_base
29
+ expected = "https://api-ssl.bitly.com/v3/shorten?login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07"
30
+ assert_equal expected, Bitly.url_base
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ require 'ruby-debug'
12
+ require 'simplecov'
13
+ require 'vcr'
14
+ require 'test/unit'
15
+ SimpleCov.start do
16
+ add_filter "test/*"
17
+ end
18
+
19
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
20
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
21
+
22
+ require 'simple_bitlify'
23
+
24
+ class Test::Unit::TestCase
25
+ end
@@ -0,0 +1,43 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://api-ssl.bitly.com/v3/shorten?apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&login=bitlyapidemo&longUrl=http://radiospiel.org/foo
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - ! '*/*'
12
+ User-Agent:
13
+ - Ruby
14
+ response:
15
+ status:
16
+ code: 200
17
+ message: OK
18
+ headers:
19
+ !binary "U2VydmVy":
20
+ - !binary |-
21
+ bmdpbng=
22
+ !binary "RGF0ZQ==":
23
+ - !binary |-
24
+ VGh1LCAwNSBBcHIgMjAxMiAxMToyMjo1MiBHTVQ=
25
+ Content-Type:
26
+ - !binary |-
27
+ YXBwbGljYXRpb24vanNvbjsgY2hhcnNldD11dGYtOA==
28
+ !binary "Q29ubmVjdGlvbg==":
29
+ - !binary |-
30
+ a2VlcC1hbGl2ZQ==
31
+ Mime-Version:
32
+ - !binary |-
33
+ MS4w
34
+ Content-Length:
35
+ - !binary |-
36
+ NzI=
37
+ body:
38
+ encoding: US-ASCII
39
+ string: ! '{ "data": [ ], "status_code": 403, "status_txt": "RATE_LIMIT_EXCEEDED"
40
+ }'
41
+ http_version:
42
+ recorded_at: Thu, 05 Apr 2012 11:22:52 GMT
43
+ recorded_with: VCR 2.0.1
@@ -0,0 +1,46 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://api-ssl.bitly.com/v3/shorten?apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&login=bitlyapidemo&longUrl=http://radiospiel.org/a=1%26b=2
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - ! '*/*'
12
+ User-Agent:
13
+ - Ruby
14
+ response:
15
+ status:
16
+ code: 200
17
+ message: OK
18
+ headers:
19
+ !binary "U2VydmVy":
20
+ - !binary |-
21
+ bmdpbng=
22
+ !binary "RGF0ZQ==":
23
+ - !binary |-
24
+ VGh1LCAwNSBBcHIgMjAxMiAxMToyMTo0MCBHTVQ=
25
+ Content-Type:
26
+ - !binary |-
27
+ YXBwbGljYXRpb24vanNvbjsgY2hhcnNldD11dGYtOA==
28
+ !binary "Q29ubmVjdGlvbg==":
29
+ - !binary |-
30
+ a2VlcC1hbGl2ZQ==
31
+ Mime-Version:
32
+ - !binary |-
33
+ MS4w
34
+ Content-Length:
35
+ - !binary |-
36
+ MTk1
37
+ body:
38
+ encoding: US-ASCII
39
+ string: ! '{ "status_code": 200, "status_txt": "OK", "data": { "long_url": "http:\/\/radiospiel.org\/a=1&b=2",
40
+ "url": "http:\/\/bit.ly\/HV7woD", "hash": "HV7woD", "global_hash": "HV7woE",
41
+ "new_hash": 0 } }
42
+
43
+ '
44
+ http_version:
45
+ recorded_at: Thu, 05 Apr 2012 11:21:40 GMT
46
+ recorded_with: VCR 2.0.1
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_bitlify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - radiospiel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: simple_cache
16
+ requirement: &70311652195180 !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: *70311652195180
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &70311652194260 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70311652194260
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &70311652193500 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70311652193500
47
+ - !ruby/object:Gem::Dependency
48
+ name: vcr
49
+ requirement: &70311652192560 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70311652192560
58
+ - !ruby/object:Gem::Dependency
59
+ name: webmock
60
+ requirement: &70311652191860 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70311652191860
69
+ - !ruby/object:Gem::Dependency
70
+ name: psych
71
+ requirement: &70311652191040 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70311652191040
80
+ - !ruby/object:Gem::Dependency
81
+ name: simplecov
82
+ requirement: &70311652190360 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70311652190360
91
+ - !ruby/object:Gem::Dependency
92
+ name: ruby-debug19
93
+ requirement: &70311652189520 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70311652189520
102
+ description: A simple, yet caching bitlifier.
103
+ email: eno@open-lab.org
104
+ executables: []
105
+ extensions: []
106
+ extra_rdoc_files:
107
+ - LICENSE.txt
108
+ - README.rdoc
109
+ files:
110
+ - .document
111
+ - Gemfile
112
+ - Gemfile.lock
113
+ - LICENSE.txt
114
+ - README.rdoc
115
+ - Rakefile
116
+ - VERSION
117
+ - lib/simple_bitlify.rb
118
+ - test/bitlify_test.rb
119
+ - test/test_helper.rb
120
+ - test/vcr/bitly_limit_exceeded.yml
121
+ - test/vcr/bitly_ok.yml
122
+ homepage: http://github.com/radiospiel/simple_bitlify
123
+ licenses:
124
+ - MIT
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ segments:
136
+ - 0
137
+ hash: -2260416520007165785
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 1.8.17
147
+ signing_key:
148
+ specification_version: 3
149
+ summary: A simple bitlifier
150
+ test_files: []