simple_bitly 0.0.1 → 0.0.2

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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format nested
data/README.mdown ADDED
@@ -0,0 +1,27 @@
1
+ # Simple Bitly
2
+
3
+ A simple and lightweight wrapper for the Bitly API (v3).
4
+
5
+ ## Installation
6
+
7
+ > [sudo] gem install simple_bitly
8
+
9
+ ## Usage
10
+
11
+ > bitly = SimpleBitly.new(USERNAME, API_KEY)
12
+
13
+ ##### shorten a URL
14
+
15
+ > result = bitly.shorten('http://reddit.com')
16
+ > result.short_url
17
+ > result.long_url
18
+ > result.hash
19
+ > result.qr_code
20
+
21
+ ##### expand one or more URLs
22
+
23
+ > results = bitly.expand('http://bit.ly/9QePJB', 'http://bit.ly/CuJU')
24
+ > results.first.long_url
25
+ > results.first.short_url
26
+ > results.first.hash
27
+ > results.first.qr_code
@@ -1,3 +1,3 @@
1
1
  module SimpleBitly
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/simple_bitly.rb CHANGED
@@ -1,47 +1,79 @@
1
1
  require 'json'
2
2
  require 'net/http'
3
+ require 'open-uri'
3
4
 
4
5
  module SimpleBitly
5
6
 
6
- def self.new(username, api_key)
7
- SimpleBitly::Client.new(username, api_key)
8
- end
7
+ ENDPOINT = "http://api.bitly.com/v3/" # The endpoint for Bitly's V3 API
8
+
9
+ # Creates a new client.
10
+ # Requires a username and api_key which can be found at http://bitly.com/a/your_api_key
11
+ def self.new(username, api_key)
12
+ SimpleBitly::Client.new(username, api_key)
13
+ end
9
14
 
10
15
  class Client
11
-
16
+
12
17
  def initialize(username, api_key)
13
18
  @username, @api_key = username, api_key
14
19
  end
15
20
 
21
+ # Shortens a long url
22
+ #
23
+ # Returns a SimpleBitly::URL class with the following instance variables: short_url, hash, qr_code, long_url
16
24
  def shorten(long_url)
17
- SimpleBitly::URL.new(@username, @api_key, long_url)
25
+ SimpleBitly::URL
26
+ url = ENDPOINT + "shorten?login=#{@username}&apiKey=#{@api_key}&longUrl=#{long_url}&format=json"
27
+ resp = get(url)
28
+ if resp['status_code'] == 200
29
+ return SimpleBitly::URL.new(resp['data'])
30
+ else
31
+ bitly_error(resp['status_code'], resp['status_txt'])
32
+ end
18
33
  end
19
34
 
35
+ # Expands one or more short urls
36
+ #
37
+ # Returns an array of SimpleBitly::URL classes with the following instance variables: short_url, hash, qr_code, long_url
38
+ # Results are returned in the order they were entered
39
+ def expand(*short_urls)
40
+ expanded_urls = []
41
+ url = ENDPOINT + "expand?login=#{@username}&apiKey=#{@api_key}&format=json"
42
+ short_urls.each {|u| url += "&shortUrl=#{u}"}
43
+ resp = get(url)
44
+ if resp['status_code'] == 200
45
+ resp['data']['expand'].each do |result|
46
+ expanded_urls << SimpleBitly::URL.new(result)
47
+ end
48
+ return expanded_urls
49
+ else
50
+ bitly_error(resp['status_code'], resp['status_txt'])
51
+ end
52
+ end
53
+
54
+ # Raises an error containing the 'Status Code' and the 'Status Message' sent by Bitly's server
55
+ def bitly_error(code, message)
56
+ raise "Error from Bitly: #{code.to_s}, #{message}"
57
+ end
58
+
59
+ # Retrieves and parses a response from Bitly
60
+ def get(url)
61
+ JSON.parse(Net::HTTP.get(URI.parse(URI.escape(url))))
62
+ end
20
63
  end
21
64
 
22
65
  class URL
23
66
 
24
- attr_reader :short_url, :long_url, :hash
67
+ attr_reader :long_url, :short_url, :hash, :qr_code
25
68
 
26
- def initialize(username, api_key, long_url)
27
- @username, @api_key, @long_url = username, api_key, long_url
28
- shorten
69
+ # SimpleBitly::URL class receives the JSON parsed hash from Bitly and assigns the results to instance variables
70
+ def initialize(opts={})
71
+ @short_url = opts['url'] || opts['short_url']
72
+ @long_url = opts['long_url']
73
+ @hash = opts['hash'] || opts['user_hash']
74
+ @qr_code = @short_url + ".qrcode"
29
75
  end
30
76
 
31
- private
32
-
33
- def shorten
34
- url = "http://api.bitly.com/v3/shorten?login=#{@username}&apiKey=#{@api_key}&longUrl=#{@long_url}&format=json"
35
- resp = JSON.parse(Net::HTTP.get(URI.parse(url)))
36
- if resp['status_code'] == 200
37
- data = resp['data']
38
- @long_url = data['long_url']
39
- @short_url = data['url']
40
- @hash = data['hash']
41
- else
42
- raise "Error from bitly: #{resp['status_code']}, #{resp['status_txt']}"
43
- end
44
- end
45
77
  end
46
78
 
47
79
  end
data/simple_bitly.gemspec CHANGED
@@ -11,6 +11,8 @@ Gem::Specification.new do |s|
11
11
  s.homepage = ""
12
12
  s.summary = %q{A lightweight wrapper for the bit.ly API}
13
13
  s.description = %q{lightweight bit.ly API wrapper}
14
+
15
+ s.add_development_dependency('rspec', '>= 2.0.0')
14
16
 
15
17
  s.rubyforge_project = "simple_bitly"
16
18
 
data/spec/helper.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'simple_bitly'
2
+
3
+ def username
4
+ 'username here'
5
+ end
6
+
7
+ def api_key
8
+ 'api_key here'
9
+ end
@@ -0,0 +1,67 @@
1
+ require 'helper'
2
+
3
+ describe "Simple Bitly" do
4
+
5
+ it "should create a new bitly client" do
6
+ bitly = SimpleBitly.new(username, api_key)
7
+ bitly.class.should == SimpleBitly::Client
8
+ end
9
+
10
+ context "client" do
11
+
12
+ context "shorten" do
13
+
14
+ before(:all) do
15
+ @bitly = SimpleBitly.new(username, api_key)
16
+ @result = @bitly.shorten('http://reddit.com/')
17
+ end
18
+
19
+ it "should return a SimpleBitly::URL" do
20
+ @result.class.should == SimpleBitly::URL
21
+ end
22
+
23
+ it "should return a short bitly url" do
24
+ @result.short_url[0..13].should == "http://bit.ly/"
25
+ end
26
+
27
+ it "should return a long url" do
28
+ @result.long_url.should == "http://reddit.com/"
29
+ end
30
+
31
+ it "should return a qr code" do
32
+ @result.qr_code.should == @result.short_url + ".qrcode"
33
+ end
34
+ end
35
+
36
+ context "expand" do
37
+
38
+ before(:all) do
39
+ @bitly = SimpleBitly.new(username, api_key)
40
+ @result = @bitly.expand('http://bit.ly/9QePJB', 'http://tcrn.ch/bkNy6W')
41
+ end
42
+
43
+ it "should return an array of SimpleBitly::URLs" do
44
+ @result.each do |result|
45
+ result.class.should == SimpleBitly::URL
46
+ end
47
+ end
48
+
49
+ it "should return the correct amount of results" do
50
+ @result.size.should == 2
51
+ end
52
+
53
+ it "should return the correct long url" do
54
+ @result.first.long_url.should == "http://reddit.com/"
55
+ end
56
+
57
+ it "should return the short url" do
58
+ @result.first.short_url.should == "http://bit.ly/9QePJB"
59
+ end
60
+
61
+ it "should return the qr code" do
62
+ @result.first.qr_code.should == "http://bit.ly/9QePJB.qrcode"
63
+ end
64
+ end
65
+
66
+ end
67
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_bitly
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Nizar Dhamani
@@ -15,10 +15,25 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-07 00:00:00 -05:00
18
+ date: 2011-07-08 00:00:00 -05:00
19
19
  default_executable:
20
- dependencies: []
21
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 0
34
+ version: 2.0.0
35
+ type: :development
36
+ version_requirements: *id001
22
37
  description: lightweight bit.ly API wrapper
23
38
  email:
24
39
  - nizar.dhamani@gmail.com
@@ -30,12 +45,15 @@ extra_rdoc_files: []
30
45
 
31
46
  files:
32
47
  - .gitignore
48
+ - .rspec
33
49
  - Gemfile
34
- - README.txt
50
+ - README.mdown
35
51
  - Rakefile
36
52
  - lib/simple_bitly.rb
37
53
  - lib/simple_bitly/version.rb
38
54
  - simple_bitly.gemspec
55
+ - spec/helper.rb
56
+ - spec/simple_bitly/client_spec.rb
39
57
  has_rdoc: true
40
58
  homepage: ""
41
59
  licenses: []
@@ -70,5 +88,6 @@ rubygems_version: 1.3.7
70
88
  signing_key:
71
89
  specification_version: 3
72
90
  summary: A lightweight wrapper for the bit.ly API
73
- test_files: []
74
-
91
+ test_files:
92
+ - spec/helper.rb
93
+ - spec/simple_bitly/client_spec.rb
data/README.txt DELETED
@@ -1,18 +0,0 @@
1
- Description:
2
-
3
- A simple and lightweight wrapper for the Bitly API.
4
-
5
-
6
- Installation:
7
-
8
- [sudo] gem install simple_bitly
9
-
10
-
11
- Usage:
12
-
13
- bitly = SimpleBitly.new(USERNAME, API_KEY)
14
- data = bitly.shorten('http://github.com')
15
-
16
- data.short_url
17
- data.long_url
18
- data.hash