rviki 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/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
- == 2012-04-10 14:00:00 +0000 Nia M <nia@viki.com>
1
+ == 2012-04-17 14:00:00 +0000 Nia M <nmutiara@gmail.com>
2
+
3
+ * Wrap API V2 with RViki::Client
4
+ * Created printer class to format server response output
5
+ * Wrap API V2 with command line interface
2
6
 
3
- * Created rviki
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # RViki readme
1
+ # RViki 0.0.1 readme
2
2
 
3
3
 
4
4
  RViki is a Ruby client gem for Viki.com API.
@@ -24,7 +24,7 @@ RViki is a Ruby client gem for Viki.com API.
24
24
 
25
25
  Clipboard output is only for Mac, for now.
26
26
 
27
- ### Woot! Command Line!
27
+ ### Command Line
28
28
 
29
29
  $ rviki shows
30
30
  $ rviki shows_item 50
@@ -32,3 +32,4 @@ Clipboard output is only for Mac, for now.
32
32
  $ rviki shows -f pretty_json 50
33
33
  $ rviki shows -o clipboard,stdout 50
34
34
 
35
+
data/Rakefile CHANGED
@@ -32,5 +32,5 @@ task :build do
32
32
  end
33
33
 
34
34
  task :release => :build do
35
- system "gem push rviki-#{RViki::VERSION::STRING}"
35
+ system "gem push rviki-#{RViki::VERSION::STRING}.gem"
36
36
  end
@@ -6,7 +6,7 @@ begin
6
6
  $:.unshift File.expand_path("../lib", bin_file)
7
7
 
8
8
  require "rviki/cli"
9
- cli = RViki::Cli.new
9
+ cli = RViki::Cli1.new
10
10
  cli.execute(*ARGV)
11
11
  rescue Interrupt
12
12
  `stty icanon echo`
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require "pathname"
5
+ bin_file = Pathname.new(__FILE__).realpath
6
+ $:.unshift File.expand_path("../lib", bin_file)
7
+
8
+ require "rviki/cli"
9
+ cli = RViki::Cli2.new
10
+ cli.execute(*ARGV)
11
+ rescue Interrupt
12
+ `stty icanon echo`
13
+ puts("\n ! Command cancelled.")
14
+ end
15
+
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require "pathname"
5
+ bin_file = Pathname.new(__FILE__).realpath
6
+ $:.unshift File.expand_path("../lib", bin_file)
7
+
8
+ require "rviki/cli"
9
+ cli = RViki::Cli3.new
10
+ cli.execute(*ARGV)
11
+ rescue Interrupt
12
+ `stty icanon echo`
13
+ puts("\n ! Command cancelled.")
14
+ end
15
+
@@ -1,4 +1,4 @@
1
1
  major:0
2
2
  minor:0
3
- patch:1
3
+ patch:2
4
4
  build:
@@ -2,27 +2,48 @@
2
2
 
3
3
  require 'optparse'
4
4
  require 'rviki'
5
+ require 'rviki/registrable'
5
6
 
6
7
  module RViki
7
- module Registrable
8
- def self.included(base)
9
- base.send :extend, ClassMethods
10
- end
8
+ class Cli1
9
+ include Registrable
11
10
 
12
- module ClassMethods
13
- def register_command(command_sym, args)
14
- registered_commands[command_sym] = args
15
- end
11
+ api_version "V1"
12
+ binary_name "rviki1"
13
+ examples <<-EOE
14
+ $ #{binary_name} shows -o clipboard
15
+ $ #{binary_name} shows_item 50
16
+ $ #{binary_name} shows_item 50 es
17
+ $ #{binary_name} shows -f pretty_json 50
18
+ $ #{binary_name} shows -o clipboard,stdout 50
19
+ EOE
16
20
 
17
- def registered_commands
18
- @registered_commands ||= {}
19
- end
21
+ def initialize
22
+ @client = RViki::ClientV1.new
20
23
  end
24
+
25
+ register_command :shows, [:language_code]
26
+ register_command :shows_item, [:id, :language_code]
21
27
  end
22
28
 
23
- class Cli
29
+ class Cli2
24
30
  include Registrable
25
31
 
32
+ api_version "V2"
33
+ binary_name "rviki2"
34
+ examples <<-EOE
35
+ $ #{binary_name} featured
36
+ $ #{binary_name} shows -o clipboard
37
+ $ #{binary_name} shows_item 50
38
+ $ #{binary_name} shows_item 50 es
39
+ $ #{binary_name} shows -f pretty_json 50
40
+ $ #{binary_name} shows -o clipboard,stdout 50
41
+ EOE
42
+
43
+ def initialize
44
+ @client = RViki::ClientV2.new
45
+ end
46
+
26
47
  register_command :shows, [:language_code]
27
48
  register_command :shows_item, [:id, :language_code]
28
49
  register_command :featured, []
@@ -38,101 +59,21 @@ module RViki
38
59
  register_command :video_casts, [:id]
39
60
  register_command :video_parts, [:id]
40
61
  register_command :video_posts, [:id]
62
+ end
41
63
 
42
- def initialize
43
- @client = RViki::Client.new
44
- @printer = RViki::Printer.new([:stdout])
45
-
46
- @print_opts_parser = OptionParser.new do |pop|
47
- pop.banner = "Print Options:"
48
- pop.on("-f", "--format FORMAT", [:tabular, :pretty_json, :pretty_ruby],
49
- "Print with FORMAT (tabular (only for array), pretty_json, or pretty_ruby)") do |pf|
50
- printer.format = pf
51
- end
52
-
53
- pop.on("-o", "--output clipboard,stdout", Array,
54
- "Output to OUTPUT_TARGET (clipboard, stdout)") do |o|
55
- o = o.map(&:to_sym)
56
- if (o.include?(:clipboard) || o.include?(:stdout))
57
- printer.targets = o
58
- end
59
- end
60
- end
61
- end
62
-
63
- def execute(*args)
64
- @cmd = args.shift
65
- @cmd = @cmd.to_sym if @cmd
66
- unless valid_command?
67
- puts <<-EOL
68
-
69
- Command not found: #{@cmd}
70
- ----------------------------------------
71
-
72
- RViki Version #{RViki::VERSION::STRING} for Viki API V2
73
- Usage: rviki <api_endpoint> [--opt optvalue] [api_param1] [api_param2]
74
-
75
- Available API Endpoints:
76
- #{commands_listing}
77
- #{print_opts_parser}
78
-
79
- Examples:
80
-
81
- $ rviki featured
82
- $ rviki shows -o clipboard
83
- $ rviki shows_item 50
84
- $ rviki shows_item 50 es
85
- $ rviki shows -f pretty_json 50
86
- $ rviki shows -o clipboard,stdout 50
87
-
88
- EOL
89
- exit(1)
90
- end
91
-
92
- @cmd_args = self.class.registered_commands[@cmd]
93
- extract_options args
94
- response = client.send(@cmd, client_options_hash)
95
- printer.object = response.parsed_response
96
- printer.do_print
97
- end
98
-
99
- protected
100
-
101
- attr_reader :client, :printer, :client_options, :print_opts_parser
102
-
103
- def commands_listing
104
- result = ""
105
- self.class.registered_commands.each do |endpoint, params|
106
- result += " #{endpoint}"
107
- result += "\n params: #{params.map{|pr| "[:#{pr}]"}.join(' ')}" if params && !params.empty?
108
- result += "\n"
109
- end
110
- result
111
- end
64
+ class Cli3
65
+ include Registrable
112
66
 
113
- def extract_options(args)
114
- client_opts_i = 0
115
- args.each_with_index do |a, i|
116
- client_opts_i += 2 if a.start_with?("-")
117
- end
67
+ api_version "V3"
68
+ binary_name "rviki3"
69
+ examples <<-EOE
70
+ EOE
118
71
 
119
- @printer_options = args[0...client_opts_i]
120
- print_opts_parser.parse! @printer_options
121
- @client_options = args[client_opts_i..-1]
122
- true
123
- end
124
-
125
- def client_options_hash
126
- options = {}
127
- client_options.each_with_index do |a, i|
128
- options[@cmd_args[i]] = a
129
- end
130
- options
72
+ def initialize
73
+ @client = RViki::ClientV3.new
131
74
  end
132
75
 
133
- def valid_command?
134
- @cmd && !@cmd.empty? && self.class.registered_commands.has_key?(@cmd)
135
- end
76
+ register_command :custom, [:hash]
136
77
  end
137
78
  end
138
79
 
@@ -1,6 +1,17 @@
1
1
  # encoding: utf-8
2
2
  module RViki
3
- class Client
3
+ class ClientV1
4
+ include Routable
5
+
6
+ base_uri 'http://www.viki.com/api/v1'
7
+ headers "Content-Type" => "application/json", "User-Agent" => "VikiMobile/4.0 RViki::Client"
8
+ format :json
9
+
10
+ route_get "/shows.json", as: :shows
11
+ route_get "/shows/:id.json", as: :shows_item
12
+ end
13
+
14
+ class ClientV2
4
15
  include Routable
5
16
 
6
17
  base_uri 'http://www.viki.com/api/v2'
@@ -23,5 +34,17 @@ module RViki
23
34
  route_get "/videos/:id/parts.json", as: :video_parts
24
35
  route_get "/videos/:id/recommended.json", as: :video_posts
25
36
  end
37
+
38
+ class ClientV3
39
+ include Routable
40
+
41
+ base_uri 'http://www.viki.com/api/v3'
42
+ headers "Content-Type" => "application/json", "User-Agent" => "VikiMobile/4.0 RViki::Client"
43
+ format :json
44
+
45
+ route_get "/custom.json", as: :custom
46
+ end
47
+
48
+ Client = ClientV2
26
49
  end
27
50
 
@@ -0,0 +1,132 @@
1
+ # encoding: utf-8
2
+ module Registrable
3
+ def self.included(base)
4
+ base.send :extend, ClassMethods
5
+ base.send :include, InstanceMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def register_command(command_sym, args)
10
+ registered_commands[command_sym] = args
11
+ end
12
+
13
+ def registered_commands
14
+ @registered_commands ||= {}
15
+ end
16
+
17
+ def api_version(version=nil)
18
+ if version.is_a?(String)
19
+ @api_version = version
20
+ end
21
+ @api_version
22
+ end
23
+
24
+ def binary_name(name=nil)
25
+ if name.is_a?(String)
26
+ @binary_name = name
27
+ end
28
+ @binary_name
29
+ end
30
+
31
+ def examples(ex=nil)
32
+ if ex.is_a?(String)
33
+ @examples = ex
34
+ end
35
+ @examples
36
+ end
37
+ end
38
+
39
+ module InstanceMethods
40
+ def prepare_printer
41
+ @printer = RViki::Printer.new([:stdout])
42
+
43
+ @print_opts_parser = OptionParser.new do |pop|
44
+ pop.banner = "Print Options:"
45
+ pop.on("-f", "--format FORMAT", [:tabular, :pretty_json, :pretty_ruby],
46
+ "Print with FORMAT (tabular (only for array), pretty_json, or pretty_ruby)") do |pf|
47
+ printer.format = pf
48
+ end
49
+
50
+ pop.on("-o", "--output clipboard,stdout", Array,
51
+ "Output to OUTPUT_TARGET (clipboard, stdout)") do |o|
52
+ o = o.map(&:to_sym)
53
+ if (o.include?(:clipboard) || o.include?(:stdout))
54
+ printer.targets = o
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ def execute(*args)
61
+ prepare_printer
62
+
63
+ @cmd = args.shift
64
+ @cmd = @cmd.to_sym if @cmd
65
+ unless valid_command?
66
+ puts <<-EOL
67
+
68
+ Command not found: #{@cmd}
69
+ ----------------------------------------
70
+
71
+ RViki Version #{RViki::VERSION::STRING} for Viki API #{self.class.api_version}
72
+ Usage: #{self.class.binary_name} <api_endpoint> [--opt optvalue] [api_param1] [api_param2]
73
+
74
+ Available API Endpoints:
75
+ #{commands_listing}
76
+ #{print_opts_parser}
77
+
78
+ Examples:
79
+
80
+ #{self.class.examples}
81
+
82
+ EOL
83
+ exit(1)
84
+ end
85
+
86
+ @cmd_args = self.class.registered_commands[@cmd]
87
+ extract_options args
88
+ response = client.send(@cmd, client_options_hash)
89
+ printer.object = response.parsed_response
90
+ printer.do_print
91
+ end
92
+
93
+ protected
94
+
95
+ attr_reader :client, :printer, :client_options, :print_opts_parser
96
+
97
+ def commands_listing
98
+ result = ""
99
+ self.class.registered_commands.each do |endpoint, params|
100
+ result += " #{endpoint}"
101
+ result += "\n params: #{params.map{|pr| "[:#{pr}]"}.join(' ')}" if params && !params.empty?
102
+ result += "\n"
103
+ end
104
+ result
105
+ end
106
+
107
+ def extract_options(args)
108
+ client_opts_i = 0
109
+ args.each_with_index do |a, i|
110
+ client_opts_i += 2 if a.start_with?("-")
111
+ end
112
+
113
+ @printer_options = args[0...client_opts_i]
114
+ print_opts_parser.parse! @printer_options
115
+ @client_options = args[client_opts_i..-1]
116
+ true
117
+ end
118
+
119
+ def client_options_hash
120
+ options = {}
121
+ client_options.each_with_index do |a, i|
122
+ options[@cmd_args[i]] = a
123
+ end
124
+ options
125
+ end
126
+
127
+ def valid_command?
128
+ @cmd && !@cmd.empty? && self.class.registered_commands.has_key?(@cmd)
129
+ end
130
+ end
131
+ end
132
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rviki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-17 00:00:00.000000000Z
12
+ date: 2012-04-18 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
16
- requirement: &70117459607260 !ruby/object:Gem::Requirement
16
+ requirement: &70201890822080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70117459607260
24
+ version_requirements: *70201890822080
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70117459605860 !ruby/object:Gem::Requirement
27
+ requirement: &70201890821540 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.5.1
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70117459605860
35
+ version_requirements: *70201890821540
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: ZenTest
38
- requirement: &70117459605200 !ruby/object:Gem::Requirement
38
+ requirement: &70201890821040 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 4.5.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70117459605200
46
+ version_requirements: *70201890821040
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &70117459604300 !ruby/object:Gem::Requirement
49
+ requirement: &70201890820540 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.8.7
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70117459604300
57
+ version_requirements: *70201890820540
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: bundler
60
- requirement: &70117459602640 !ruby/object:Gem::Requirement
60
+ requirement: &70201890820040 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,12 +65,14 @@ dependencies:
65
65
  version: 1.0.12
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70117459602640
68
+ version_requirements: *70201890820040
69
69
  description: A short description
70
70
  email:
71
71
  - nia@viki.com
72
72
  executables:
73
- - rviki
73
+ - rviki1
74
+ - rviki2
75
+ - rviki3
74
76
  extensions: []
75
77
  extra_rdoc_files:
76
78
  - README.md
@@ -81,12 +83,15 @@ files:
81
83
  - lib/rviki/cli.rb
82
84
  - lib/rviki/client.rb
83
85
  - lib/rviki/printer.rb
86
+ - lib/rviki/registrable.rb
84
87
  - lib/rviki/routable.rb
85
88
  - lib/rviki/version.rb
86
89
  - lib/rviki.rb
87
90
  - lib/VERSION
88
91
  - CHANGELOG
89
- - bin/rviki
92
+ - bin/rviki1
93
+ - bin/rviki2
94
+ - bin/rviki3
90
95
  homepage: ''
91
96
  licenses:
92
97
  - ''
@@ -100,6 +105,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
105
  - - ! '>='
101
106
  - !ruby/object:Gem::Version
102
107
  version: '0'
108
+ segments:
109
+ - 0
110
+ hash: -2437546894109681493
103
111
  required_rubygems_version: !ruby/object:Gem::Requirement
104
112
  none: false
105
113
  requirements: