ruby-bitly 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/bin/bitly CHANGED
@@ -4,5 +4,12 @@ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4
4
 
5
5
  require 'ruby-bitly'
6
6
 
7
- url = Bitly.new ARGV[0]
8
- puts url.bitly
7
+ if ARGV[0] == "s"
8
+ result = Bitly.shorten(ARGV[1]).short_url
9
+ elsif ARGV[0] == "e"
10
+ result = Bitly.expand(ARGV[1]).long_url
11
+ else
12
+ result = "Invalid Option!\n" << "Use:\n" << "\tbitly s <url> to shorten url.\n" << "\tbitly e <url> to expand url.\n\n"
13
+ end
14
+
15
+ puts result
data/lib/ruby-bitly.rb CHANGED
@@ -5,15 +5,26 @@ require 'json'
5
5
 
6
6
  class Bitly
7
7
 
8
- attr_reader :response, :long_url, :status_code, :status_txt, :new_hash, :global_hash, :hash
8
+ attr_reader :response, :status_code, :status_txt, :new_hash, :global_hash, :user_hash, :hash, :response, :long_url, :short_url
9
9
 
10
10
  PERSONAL_FILE_PATH = "#{ENV['HOME']}/.bitly"
11
11
  REST_API_URL = "http://api.bit.ly"
12
- ACTION_PATH = { :shorten => '/v3/shorten' }
12
+ ACTION_PATH = { :shorten => '/v3/shorten', :expand => '/v3/expand' }
13
13
 
14
- def initialize(url)
15
- @long_url = url
16
- Bitly.load_personal_data
14
+ def initialize
15
+ @read_only = false
16
+ end
17
+
18
+ def read_only?
19
+ @read_only
20
+ end
21
+
22
+ def long_url=(url)
23
+ @long_url = url unless read_only?
24
+ end
25
+
26
+ def short_url=(url)
27
+ @short_url = url unless read_only?
17
28
  end
18
29
 
19
30
  def Bitly.load_personal_data
@@ -26,26 +37,60 @@ class Bitly
26
37
  end
27
38
  end
28
39
 
40
+ def read_only_now!
41
+ @read_only = true
42
+ end
43
+
29
44
  def shorten
30
- unless @response
31
- @response = Bitly.shorten(@long_url)
45
+ unless read_only?
46
+ @response = Bitly.post_shorten(@long_url)
32
47
 
33
- @status_code = response["status_code"]
34
- @status_txt = response["status_txt"]
35
- @new_hash = response["data"]["new_hash"]
36
- @global_hash = response["data"]["global_hash"]
37
- @hash = response["data"]["hash"]
38
- @bitly = response["data"]["url"]
48
+ @status_code = @response["status_code"]
49
+ @status_txt = @response["status_txt"]
50
+ @long_url = @response["data"]["long_url"]
51
+ @new_hash = @response["data"]["new_hash"]
52
+ @global_hash = @response["data"]["global_hash"]
53
+ @hash = @response["data"]["hash"]
54
+ @short_url = @response["data"]["url"]
55
+
56
+ read_only_now!
39
57
  end
58
+ end
59
+
60
+ def expand
61
+ @response = Bitly.post_expand(@short_url)
62
+ @long_url = @response["data"]["expand"].first["long_url"]
63
+ @global_hash = @response["data"]["expand"].first["global_hash"]
64
+ @user_hash = @response["data"]["expand"].first["user_hash"]
65
+ @status_code = @response["status_code"]
66
+ @status_txt = @response["status_txt"]
40
67
 
41
- @bitly
68
+ read_only_now!
42
69
  end
43
70
 
44
- def Bitly.shorten(new_long_url)
71
+ def Bitly.post_shorten(new_long_url)
45
72
  Bitly.load_personal_data
46
73
  response = RestClient.post(REST_API_URL + ACTION_PATH[:shorten], { :longURL => new_long_url, :login => @@login, :apiKey => @@key })
47
74
  JSON.parse(response)
48
75
  end
49
76
 
50
- alias :bitly :shorten
77
+ def Bitly.post_expand(new_short_url)
78
+ Bitly.load_personal_data
79
+ response = RestClient.post(REST_API_URL + ACTION_PATH[:expand], { :shortURL => new_short_url, :login => @@login, :apiKey => @@key })
80
+ JSON.parse(response)
81
+ end
82
+
83
+ def Bitly.shorten(new_long_url)
84
+ bitly = Bitly.new
85
+ bitly.long_url = new_long_url
86
+ bitly.shorten
87
+ bitly
88
+ end
89
+
90
+ def Bitly.expand(new_short_url)
91
+ bitly = Bitly.new
92
+ bitly.short_url = new_short_url
93
+ bitly.expand
94
+ bitly
95
+ end
51
96
  end
data/ruby-bitly.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruby-bitly}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["rafaeldx7"]
12
- s.date = %q{2010-07-01}
12
+ s.date = %q{2010-07-02}
13
13
  s.default_executable = %q{bitly}
14
14
  s.description = %q{bit.ly ruby client}
15
15
  s.email = %q{rafaeldx7@gmail.com}
@@ -3,11 +3,13 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
  describe "RubyBitly" do
4
4
 
5
5
  it "Load personal data from ~/.bitly" do
6
- Bitly.load_personal_data.should == { "key" => "R_59c1b174b21d92b2beeb4787a6d7ebaf", "login" => "rafaeldx7" }
6
+ file_data = Bitly.load_personal_data
7
+ file_data["key"].should_not be_empty
8
+ file_data["login"].should_not be_empty
7
9
  end
8
10
 
9
- it "Shorten log url staticaly" do
10
- response = Bitly.shorten("http://google.com")
11
+ it "Shorten log url staticaly and return a hash" do
12
+ response = Bitly.post_shorten "http://google.com"
11
13
 
12
14
  response["status_code"].should == 200
13
15
  response["status_txt"].should == "OK"
@@ -17,15 +19,127 @@ describe "RubyBitly" do
17
19
  response["data"]["url"].should match /^http:\/\/bit\.ly\/[A-Za-z0-9]*/
18
20
  end
19
21
 
20
- it "Shorten log url OO" do
21
- url = Bitly.new("http://google.com")
22
- url.shorten.should match /^http:\/\/bit\.ly\/[A-Za-z0-9]*/
22
+ it "Shorten log url with an object" do
23
+ url = Bitly.new
24
+ url.long_url = "http://google.com"
25
+ url.shorten
23
26
 
24
27
  url.status_code.should == 200
25
28
  url.status_txt.should == "OK"
26
29
  url.new_hash.should == 0
27
30
  url.global_hash == "zzzzzzz"
28
31
  url.hash.length.should_not == 0
29
- url.bitly.should match /^http:\/\/bit\.ly\/[A-Za-z0-9]*/
32
+ url.short_url.should match /^http:\/\/bit\.ly\/[A-Za-z0-9]*/
33
+ end
34
+
35
+ it "Expand short url to long url staticaly and return a hash" do
36
+ Bitly.post_expand("http://bit.ly/bcvNe5").should == { "data"=>
37
+ { "expand" => [
38
+ { "long_url" => "http://google.com",
39
+ "short_url" => "http://bit.ly/bcvNe5",
40
+ "global_hash" => "zzzzzzz",
41
+ "user_hash" => "bcvNe5"}]
42
+ },
43
+ "status_txt" => "OK",
44
+ "status_code" => 200
45
+ }
46
+ end
47
+
48
+ it "Expand short url to long url with an object" do
49
+ url = Bitly.new
50
+ url.short_url = "http://bit.ly/bcvNe5"
51
+ url.expand
52
+
53
+ url.long_url.should == "http://google.com"
54
+ url.short_url.should == "http://bit.ly/bcvNe5"
55
+ url.global_hash.should == "zzzzzzz"
56
+ url.user_hash.should == "bcvNe5"
57
+ url.status_code.should == 200
58
+ url.status_txt.should == "OK"
59
+ end
60
+
61
+ it "Update object as read only" do
62
+ url = Bitly.new
63
+ url.read_only?.should be false
64
+ url.read_only_now!
65
+ url.read_only?.should be true
66
+ end
67
+
68
+ it "Shorten url and return an object" do
69
+ url = Bitly.shorten("http://google.com")
70
+ url.should be_an_instance_of(Bitly)
71
+
72
+ url.status_code.should == 200
73
+ url.status_txt.should == "OK"
74
+ url.long_url.should == "http://google.com"
75
+ url.new_hash.should == 0
76
+ url.global_hash.should == "zzzzzzz"
77
+ url.hash.should match /^[A-Za-z0-9]*$/
78
+ url.short_url.should match /^http:\/\/bit\.ly\/[A-Za-z0-9]*/
79
+ end
80
+
81
+ it "New bitly object should be not read only" do
82
+ url = Bitly.new
83
+ url.read_only?.should be false
84
+ end
85
+
86
+ it "Shortened object should be read only" do
87
+ url = Bitly.shorten "http://google.com"
88
+ url.read_only?.should be true
89
+ end
90
+
91
+ it "Expanded object should be read only" do
92
+ url = Bitly.expand "http://bit.ly/bcvNe5"
93
+ url.read_only?.should be true
94
+ end
95
+
96
+ it "Attribute long_url should be updatable when object is not read only" do
97
+ url = Bitly.new
98
+ url.long_url = "http://google.com"
99
+ url.long_url.should == "http://google.com"
100
+
101
+ url.long_url = "http://xlii.com.br"
102
+ url.long_url.should == "http://xlii.com.br"
103
+ end
104
+
105
+ it "Attribute long_url should not be updatable when object is read only" do
106
+ url = Bitly.new
107
+ url.long_url = "http://google.com"
108
+ url.long_url.should == "http://google.com"
109
+ url.read_only_now!
110
+
111
+ url.long_url = "http://xlii.com.br"
112
+ url.long_url.should == "http://google.com"
113
+ end
114
+
115
+ it "Expand url and return an object" do
116
+ url = Bitly.expand "http://bit.ly/bcvNe5"
117
+ url.should be_an_instance_of(Bitly)
118
+
119
+ url.long_url.should == "http://google.com"
120
+ url.short_url.should == "http://bit.ly/bcvNe5"
121
+ url.global_hash.should == "zzzzzzz"
122
+ url.user_hash.should == "bcvNe5"
123
+ url.status_code.should == 200
124
+ url.status_txt.should == "OK"
125
+ end
126
+
127
+ it "Attribute short_url should be updated when object is not read only" do
128
+ url = Bitly.new
129
+ url.short_url = "http://bit.ly/bcvNe5"
130
+ url.short_url.should == "http://bit.ly/bcvNe5"
131
+
132
+ url.short_url = "http://bit.ly/xlii42"
133
+ url.short_url.should == "http://bit.ly/xlii42"
134
+ end
135
+
136
+ it "Attribute short_url should not be updated when object is read only" do
137
+ url = Bitly.new
138
+ url.short_url = "http://bit.ly/xlii42"
139
+ url.short_url.should == "http://bit.ly/xlii42"
140
+ url.read_only_now!
141
+
142
+ url.short_url = "http://bit.ly/bcvNe5"
143
+ url.short_url.should == "http://bit.ly/xlii42"
30
144
  end
31
145
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-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
  - rafaeldx7
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-01 00:00:00 -03:00
18
+ date: 2010-07-02 00:00:00 -03:00
19
19
  default_executable: bitly
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency