ruby-bitly 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. data/README +34 -0
  2. data/README.rdoc +30 -13
  3. data/Rakefile +6 -3
  4. data/VERSION +1 -1
  5. data/bin/bitly +139 -12
  6. data/lib/ruby-bitly.rb +48 -33
  7. data/ruby-bitly.gemspec +14 -5
  8. metadata +55 -7
data/README CHANGED
@@ -0,0 +1,34 @@
1
+ # This app is a simple bitly ruby client.
2
+
3
+ ## Examples
4
+ bitly -s http://long-url.com.whatever/
5
+ bitly -e http://bit.ly/whatever
6
+ bitly --user-clicks http://bit.ly/whatever
7
+ bitly --global-clicks http://bit.ly/whatever
8
+
9
+ ### Other examples:
10
+ bitly -q -s http://long-url.com.whatever/
11
+ bitly --verbose -e http://bit.ly/whatever
12
+
13
+ ## Usage
14
+ bitly [options] url
15
+
16
+ For help: bitly -h
17
+
18
+ ## Options
19
+ -s, --shorten Shorten a long url
20
+ -e, --expand Expand a short bitly url
21
+ -u, --user-clicks Show user clicks from the short bitly url
22
+ -g, --global-clicks Show global clicks from the short bitly url
23
+
24
+ -h, --help Displays help message, then exit
25
+ -v, --version Display the version, then exit
26
+ -q, --quiet Output as little as possible, overrides verbose
27
+ -V, --verbose Verbose output
28
+
29
+ ## Author
30
+ rafaeldx7 ~ rafaeldx7(a)gmail.com
31
+
32
+ ## Copyright
33
+ Copyright (c) 2010 rafaeldx7. Licensed under the MIT License:
34
+ http://www.opensource.org/licenses/mit-license.php
data/README.rdoc CHANGED
@@ -1,17 +1,34 @@
1
- = ruby-bitly
1
+ = This app is a simple bitly ruby client.
2
2
 
3
- Description goes here.
3
+ == Examples
4
+ bitly -s http://long-url.com.whatever/
5
+ bitly -e http://bit.ly/whatever
6
+ bitly --user-clicks http://bit.ly/whatever
7
+ bitly --global-clicks http://bit.ly/whatever
4
8
 
5
- == Note on Patches/Pull Requests
6
-
7
- * Fork the project.
8
- * Make your feature addition or bug fix.
9
- * Add tests for it. This is important so I don't break it in a
10
- future version unintentionally.
11
- * Commit, do not mess with rakefile, version, or history.
12
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
- * Send me a pull request. Bonus points for topic branches.
9
+ Other examples:
10
+ bitly -q -s http://long-url.com.whatever/
11
+ bitly --verbose -e http://bit.ly/whatever
14
12
 
15
- == Copyright
13
+ == Usage
14
+ bitly [options] url
16
15
 
17
- Copyright (c) 2010 Rafael. See LICENSE for details.
16
+ For help: bitly -h
17
+
18
+ == Options
19
+ -s, --shorten Shorten a long url
20
+ -e, --expand Expand a short bitly url
21
+ -u, --user-clicks Show user clicks from the short bitly url
22
+ -g, --global-clicks Show global clicks from the short bitly url
23
+
24
+ -h, --help Displays help message, then exit
25
+ -v, --version Display the version, then exit
26
+ -q, --quiet Output as little as possible, overrides verbose
27
+ -V, --verbose Verbose output
28
+
29
+ == Author
30
+ rafaeldx7 ~ rafaeldx7(a)gmail.com
31
+
32
+ == Copyright
33
+ Copyright (c) 2010 rafaeldx7. Licensed under the MIT License:
34
+ http://www.opensource.org/licenses/mit-license.php
data/Rakefile CHANGED
@@ -5,12 +5,15 @@ begin
5
5
  require 'jeweler'
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "ruby-bitly"
8
- gem.summary = %Q{bit.ly ruby client}
9
- gem.description = %Q{bit.ly ruby client}
8
+ gem.summary = %Q{This is a simple bit.ly ruby client}
9
+ gem.description = %Q{This is a simple bit.ly ruby client}
10
10
  gem.email = "rafaeldx7@gmail.com"
11
11
  gem.homepage = "http://github.com/rafaeldx7/ruby-bitly"
12
12
  gem.authors = ["rafaeldx7"]
13
- gem.add_development_dependency "rest-client", "1.5.1"
13
+ gem.add_runtime_dependency "rest-client", "1.5.1"
14
+ gem.add_runtime_dependency "json_pure", "1.4.3"
15
+ gem.add_development_dependency "rake", "0.8.7"
16
+ gem.add_development_dependency "rspec", "1.3.0"
14
17
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
18
  end
16
19
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/bin/bitly CHANGED
@@ -1,19 +1,146 @@
1
- #!/usr/bin/env ruby
1
+ #!/usr/bin/env ruby
2
+ # This app is a simple bitly ruby client.
3
+ #
4
+ # == Examples
5
+ # bitly -s http://long-url.com.whatever/
6
+ # bitly -e http://bit.ly/whatever
7
+ # bitly --user-clicks http://bit.ly/whatever
8
+ # bitly --global-clicks http://bit.ly/whatever
9
+ #
10
+ # Other examples:
11
+ # bitly -q -s http://long-url.com.whatever/
12
+ # bitly --verbose -e http://bit.ly/whatever
13
+ #
14
+ # == Usage
15
+ # bitly [options] url
16
+ #
17
+ # For help: bitly -h
18
+ #
19
+ # == Options
20
+ # -s, --shorten Shorten a long url
21
+ # -e, --expand Expand a short bitly url
22
+ # -u, --user-clicks Show user clicks from the short bitly url
23
+ # -g, --global-clicks Show global clicks from the short bitly url
24
+ #
25
+ # -h, --help Displays help message, then exit
26
+ # -v, --version Display the version, then exit
27
+ # -q, --quiet Output as little as possible, overrides verbose
28
+ # -V, --verbose Verbose output
29
+ #
30
+ # == Author
31
+ # rafaeldx7 ~ rafaeldx7(a)gmail.com
32
+ #
33
+ # == Copyright
34
+ # Copyright (c) 2010 rafaeldx7. Licensed under the MIT License:
35
+ # http://www.opensource.org/licenses/mit-license.php
2
36
 
3
37
  $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4
38
 
5
39
  require 'ruby-bitly'
40
+ require 'optparse'
41
+ require 'rdoc/usage'
42
+ require 'ostruct'
43
+ require 'date'
6
44
 
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
- 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
15
- else
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"
45
+ class App
46
+ VERSION = File.read(File.join(File.dirname(__FILE__), '..', 'VERSION'))
47
+
48
+ attr_reader :options
49
+
50
+ def initialize(arguments, stdin)
51
+ @arguments = arguments
52
+ @stdin = stdin
53
+
54
+ # Set defaults
55
+ @options = OpenStruct.new
56
+ @options.verbose = false
57
+ @options.quiet = false
58
+ end
59
+
60
+ # Parse options, check arguments, then process the command
61
+ def run
62
+
63
+ if parsed_options? && arguments_valid?
64
+
65
+ puts "Start at #{DateTime.now}\n\n" if @options.verbose
66
+
67
+ output_options if @options.verbose # [Optional]
68
+
69
+ process_arguments
70
+ puts process_command.to_s
71
+
72
+ puts "\nFinished at #{DateTime.now}" if @options.verbose
73
+
74
+ else
75
+ output_usage
76
+ end
77
+
78
+ end
79
+
80
+ protected
81
+
82
+ def parsed_options?
83
+
84
+ # Specify options
85
+ opts = OptionParser.new
86
+
87
+ opts.on('-s', '--shorten') { @options.shorten = true }
88
+ opts.on('-e', '--expand') { @options.expand = true }
89
+ opts.on('-u', '--user-clicks') { @options.user_clicks = true }
90
+ opts.on('-g', '--global-clicks') { @options.global_clicks = true }
91
+
92
+ opts.on('-v', '--version') { output_version ; exit 0 }
93
+ opts.on('-h', '--help') { output_help }
94
+ opts.on('-V', '--verbose') { @options.verbose = true }
95
+ opts.on('-q', '--quiet') { @options.quiet = true }
96
+
97
+ opts.parse!(@arguments) rescue return false
98
+
99
+ process_options
100
+ true
101
+ end
102
+
103
+ # Performs post-parse processing on options
104
+ def process_options
105
+ @options.verbose = false if @options.quiet
106
+ end
107
+
108
+ def output_options
109
+ puts "Options:\n"
110
+
111
+ @options.marshal_dump.each do |name, val|
112
+ puts " #{name} = #{val}"
113
+ end
114
+ end
115
+
116
+ # True if required arguments were provided
117
+ def arguments_valid?
118
+ @arguments.length == 1 and (@options.shorten or @options.expand or @options.user_clicks or @options.global_clicks)
119
+ end
120
+
121
+ # Setup the arguments
122
+ def process_arguments
123
+ end
124
+
125
+ def output_help
126
+ RDoc::usage() #exits app
127
+ end
128
+
129
+ def output_usage
130
+ RDoc::usage('usage') # gets usage from comments above
131
+ end
132
+
133
+ def output_version
134
+ puts "#{File.basename(__FILE__)} version #{VERSION}"
135
+ end
136
+
137
+ def process_command
138
+ return Bitly.shorten(@arguments[0]).short_url if @options.shorten
139
+ return Bitly.expand(@arguments[0]).long_url if @options.expand
140
+ return Bitly.clicks(@arguments[0]).user_clicks.to_s if @options.user_clicks
141
+ return Bitly.clicks(@arguments[0]).global_clicks.to_s if @options.global_clicks
142
+ end
17
143
  end
18
144
 
19
- puts result
145
+ app = App.new(ARGV, STDIN)
146
+ app.run
data/lib/ruby-bitly.rb CHANGED
@@ -43,61 +43,76 @@ class Bitly
43
43
 
44
44
  def shorten
45
45
  unless read_only?
46
- @response = Bitly.post_shorten(@long_url)
46
+ if @response = Bitly.post_shorten(@long_url)
47
+
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!
57
+ end
58
+ end
59
+ end
60
+
61
+ def expand
62
+ if @response = Bitly.post_expand(@short_url)
47
63
 
64
+ @long_url = @response["data"]["expand"].first["long_url"]
65
+ @global_hash = @response["data"]["expand"].first["global_hash"]
66
+ @user_hash = @response["data"]["expand"].first["user_hash"]
48
67
  @status_code = @response["status_code"]
49
68
  @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
69
 
56
70
  read_only_now!
57
71
  end
58
72
  end
59
73
 
60
- def expand
61
- @response = Bitly.post_expand(@short_url)
74
+ def clicks
75
+ if @response = Bitly.get_clicks(@short_url)
62
76
 
63
- @long_url = @response["data"]["expand"].first["long_url"]
64
- @global_hash = @response["data"]["expand"].first["global_hash"]
65
- @user_hash = @response["data"]["expand"].first["user_hash"]
66
- @status_code = @response["status_code"]
67
- @status_txt = @response["status_txt"]
77
+ @global_clicks = @response["data"]["clicks"].first["global_clicks"]
78
+ @user_clicks = @response["data"]["clicks"].first["user_clicks"]
79
+ @global_hash = @response["data"]["clicks"].first["global_hash"]
80
+ @user_hash = @response["data"]["clicks"].first["user_hash"]
81
+ @status_code = @response["status_code"]
82
+ @status_txt = @response["status_txt"]
68
83
 
69
- read_only_now!
84
+ read_only_now!
85
+ end
70
86
  end
71
87
 
72
- def clicks
73
- @response = Bitly.get_clicks(@short_url)
88
+ def Bitly.request
89
+ Bitly.load_personal_data
74
90
 
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!
91
+ begin
92
+ response = yield
93
+ JSON.parse(response)
94
+ rescue
95
+ response[:error] = "error"
96
+ end
83
97
  end
84
98
 
85
99
  def Bitly.post_shorten(new_long_url)
86
- Bitly.load_personal_data
87
- response = RestClient.post(REST_API_URL + ACTION_PATH[:shorten], { :longURL => new_long_url, :login => @@login, :apiKey => @@key })
88
- JSON.parse(response)
100
+ Bitly.request do
101
+ RestClient.post(REST_API_URL + ACTION_PATH[:shorten], { :longURL => new_long_url, :login => @@login, :apiKey => @@key })
102
+ end
89
103
  end
90
104
 
91
105
  def Bitly.post_expand(new_short_url)
92
- Bitly.load_personal_data
93
- response = RestClient.post(REST_API_URL + ACTION_PATH[:expand], { :shortURL => new_short_url, :login => @@login, :apiKey => @@key })
94
- JSON.parse(response)
106
+ Bitly.request do
107
+ RestClient.post(REST_API_URL + ACTION_PATH[:expand], { :shortURL => new_short_url, :login => @@login, :apiKey => @@key })
108
+ end
95
109
  end
96
110
 
97
111
  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)
112
+ Bitly.request do
113
+ RestClient.get("#{REST_API_URL}#{ACTION_PATH[:clicks]}?login=#{@@login}&apiKey=#{@@key}&shortUrl=#{new_short_url}")
114
+ # RestClient.get(REST_API_URL + ACTION_PATH[:clicks], { :login => @@login, :apiKey => @@key, :shortUrl => new_short_url })
115
+ end
101
116
  end
102
117
 
103
118
  def Bitly.shorten(new_long_url)
data/ruby-bitly.gemspec CHANGED
@@ -5,13 +5,13 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruby-bitly}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
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-02}
12
+ s.date = %q{2010-08-04}
13
13
  s.default_executable = %q{bitly}
14
- s.description = %q{bit.ly ruby client}
14
+ s.description = %q{This is a simple bit.ly ruby client}
15
15
  s.email = %q{rafaeldx7@gmail.com}
16
16
  s.executables = ["bitly"]
17
17
  s.extra_rdoc_files = [
@@ -38,7 +38,7 @@ Gem::Specification.new do |s|
38
38
  s.rdoc_options = ["--charset=UTF-8"]
39
39
  s.require_paths = ["lib"]
40
40
  s.rubygems_version = %q{1.3.7}
41
- s.summary = %q{bit.ly ruby client}
41
+ s.summary = %q{This is a simple bit.ly ruby client}
42
42
  s.test_files = [
43
43
  "spec/ruby-bitly_spec.rb",
44
44
  "spec/spec_helper.rb"
@@ -49,12 +49,21 @@ Gem::Specification.new do |s|
49
49
  s.specification_version = 3
50
50
 
51
51
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
- s.add_development_dependency(%q<rest-client>, ["= 1.5.1"])
52
+ s.add_runtime_dependency(%q<rest-client>, ["= 1.5.1"])
53
+ s.add_runtime_dependency(%q<json_pure>, ["= 1.4.3"])
54
+ s.add_development_dependency(%q<rake>, ["= 0.8.7"])
55
+ s.add_development_dependency(%q<rspec>, ["= 1.3.0"])
53
56
  else
54
57
  s.add_dependency(%q<rest-client>, ["= 1.5.1"])
58
+ s.add_dependency(%q<json_pure>, ["= 1.4.3"])
59
+ s.add_dependency(%q<rake>, ["= 0.8.7"])
60
+ s.add_dependency(%q<rspec>, ["= 1.3.0"])
55
61
  end
56
62
  else
57
63
  s.add_dependency(%q<rest-client>, ["= 1.5.1"])
64
+ s.add_dependency(%q<json_pure>, ["= 1.4.3"])
65
+ s.add_dependency(%q<rake>, ["= 0.8.7"])
66
+ s.add_dependency(%q<rspec>, ["= 1.3.0"])
58
67
  end
59
68
  end
60
69
 
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: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
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-02 00:00:00 -03:00
18
+ date: 2010-08-04 00:00:00 -03:00
19
19
  default_executable: bitly
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -32,9 +32,57 @@ dependencies:
32
32
  - 5
33
33
  - 1
34
34
  version: 1.5.1
35
- type: :development
35
+ type: :runtime
36
36
  version_requirements: *id001
37
- description: bit.ly ruby client
37
+ - !ruby/object:Gem::Dependency
38
+ name: json_pure
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - "="
44
+ - !ruby/object:Gem::Version
45
+ hash: 1
46
+ segments:
47
+ - 1
48
+ - 4
49
+ - 3
50
+ version: 1.4.3
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rake
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - "="
60
+ - !ruby/object:Gem::Version
61
+ hash: 49
62
+ segments:
63
+ - 0
64
+ - 8
65
+ - 7
66
+ version: 0.8.7
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - "="
76
+ - !ruby/object:Gem::Version
77
+ hash: 27
78
+ segments:
79
+ - 1
80
+ - 3
81
+ - 0
82
+ version: 1.3.0
83
+ type: :development
84
+ version_requirements: *id004
85
+ description: This is a simple bit.ly ruby client
38
86
  email: rafaeldx7@gmail.com
39
87
  executables:
40
88
  - bitly
@@ -91,7 +139,7 @@ rubyforge_project:
91
139
  rubygems_version: 1.3.7
92
140
  signing_key:
93
141
  specification_version: 3
94
- summary: bit.ly ruby client
142
+ summary: This is a simple bit.ly ruby client
95
143
  test_files:
96
144
  - spec/ruby-bitly_spec.rb
97
145
  - spec/spec_helper.rb