previews 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/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ .DS_Store
2
+ results.html
3
+ pkg
4
+ html
5
+ .bundle
6
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in previews.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ivan K.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # previews
2
+ Read user reviews of linux packages.
3
+
4
+ **Author:** Ivan K.
5
+
6
+ [Source on Github](https://github.com/divout/previews)
7
+
8
+ ## Installation:
9
+ gem install previews
10
+ ## Usage:
11
+ previews vlc --sort-rating asc --lang es
12
+ Type `previews --help` for full list of options.
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require 'bundler'
2
+ require 'rake/clean'
3
+ require 'rake/testtask'
4
+ require 'cucumber'
5
+ require 'cucumber/rake/task'
6
+ gem 'rdoc' # we need the installed RDoc gem, not the system one
7
+ require 'rdoc/task'
8
+
9
+ include Rake::DSL
10
+
11
+ Bundler::GemHelper.install_tasks
12
+
13
+ Rake::TestTask.new do |t|
14
+ t.pattern = 'test/tc_*.rb'
15
+ end
16
+
17
+ CUKE_RESULTS = 'results.html'
18
+ CLEAN << CUKE_RESULTS
19
+ Cucumber::Rake::Task.new(:features) do |t|
20
+ t.cucumber_opts = "features --format html -o #{CUKE_RESULTS} --format progress -x"
21
+ t.fork = false
22
+ end
23
+
24
+ Rake::RDocTask.new do |rd|
25
+
26
+ rd.main = "README.rdoc"
27
+
28
+ rd.rdoc_files.include("README.rdoc","lib/**/*.rb","bin/**/*")
29
+ end
30
+
31
+ task :default => [:test,:features]
data/bin/previews ADDED
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ $LOAD_PATH << File.join(File.dirname(__FILE__), *%w[.. lib])
4
+
5
+ require 'optparse'
6
+ require 'previews'
7
+
8
+ HEADERS = { 'origin' => 'Origin',
9
+ 'rating' => 'Rating',
10
+ 'app_name' => 'Application Name',
11
+ 'language' => 'Language',
12
+ 'reviewer_username' => 'Username',
13
+ 'reviewer_displayname' => 'From',
14
+ 'summary' => 'Summary',
15
+ 'review_text' => 'Review',
16
+ 'version' => 'Version',
17
+ 'date_created' => 'Date',
18
+ 'package_name' => 'Package Name',
19
+ 'distroseries' => 'distroseries' }
20
+
21
+ options = {
22
+ :fields => ['rating', 'reviewer_displayname', 'summary', 'review_text'],
23
+ :release => 'oneiric',
24
+ :locale => 'en',
25
+ :package => ARGV[0],
26
+ :system_less => true,
27
+ :sort_field => nil,
28
+ :sort_direction => nil
29
+ }
30
+
31
+ OptionParser.new do |opts|
32
+ opts.banner = 'Usage: previews PACKAGE [options]'
33
+
34
+ opts.separator ""
35
+ opts.separator "Options:"
36
+
37
+ sort_options = ['asc', 'desc', 'ASC', 'DESC']
38
+ opts.on('-d', '--sort-date DIRECTION', sort_options, 'Sort reviews by date. DIRECTION could be DESC or ASC') do |direction|
39
+ options[:sort_field] = 'date_created'
40
+ options[:sort_direction] = direction.downcase
41
+ end
42
+
43
+ opts.on('-t', '--sort-rating DIRECTION', sort_options, 'Sort reviews by rating. DIRECTION could be DESC or ASC') do |direction|
44
+ options[:sort_field] = 'rating'
45
+ options[:sort_direction] = direction.downcase
46
+ end
47
+
48
+ opts.on('-u', '--sort-usefulness DIRECTION', sort_options, 'Sort reviews by usefulness. DIRECTION could be DESC or ASC', 'Default is DESC') do |direction|
49
+ options[:sort_field] = 'usefulness'
50
+ options[:sort_direction] = direction.downcase
51
+ end
52
+
53
+ opts.on('-s', '--[no-]less', 'Set `less` for displaying reviews. By default it is true') do |less|
54
+ options[:system_less] = less
55
+ end
56
+ opts.on('-l', '--lang LANGUAGE', "Set language of reviews. Default is #{options[:locale]}") do |locale|
57
+ options[:locale] = locale
58
+ end
59
+
60
+ opts.on('-r', '--release RELEASE', "Set the release for package. Default is #{options[:release]}" ) do |release|
61
+ options[:release] = release
62
+ end
63
+
64
+ opts.on('-f', '--fields A,B,C', Array, 'Set the fields you want to display.', "Available fields: #{HEADERS.keys.join(', ')}") do |fields|
65
+ options[:fields] = fields.map(&:strip)
66
+ end
67
+
68
+ opts.on( '-h', '--help', 'Show this message' ) do
69
+ puts opts
70
+ exit
71
+ end
72
+
73
+ if ARGV.empty?
74
+ puts opts
75
+ exit
76
+ end
77
+ end.parse!
78
+
79
+ reviews = Previews::PackageReviews.new(options[:locale], options[:release], options[:package]).reviews
80
+ reviews.parse_date_time
81
+ reviews.sort_with!(options[:sort_field], options[:sort_direction]) if options[:sort_field]
82
+ reviews.replace_rating_with("★", "☆")
83
+ reviews.print_with(options[:fields], options[:system_less])
@@ -0,0 +1,11 @@
1
+ Feature: My bootstrapped app kinda works
2
+ In order to get going on coding my awesome app
3
+ I want to have aruba and cucumber setup
4
+ So I don't have to do it myself
5
+
6
+ Scenario: App just runs
7
+ When I get help for "previews"
8
+ Then the exit status should be 0
9
+ And the banner should be present
10
+ And the banner should document that this app takes no options
11
+ And the banner should document that this app takes no arguments
@@ -0,0 +1 @@
1
+ # Put your step defintions here
@@ -0,0 +1,9 @@
1
+ require 'aruba/cucumber'
2
+ require 'methadone/cucumber'
3
+
4
+ ENV['PATH'] = "#{File.expand_path(File.dirname(__FILE__) + '/../../bin')}#{File::PATH_SEPARATOR}#{ENV['PATH']}"
5
+
6
+ Before do
7
+ # Using "announce" causes massive warnings on 1.9.2
8
+ @puts = true
9
+ end
data/lib/previews.rb ADDED
@@ -0,0 +1,42 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'previews/version'
5
+ require 'previews/array'
6
+ require 'previews/hirb_vertical_table'
7
+
8
+ module Previews
9
+ class PackageReviews
10
+ def initialize(locale, release, package)
11
+ @url = "http://reviews.ubuntu.com/reviews/api/1.0/reviews/filter/#{locale}/ubuntu/#{release}/any/#{package}/"
12
+ @reviews = []
13
+ end
14
+
15
+ # Return Array of Hases
16
+ def reviews
17
+ download_reviews
18
+ @reviews.flatten!
19
+
20
+ @reviews
21
+ end
22
+
23
+ private
24
+ # Get all reviews
25
+ # Return Array of Hashes
26
+ def download_reviews
27
+ page = 1
28
+ loop do
29
+ @reviews << download_page(page)
30
+ break if @reviews.last.empty?
31
+ page += 1
32
+ end
33
+ end
34
+
35
+ # Get the one page of reviews.
36
+ # Return Array of Hashes
37
+ def download_page(page_number)
38
+ print '.'
39
+ JSON.parse(Net::HTTP.get(URI(@url + "page/#{page_number}/")))
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,41 @@
1
+ class Array
2
+ # Using Hirb to print reviews.
3
+ # :fields is fields to display
4
+ # print out :vertical table
5
+ # HEADERS is Hash of headers
6
+ # passing system_less as a option
7
+ def print_with(fields, system_less)
8
+ extend Hirb::Console
9
+ table self, :fields => fields,
10
+ :vertical => true,
11
+ :headers => HEADERS,
12
+ :system_less => system_less
13
+ end
14
+
15
+ # By default reviews sorted by usefulness. We can reverse it to sort by ascending
16
+ # For others fields we sorting by comparing each element of Array
17
+ def sort_with!(field, direction)
18
+ self if 'usefulness' == field && 'desc' == direction
19
+ self.reverse! if 'usefulness' == field && 'asc' == direction
20
+ if 'asc' == direction
21
+ self.sort! { |review1, review2| review1[field] <=> review2[field]}
22
+ elsif 'desc' == direction
23
+ self.sort! { |review1, review2| review2[field] <=> review1[field]}
24
+ end
25
+ end
26
+
27
+ # Parses all 'date_created' fields to DateTime object
28
+ def parse_date_time
29
+ self.each do |review|
30
+ review['date_created'] = DateTime.parse(review['date_created'])
31
+ end
32
+ end
33
+
34
+ # Replace each review's rating into some symbols
35
+ # Usage: array.replace_rating_with("★", "☆")
36
+ def replace_rating_with(up, empty)
37
+ self.each do |review|
38
+ review['rating'] = up * review['rating'] + empty * (5 - review['rating'])
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,30 @@
1
+ require 'hirb'
2
+
3
+ class Hirb::Helpers::VerticalTable < Hirb::Helpers::Table
4
+ # Method should return an Array
5
+ def render_rows
6
+ i = 0
7
+ rows = ''
8
+ longest_header = Hirb::String.size @headers.values.sort_by {|e| Hirb::String.size(e) }.last
9
+ delimiter = "-" * longest_header
10
+ @rows.map do |row|
11
+ row = "\n#{delimiter} #{i+1}. review #{delimiter}\n" +
12
+ @fields.map {|f|
13
+ if !@options[:hide_empty] || (@options[:hide_empty] && !row[f].empty?)
14
+ "#{Hirb::String.rjust(@headers[f], longest_header)}: #{row[f]}"
15
+ else
16
+ nil
17
+ end
18
+ }.compact.join("\n")
19
+ i+= 1
20
+ rows += row
21
+ end
22
+ rows = "#{@rows.size} review(s)" + rows
23
+ if @options[:system_less] && system("which less 2>&1 > /dev/null")
24
+ IO.popen('less', 'w') { |io| io.puts rows }
25
+ []
26
+ else
27
+ [rows]
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Previews
2
+ VERSION = "0.0.2"
3
+ end
data/previews.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "previews/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "previews"
7
+ s.version = Previews::VERSION
8
+ s.authors = ["Ivan K"]
9
+ s.email = ["divout@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Read user reviews of linux packages.}
12
+ s.description = %q{previews lets you read user reviews of linux packages from your console.}
13
+
14
+ s.rubyforge_project = "previews"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency('rdoc')
22
+ s.add_development_dependency('aruba')
23
+ s.add_development_dependency('rake','~> 0.9.2')
24
+ s.add_dependency('methadone')
25
+ s.add_dependency('hirb', '~> 0.6.0')
26
+ end
@@ -0,0 +1,7 @@
1
+ require 'test/unit'
2
+
3
+ class TestSomething < Test::Unit::TestCase
4
+ def test_truth
5
+ assert true
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: previews
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ivan K
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-18 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: &16007560 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *16007560
25
+ - !ruby/object:Gem::Dependency
26
+ name: aruba
27
+ requirement: &16007020 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *16007020
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &16005920 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.9.2
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *16005920
47
+ - !ruby/object:Gem::Dependency
48
+ name: methadone
49
+ requirement: &16005460 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *16005460
58
+ - !ruby/object:Gem::Dependency
59
+ name: hirb
60
+ requirement: &16004920 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 0.6.0
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *16004920
69
+ description: previews lets you read user reviews of linux packages from your console.
70
+ email:
71
+ - divout@gmail.com
72
+ executables:
73
+ - previews
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - MIT-LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - bin/previews
83
+ - features/previews.feature
84
+ - features/step_definitions/previews_steps.rb
85
+ - features/support/env.rb
86
+ - lib/previews.rb
87
+ - lib/previews/array.rb
88
+ - lib/previews/hirb_vertical_table.rb
89
+ - lib/previews/version.rb
90
+ - previews.gemspec
91
+ - test/tc_something.rb
92
+ homepage: ''
93
+ licenses: []
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project: previews
112
+ rubygems_version: 1.8.10
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Read user reviews of linux packages.
116
+ test_files: []