url_shortener 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -28,3 +28,13 @@
28
28
  * other api call methods now return a UrlShortener::Response object
29
29
  These changes are introduced to return the api call results in a flexible manner and provide more flexibility for manipulating result output
30
30
 
31
+ === 0.0.5 2009-11-17
32
+
33
+ * 3 additions:
34
+ * adds Hashie for convert hashe keys to method calls
35
+ * adds UrlShortener::Response::Stats
36
+ * adds some convenience methods to the returned results for stats
37
+
38
+ * 1 changes:
39
+ * api call method UrlShortener::Client#stats now returns a UrlShortener::Response::Stats object
40
+
data/Manifest.txt CHANGED
@@ -9,6 +9,7 @@ lib/url_shortener/interface.rb
9
9
  lib/url_shortener/response.rb
10
10
  lib/url_shortener/response/shorten.rb
11
11
  lib/url_shortener/response/expand.rb
12
+ lib/url_shortener/response/stats.rb
12
13
  spec/spec_helper.rb
13
14
  spec/url_shortener/authorize_spec.rb
14
15
  spec/url_shortener/client_spec.rb
@@ -16,6 +17,7 @@ spec/url_shortener/interface_spec.rb
16
17
  spec/url_shortener/response_spec.rb
17
18
  spec/url_shortener/response/shorten_spec.rb
18
19
  spec/url_shortener/response/expand_spec.rb
20
+ spec/url_shortener/response/stats_spec.rb
19
21
  features/request_failure.feature
20
22
  features/api_calls.feature
21
23
  features/support/env.rb
data/README.rdoc CHANGED
@@ -67,7 +67,6 @@ To use the url_shortener gem you need a bit.ly login and Api key.
67
67
 
68
68
  stats = client.stats(:hash => '1RmnUT') # => UrlShortener::Response object
69
69
  stats.result # => returns a hash of all data returned from bitly
70
- #TODO: ADD convenience methods for stats
71
70
 
72
71
  ===To get url info
73
72
 
data/lib/url_shortener.rb CHANGED
@@ -4,10 +4,12 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require 'rubygems'
5
5
  require 'httparty'
6
6
  require 'cgi'
7
+ require 'hashie'
7
8
  require 'url_shortener/error.rb'
8
9
  require 'url_shortener/authorize.rb'
9
10
  require 'url_shortener/client.rb'
10
11
  require 'url_shortener/interface.rb'
11
12
  require 'url_shortener/response.rb'
12
13
  require 'url_shortener/response/shorten.rb'
13
- require 'url_shortener/response/expand.rb'
14
+ require 'url_shortener/response/expand.rb'
15
+ require 'url_shortener/response/stats.rb'
@@ -33,7 +33,7 @@ module UrlShortener
33
33
  def stats(option)
34
34
  check_request_parameters(option)
35
35
  response = interface(nil, endpoint_with_options('stats',option)).get
36
- UrlShortener::Response.new(response)
36
+ UrlShortener::Response::Stats.new(response)
37
37
  end
38
38
 
39
39
  def info(option)
@@ -1,10 +1,11 @@
1
1
  module UrlShortener
2
2
  class Response
3
3
 
4
- attr_reader :result
4
+ attr_reader :result, :attributes
5
5
 
6
6
  def initialize(result)
7
7
  @result = result
8
+ @attributes = Hashie::Mash.new(result)
8
9
  end
9
10
 
10
11
  end
@@ -0,0 +1,69 @@
1
+ module UrlShortener
2
+ class Response::Stats < UrlShortener::Response
3
+
4
+ def initialize(response)
5
+ super
6
+ end
7
+
8
+ def clicks
9
+ attributes.clicks
10
+ end
11
+
12
+ def user_clicks
13
+ attributes.userClicks
14
+ end
15
+
16
+ def referrers
17
+ find_referrers
18
+ end
19
+
20
+ def user_referrers
21
+ find_referrers(true)
22
+ end
23
+
24
+ # There are user referrers and only referrers
25
+ def find_referrers(user_referrers=false)
26
+ return unless referrer_data?
27
+ base_referrer = user_referrers ? attributes.userReferrers : attributes.referrers
28
+ return unless base_referrer
29
+ referrer_values(base_referrer)
30
+ end
31
+
32
+ private
33
+
34
+ def referrer_values(base_referrer)
35
+ collect_referrers = []
36
+ base_referrer.nodeKeyVal.each { |base| collect_referrers << full_referrer_urls(base) if node_key?(base) }
37
+ collect_referrers.flatten
38
+ end
39
+
40
+ def full_referrer_urls(base)
41
+ return unless node_key?(base)
42
+ resources = base.nodeKeyVal.is_a?(Array) ? base.nodeKeyVal.collect{|node| node.nodeKey} : base.nodeKeyVal.nodeKey
43
+ referrer_urls(base.nodeKey, resources)
44
+ end
45
+
46
+ def node_key?(base)
47
+ !(base.nil? || base.nodeKeyVal.nil?)
48
+ end
49
+
50
+ def attributes_present?
51
+ !(attributes.nil? || attributes.empty?)
52
+ end
53
+
54
+ def referrers_present?(another_attr=nil)
55
+ return !(attributes.referrers.nil? || attributes.referrers.empty?) unless another_attr
56
+ !(attributes.send(:another_attr).nil? || attributes.send(:another_attr).empty?)
57
+ end
58
+
59
+ def referrer_data?
60
+ attributes_present? && referrers_present?
61
+ end
62
+
63
+ def referrer_urls(base_url, resources)
64
+ return base_url unless resources
65
+ resources.collect { |resource| "#{base_url}#{resource}" }
66
+ end
67
+
68
+ end
69
+ end
@@ -153,6 +153,10 @@ describe UrlShortener::Client do
153
153
  @interface.should_receive(:get)
154
154
  @client.stats(:hash => @hash)
155
155
  end
156
+
157
+ it "should return the UrlShortener::Response::Stats object" do
158
+ @client.stats(:hash => @hash).should be_instance_of(UrlShortener::Response::Stats)
159
+ end
156
160
  end
157
161
 
158
162
  describe "#info" do
@@ -0,0 +1,172 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe UrlShortener::Response::Stats do
4
+
5
+ describe "#clicks" do
6
+ before(:each) do
7
+ @hashie = stub('Hashie::Mash')
8
+ @response = {:referrers => {:key => :val}, :clicks => '23'}
9
+ @stats = UrlShortener::Response::Stats.new(@response)
10
+ @stats.stub!(:attributes).and_return(@hashie)
11
+ @hashie.stub!(:clicks)
12
+ end
13
+
14
+ it "should get the hash attributes" do
15
+ @stats.should_receive(:attributes).and_return(@hashie)
16
+ @stats.clicks
17
+ end
18
+
19
+ it "should get the clicks from the hash attributes" do
20
+ @hashie.should_receive(:clicks)
21
+ @stats.clicks
22
+ end
23
+
24
+ it "should return nil when response is empty" do
25
+ response = {}
26
+ @stats = UrlShortener::Response::Stats.new(response)
27
+ @stats.clicks.should eql(nil)
28
+ end
29
+ end
30
+
31
+ describe "#user_clicks" do
32
+ before(:each) do
33
+ @hashie = stub('Hashie::Mash')
34
+ @response = {:referrers => {:key => :val}, :clicks => '23', :userClicks => '233'}
35
+ @stats = UrlShortener::Response::Stats.new(@response)
36
+ @stats.stub!(:attributes).and_return(@hashie)
37
+ @hashie.stub!(:userClicks).and_return('233')
38
+ end
39
+
40
+ it "should get the hash attributes" do
41
+ @stats.should_receive(:attributes).and_return(@hashie)
42
+ @stats.user_clicks
43
+ end
44
+
45
+ it "should get the user clicks from the hash attributes" do
46
+ @hashie.should_receive(:userClicks)
47
+ @stats.user_clicks
48
+ end
49
+
50
+ it "should return userClicks" do
51
+ @stats.user_clicks.should eql('233')
52
+ end
53
+
54
+ it "should return nil when response is empty" do
55
+ response = {}
56
+ @stats = UrlShortener::Response::Stats.new(response)
57
+ @stats.user_clicks.should eql(nil)
58
+ end
59
+ end
60
+
61
+ describe "#referrers" do
62
+ before(:each) do
63
+ @response = {:referrers => {:nodeKeyVal => {:nodeKey => 'val'}}}
64
+ @stats = UrlShortener::Response::Stats.new(@response) end
65
+
66
+ it "should get the hash attributes" do
67
+ @stats.should_receive(:find_referrers)
68
+ @stats.referrers
69
+ end
70
+
71
+ end
72
+
73
+ describe "#user_referrers" do
74
+ before(:each) do
75
+ @response = {:referrers => {:nodeKeyVal => {:nodeKey => 'val'}}}
76
+ @stats = UrlShortener::Response::Stats.new(@response)
77
+ end
78
+
79
+ it "should get the hash attributes" do
80
+ @stats.should_receive(:find_referrers).with(true)
81
+ @stats.user_referrers
82
+ end
83
+
84
+ end
85
+
86
+ describe "#find_referrers" do
87
+ before(:each) do
88
+ @response = {:referrers => {:nodeKeyVal => {:nodeKey => 'val'}}}
89
+ @stats = UrlShortener::Response::Stats.new(@response)
90
+ end
91
+
92
+ it "should check if referrer data is present " do
93
+ @stats.should_receive(:referrer_data?)
94
+ @stats.find_referrers
95
+ end
96
+
97
+ context "when no referrer data present" do
98
+ before(:each) do
99
+ @stats.stub!(:referrer_data?)
100
+ end
101
+ it "should return nil" do
102
+ @stats.find_referrers.should eql(nil)
103
+ end
104
+
105
+ it "should try to find the attributes" do
106
+ @stats.should_receive(:attributes).never
107
+ @stats.find_referrers
108
+ end
109
+
110
+ it "should not try to get the referrer values" do
111
+ @stats.should_receive(:referrer_values).never
112
+ @stats.find_referrers
113
+ end
114
+ end
115
+
116
+ context "when referrer data is present but referrer element is not" do
117
+ before(:each) do
118
+ @hashie = stub('Hashie::Mash')
119
+ @stats.stub!(:referrer_data?).and_return(true)
120
+ @stats.stub!(:attributes).and_return(@hashie)
121
+ @hashie.stub!(:referrers)
122
+ @hashie.stub!(:userReferrers)
123
+ end
124
+
125
+ it "should find the attributes" do
126
+ @stats.should_receive(:attributes).times.at_least(1)
127
+ @stats.find_referrers
128
+ end
129
+
130
+ it "should get referrers if true is not passed in parameter for user referrers" do
131
+ @hashie.should_receive(:referrers)
132
+ @stats.find_referrers
133
+ end
134
+
135
+ it "should not get user referrers if true is not passed in parameter for user referrers" do
136
+ @hashie.should_receive(:userReferrers).never
137
+ @stats.find_referrers
138
+ end
139
+
140
+ it "should get user referrers if true is passed in parameter for user referrers" do
141
+ @hashie.should_receive(:userReferrers)
142
+ @stats.find_referrers(true)
143
+ end
144
+
145
+ it "should not get referrers if true is passed in parameter for user referrers" do
146
+ @hashie.should_receive(:referrers).never
147
+ @stats.find_referrers(true)
148
+ end
149
+
150
+ it "should not get referrer values when referrers are not present" do
151
+ @stats.should_receive(:referrer_values).never
152
+ @stats.find_referrers
153
+ end
154
+ end
155
+
156
+ context "when both referrer data referrer element is present" do
157
+ before(:each) do
158
+ @hashie = stub('Hashie::Mash')
159
+ @stats.stub!(:referrer_data?).and_return(true)
160
+ @stats.stub!(:attributes).and_return(@hashie)
161
+ @hashie.stub!(:referrers).and_return(@hashie)
162
+ end
163
+
164
+ it "should get referrer values when referrers are present" do
165
+ @stats.should_receive(:referrer_values).with(@hashie)
166
+ @stats.find_referrers
167
+ end
168
+ end
169
+
170
+ end
171
+
172
+ end
@@ -4,12 +4,24 @@ describe UrlShortener::Response do
4
4
 
5
5
  describe ".new" do
6
6
  before(:each) do
7
- @response = {:a_key => 'value'}
7
+ @response_hash = {:a_key => 'value'}
8
+ @attribute = stub('Hashie::Mash')
9
+ Hashie::Mash.stub!(:new).and_return(@attribute)
10
+ @response = UrlShortener::Response.new(@response_hash)
8
11
  end
9
12
 
10
- it "should set the result attribute" do
11
- response = UrlShortener::Response.new(@response)
12
- response.result.should eql(@response)
13
+ it "should set the result method" do
14
+ @response.result.should eql(@response_hash)
15
+ end
16
+
17
+ it "should set the attrubutes" do
18
+ @response.attributes.should eql(@attribute)
19
+ end
20
+
21
+ it "should initialize a new Hashie::Mash attribute" do
22
+ Hashie::Mash.should_receive(:new).with(@response_hash)
23
+ @response.attributes
24
+ UrlShortener::Response.new(@response_hash)
13
25
  end
14
26
  end
15
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: url_shortener
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nasir Jamal
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-15 00:00:00 +00:00
12
+ date: 2009-11-17 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -18,10 +18,20 @@ dependencies:
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
20
20
  requirements:
21
- - - ">="
21
+ - - "="
22
22
  - !ruby/object:Gem::Version
23
23
  version: 0.4.5
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hashie
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.4
34
+ version:
25
35
  description: Url Shortener is a Ruby library / gem and API wrapper for bit.ly to shorten/expand the urls and retrieve other information about them.
26
36
  email: nas35_in@yahoo.com
27
37
  executables: []
@@ -42,6 +52,7 @@ files:
42
52
  - lib/url_shortener/response.rb
43
53
  - lib/url_shortener/response/shorten.rb
44
54
  - lib/url_shortener/response/expand.rb
55
+ - lib/url_shortener/response/stats.rb
45
56
  has_rdoc: true
46
57
  homepage: http://github.com/nas/url_shortener
47
58
  licenses: []
@@ -78,6 +89,7 @@ test_files:
78
89
  - spec/url_shortener/response_spec.rb
79
90
  - spec/url_shortener/response/shorten_spec.rb
80
91
  - spec/url_shortener/response/expand_spec.rb
92
+ - spec/url_shortener/response/stats_spec.rb
81
93
  - features/api_calls.feature
82
94
  - features/request_failure.feature
83
95
  - features/support/env.rb