ruby-bitly 0.0.2 → 0.0.3
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/VERSION +1 -1
- data/bin/bitly +5 -1
- data/lib/ruby-bitly.rb +29 -2
- data/ruby-bitly.gemspec +1 -1
- data/spec/ruby-bitly_spec.rb +49 -5
- metadata +3 -3
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.0.
|
|
1
|
+
0.0.3
|
data/bin/bitly
CHANGED
|
@@ -8,8 +8,12 @@ if ARGV[0] == "s"
|
|
|
8
8
|
result = Bitly.shorten(ARGV[1]).short_url
|
|
9
9
|
elsif ARGV[0] == "e"
|
|
10
10
|
result = Bitly.expand(ARGV[1]).long_url
|
|
11
|
+
elsif ARGV[0] == "--user-clicks"
|
|
12
|
+
result = Bitly.clicks(ARGV[1]).user_clicks.to_s
|
|
13
|
+
elsif ARGV[0] == "--global-clicks"
|
|
14
|
+
result = Bitly.clicks(ARGV[1]).global_clicks.to_s
|
|
11
15
|
else
|
|
12
|
-
result = "Invalid Option!\n" << "Use:\n" << "\tbitly s <url> to shorten url.\n" << "\tbitly e <url> to expand url.\n\n"
|
|
16
|
+
result = "Invalid Option!\n" << "Use:\n" << "\tbitly s <url> to shorten url.\n" << "\tbitly e <url> to expand url.\n" << "\tbitly --user-clicks <url>\n" << "\tbitly --global-clicks <url>\n" << "\n"
|
|
13
17
|
end
|
|
14
18
|
|
|
15
19
|
puts result
|
data/lib/ruby-bitly.rb
CHANGED
|
@@ -5,11 +5,11 @@ require 'json'
|
|
|
5
5
|
|
|
6
6
|
class Bitly
|
|
7
7
|
|
|
8
|
-
attr_reader :response, :status_code, :status_txt, :new_hash, :global_hash, :user_hash, :hash, :response, :long_url, :short_url
|
|
8
|
+
attr_reader :response, :status_code, :status_txt, :new_hash, :global_hash, :user_hash, :hash, :response, :long_url, :short_url, :global_clicks, :user_clicks
|
|
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', :expand => '/v3/expand' }
|
|
12
|
+
ACTION_PATH = { :shorten => '/v3/shorten', :expand => '/v3/expand', :clicks => '/v3/clicks' }
|
|
13
13
|
|
|
14
14
|
def initialize
|
|
15
15
|
@read_only = false
|
|
@@ -59,6 +59,7 @@ class Bitly
|
|
|
59
59
|
|
|
60
60
|
def expand
|
|
61
61
|
@response = Bitly.post_expand(@short_url)
|
|
62
|
+
|
|
62
63
|
@long_url = @response["data"]["expand"].first["long_url"]
|
|
63
64
|
@global_hash = @response["data"]["expand"].first["global_hash"]
|
|
64
65
|
@user_hash = @response["data"]["expand"].first["user_hash"]
|
|
@@ -68,6 +69,19 @@ class Bitly
|
|
|
68
69
|
read_only_now!
|
|
69
70
|
end
|
|
70
71
|
|
|
72
|
+
def clicks
|
|
73
|
+
@response = Bitly.get_clicks(@short_url)
|
|
74
|
+
|
|
75
|
+
@global_clicks = @response["data"]["clicks"].first["global_clicks"]
|
|
76
|
+
@user_clicks = @response["data"]["clicks"].first["user_clicks"]
|
|
77
|
+
@global_hash = @response["data"]["clicks"].first["global_hash"]
|
|
78
|
+
@user_hash = @response["data"]["clicks"].first["user_hash"]
|
|
79
|
+
@status_code = @response["status_code"]
|
|
80
|
+
@status_txt = @response["status_txt"]
|
|
81
|
+
|
|
82
|
+
read_only_now!
|
|
83
|
+
end
|
|
84
|
+
|
|
71
85
|
def Bitly.post_shorten(new_long_url)
|
|
72
86
|
Bitly.load_personal_data
|
|
73
87
|
response = RestClient.post(REST_API_URL + ACTION_PATH[:shorten], { :longURL => new_long_url, :login => @@login, :apiKey => @@key })
|
|
@@ -80,6 +94,12 @@ class Bitly
|
|
|
80
94
|
JSON.parse(response)
|
|
81
95
|
end
|
|
82
96
|
|
|
97
|
+
def Bitly.get_clicks(new_short_url)
|
|
98
|
+
Bitly.load_personal_data
|
|
99
|
+
response = RestClient.get("#{REST_API_URL}#{ACTION_PATH[:clicks]}?login=#{@@login}&apiKey=#{@@key}&shortUrl=#{new_short_url}")
|
|
100
|
+
JSON.parse(response)
|
|
101
|
+
end
|
|
102
|
+
|
|
83
103
|
def Bitly.shorten(new_long_url)
|
|
84
104
|
bitly = Bitly.new
|
|
85
105
|
bitly.long_url = new_long_url
|
|
@@ -93,4 +113,11 @@ class Bitly
|
|
|
93
113
|
bitly.expand
|
|
94
114
|
bitly
|
|
95
115
|
end
|
|
116
|
+
|
|
117
|
+
def Bitly.clicks(new_short_url)
|
|
118
|
+
bitly = Bitly.new
|
|
119
|
+
bitly.short_url = new_short_url
|
|
120
|
+
bitly.clicks
|
|
121
|
+
bitly
|
|
122
|
+
end
|
|
96
123
|
end
|
data/ruby-bitly.gemspec
CHANGED
data/spec/ruby-bitly_spec.rb
CHANGED
|
@@ -10,7 +10,7 @@ describe "RubyBitly" do
|
|
|
10
10
|
|
|
11
11
|
it "Shorten log url staticaly and return a hash" do
|
|
12
12
|
response = Bitly.post_shorten "http://google.com"
|
|
13
|
-
|
|
13
|
+
|
|
14
14
|
response["status_code"].should == 200
|
|
15
15
|
response["status_txt"].should == "OK"
|
|
16
16
|
response["data"]["new_hash"].should == 0
|
|
@@ -49,7 +49,7 @@ describe "RubyBitly" do
|
|
|
49
49
|
url = Bitly.new
|
|
50
50
|
url.short_url = "http://bit.ly/bcvNe5"
|
|
51
51
|
url.expand
|
|
52
|
-
|
|
52
|
+
|
|
53
53
|
url.long_url.should == "http://google.com"
|
|
54
54
|
url.short_url.should == "http://bit.ly/bcvNe5"
|
|
55
55
|
url.global_hash.should == "zzzzzzz"
|
|
@@ -68,7 +68,7 @@ describe "RubyBitly" do
|
|
|
68
68
|
it "Shorten url and return an object" do
|
|
69
69
|
url = Bitly.shorten("http://google.com")
|
|
70
70
|
url.should be_an_instance_of(Bitly)
|
|
71
|
-
|
|
71
|
+
|
|
72
72
|
url.status_code.should == 200
|
|
73
73
|
url.status_txt.should == "OK"
|
|
74
74
|
url.long_url.should == "http://google.com"
|
|
@@ -93,6 +93,11 @@ describe "RubyBitly" do
|
|
|
93
93
|
url.read_only?.should be true
|
|
94
94
|
end
|
|
95
95
|
|
|
96
|
+
it "Stats object should be read only" do
|
|
97
|
+
url = Bitly.clicks "http://bit.ly/bcvNe5"
|
|
98
|
+
url.read_only?.should be true
|
|
99
|
+
end
|
|
100
|
+
|
|
96
101
|
it "Attribute long_url should be updatable when object is not read only" do
|
|
97
102
|
url = Bitly.new
|
|
98
103
|
url.long_url = "http://google.com"
|
|
@@ -111,11 +116,11 @@ describe "RubyBitly" do
|
|
|
111
116
|
url.long_url = "http://xlii.com.br"
|
|
112
117
|
url.long_url.should == "http://google.com"
|
|
113
118
|
end
|
|
114
|
-
|
|
119
|
+
|
|
115
120
|
it "Expand url and return an object" do
|
|
116
121
|
url = Bitly.expand "http://bit.ly/bcvNe5"
|
|
117
122
|
url.should be_an_instance_of(Bitly)
|
|
118
|
-
|
|
123
|
+
|
|
119
124
|
url.long_url.should == "http://google.com"
|
|
120
125
|
url.short_url.should == "http://bit.ly/bcvNe5"
|
|
121
126
|
url.global_hash.should == "zzzzzzz"
|
|
@@ -124,6 +129,19 @@ describe "RubyBitly" do
|
|
|
124
129
|
url.status_txt.should == "OK"
|
|
125
130
|
end
|
|
126
131
|
|
|
132
|
+
it "Get clicks and return an object" do
|
|
133
|
+
url = Bitly.clicks "http://bit.ly/xlii42"
|
|
134
|
+
url.should be_an_instance_of(Bitly)
|
|
135
|
+
|
|
136
|
+
url.status_code.should == 200
|
|
137
|
+
url.status_txt.should == "OK"
|
|
138
|
+
url.global_clicks.should be_an_instance_of Fixnum
|
|
139
|
+
url.user_clicks.should be_an_instance_of Fixnum
|
|
140
|
+
url.short_url.should == "http://bit.ly/xlii42"
|
|
141
|
+
url.global_hash == "cunZEP"
|
|
142
|
+
url.user_hash.should == "cT1Izu"
|
|
143
|
+
end
|
|
144
|
+
|
|
127
145
|
it "Attribute short_url should be updated when object is not read only" do
|
|
128
146
|
url = Bitly.new
|
|
129
147
|
url.short_url = "http://bit.ly/bcvNe5"
|
|
@@ -142,4 +160,30 @@ describe "RubyBitly" do
|
|
|
142
160
|
url.short_url = "http://bit.ly/bcvNe5"
|
|
143
161
|
url.short_url.should == "http://bit.ly/xlii42"
|
|
144
162
|
end
|
|
163
|
+
|
|
164
|
+
it "Get clicks by short_url" do
|
|
165
|
+
bitly = Bitly.get_clicks("http://bit.ly/xlii42")
|
|
166
|
+
|
|
167
|
+
bitly["status_txt"].should == "OK"
|
|
168
|
+
bitly["status_code"].should == 200
|
|
169
|
+
bitly["data"]["clicks"].first["global_clicks"].should be_an_instance_of Fixnum
|
|
170
|
+
bitly["data"]["clicks"].first["user_clicks"].should be_an_instance_of Fixnum
|
|
171
|
+
bitly["data"]["clicks"].first["short_url"].should == "http://bit.ly/xlii42"
|
|
172
|
+
bitly["data"]["clicks"].first["global_hash"].should == "cunZEP"
|
|
173
|
+
bitly["data"]["clicks"].first["user_hash"].should == "cT1Izu"
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
it "Clicks with an object" do
|
|
177
|
+
url = Bitly.new
|
|
178
|
+
url.short_url = "http://bit.ly/xlii42"
|
|
179
|
+
url.clicks
|
|
180
|
+
|
|
181
|
+
url.status_code.should == 200
|
|
182
|
+
url.status_txt.should == "OK"
|
|
183
|
+
url.global_clicks.should be_an_instance_of Fixnum
|
|
184
|
+
url.user_clicks.should be_an_instance_of Fixnum
|
|
185
|
+
url.short_url.should == "http://bit.ly/xlii42"
|
|
186
|
+
url.global_hash == "cunZEP"
|
|
187
|
+
url.user_hash.should == "cT1Izu"
|
|
188
|
+
end
|
|
145
189
|
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:
|
|
4
|
+
hash: 25
|
|
5
5
|
prerelease: false
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
8
|
- 0
|
|
9
|
-
-
|
|
10
|
-
version: 0.0.
|
|
9
|
+
- 3
|
|
10
|
+
version: 0.0.3
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- rafaeldx7
|