grepg 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e6f1d0a10e8d070997d8bb6766fdcc1e63225f28
4
+ data.tar.gz: c47a93c984efa2809b2f855d3d1647eb3dccbac7
5
+ SHA512:
6
+ metadata.gz: ccbe4d8835cd16ee0af1471793339a4d718cd67ceeef56a444e987557daac9133bf3971cc430aae771a92d648097062bb57e7e205d3a685f5fdbf6874f96dbd2
7
+ data.tar.gz: bfff67002c982ea130a1fb1b92a2e748c488f863e8f730427ad630e230d61dc5fc859b69f51773c05c4e7216766744b2e264046844af51add842ea018be1d6d8
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ pkg/
2
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://www.rubygems.org'
2
+
3
+ gem 'rake', '~> 10.1.0'
4
+ gem 'trollop', '~> 2.1.2'
5
+ gem 'rest-client', '~> 1.8.0'
6
+ gem 'colorize', '~> 0.7.7'
7
+
8
+ group :development do
9
+ gem 'rspec', :require => 'spec'
10
+ gem 'webmock', '~> 1.21.0'
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,54 @@
1
+ GEM
2
+ remote: https://www.rubygems.org/
3
+ specs:
4
+ addressable (2.4.0)
5
+ colorize (0.7.7)
6
+ crack (0.4.3)
7
+ safe_yaml (~> 1.0.0)
8
+ diff-lcs (1.2.5)
9
+ domain_name (0.5.20160310)
10
+ unf (>= 0.0.5, < 1.0.0)
11
+ http-cookie (1.0.2)
12
+ domain_name (~> 0.5)
13
+ mime-types (2.99)
14
+ netrc (0.11.0)
15
+ rake (10.1.1)
16
+ rest-client (1.8.0)
17
+ http-cookie (>= 1.0.2, < 2.0)
18
+ mime-types (>= 1.16, < 3.0)
19
+ netrc (~> 0.7)
20
+ rspec (3.4.0)
21
+ rspec-core (~> 3.4.0)
22
+ rspec-expectations (~> 3.4.0)
23
+ rspec-mocks (~> 3.4.0)
24
+ rspec-core (3.4.4)
25
+ rspec-support (~> 3.4.0)
26
+ rspec-expectations (3.4.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.4.0)
29
+ rspec-mocks (3.4.1)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.4.0)
32
+ rspec-support (3.4.1)
33
+ safe_yaml (1.0.4)
34
+ trollop (2.1.2)
35
+ unf (0.1.4)
36
+ unf_ext
37
+ unf_ext (0.0.7.2)
38
+ webmock (1.21.0)
39
+ addressable (>= 2.3.6)
40
+ crack (>= 0.3.2)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ colorize (~> 0.7.7)
47
+ rake (~> 10.1.0)
48
+ rest-client (~> 1.8.0)
49
+ rspec
50
+ trollop (~> 2.1.2)
51
+ webmock (~> 1.21.0)
52
+
53
+ BUNDLED WITH
54
+ 1.11.2
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ #grepg
2
+ ###The ruby client for greppage
3
+
4
+ `grepg` (pronounced Grep G) is a ruby client for [GrepPage](https://www.greppage.com). It is packaged as a gem.
5
+
6
+ ###Install
7
+ To install `grepg` run ```gem install grepg```
8
+
9
+ ###Examples
10
+ ```
11
+ Usage:
12
+ grepg user_name topic_name [-s search_term]
13
+ ```
14
+
15
+ Get all items/microdoc for a topic on any collection.
16
+
17
+ ```
18
+ $ grepg kdavis git
19
+ User: kdavis, Topic: git
20
+ push tags to remote / Github
21
+ git push --tags
22
+
23
+ remove delete tag
24
+ git tag -d v0.0.8
25
+
26
+ list branches that have not been merged
27
+ git branch --no-merged
28
+
29
+ list branches merged into master
30
+ git branch --merged master
31
+ ...
32
+
33
+ ```
34
+
35
+ Search for a specific string
36
+
37
+ ```
38
+ $ grepg kdavis git -s stash
39
+ User: kdavis, Topic: git, Search-Term: stash
40
+ TO apply your changes
41
+ git stash --apply
42
+
43
+ To list the stash
44
+ git stash list
45
+
46
+ Git stash
47
+ git stash
48
+ ```
49
+
50
+ ##Development
51
+ To run tests run ```bundle exec rake spec```. To install the gem locally, first build it using ```bundle exec rake build```. Then install the gem ```gem install pkg/grepg-0.0.1.gem```
52
+
53
+ ##License
54
+ grepg is under the [MIT License](http://www.opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ $:.unshift('lib')
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
8
+ t.pattern = 'spec/**/*_spec.rb'
9
+ end
10
+
11
+ task :default => :spec
data/bin/grepg ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'grepg'
3
+
4
+ GrepPage::Parser.new(ARGV).run!
data/grepg.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require_relative 'lib/grepg/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "grepg"
7
+ s.version = GrepPage::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Yash Ranadive"]
10
+ s.email = ["yash.ranadive@gmail.com"]
11
+ s.homepage = "https://www.greppage.com"
12
+ s.summary = %q{A ruby client to access greppage.com from the commandline}
13
+ s.description = %q{A ruby client to access greppage.com from the commandline}
14
+
15
+ s.add_development_dependency "rspec", "~>3.0.0"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- spec/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
22
+
data/lib/grepg/api.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'rest-client'
2
+
3
+ module GrepPage
4
+ module API
5
+ BASE_URL = 'https://www.greppage.com/api'
6
+ ACCESS_TOKEN = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImFsZyI6IkhTMjU2IiwidHlwIjoiSldUIn0.eyJpZCI6MjAwMDAwMDAwMCwiZW1haWwiOiJndWVzdEBndWVzdC5jb20iLCJuYW1lIjoiZ3Vlc3QiLCJleHAiOjE1MTExMzY4MzB9.gWohR7LLtROgjSl5SxbEaGRBveZQEv7Uj2rzmgYrbys'
7
+ def self.get(uri, access_token = ACCESS_TOKEN)
8
+ RestClient.get uri, {:accept => :json, :authorization => access_token}
9
+ end
10
+
11
+ def self.sheets(user_name)
12
+ response = get(sheets_uri(user_name))
13
+ raise RunTimeException if response.code != 200
14
+
15
+ JSON.parse(response.to_str, symbolize_names: true)
16
+ end
17
+
18
+ def self.cheats(user_name, sheet_id)
19
+ response = get(cheats_uri(user_name, sheet_id))
20
+ raise Exception if response.code != 200
21
+
22
+ JSON.parse(response.to_str, symbolize_names: true)
23
+ end
24
+
25
+ def self.sheets_uri(user_name)
26
+ [BASE_URL, 'users', user_name, 'sheets_with_stats'].join('/')
27
+ end
28
+
29
+ def self.cheats_uri(user_name, sheet_id)
30
+ [BASE_URL, 'users', user_name, 'sheets', sheet_id, 'cheats'].join('/')
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,13 @@
1
+ require 'colorize'
2
+ module GrepPage
3
+ class Formatter
4
+ # Displays an array of cheats
5
+ def self.cheat_rows(cheats, search_term)
6
+ cheats.map do |cheat|
7
+ puts cheat[:description].colorize(:green)
8
+ puts cheat[:command].colorize(:blue)
9
+ puts
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,82 @@
1
+ require 'trollop'
2
+ require 'rest-client'
3
+
4
+ # This class parses commandline arguments
5
+ module GrepPage
6
+ class Parser
7
+ def initialize(args)
8
+ parser = Trollop::Parser.new do
9
+ opt :search,
10
+ "text to search",
11
+ :type => :string,
12
+ :required => false,
13
+ :short => "-s"
14
+ banner <<-EOS
15
+
16
+ Usage:
17
+ grepg user_name topic_name [-s search_term]
18
+
19
+ Examples:
20
+ grepg kdavis css
21
+ greppg kdavis css -s color
22
+ EOS
23
+ end
24
+
25
+ @opts = Trollop::with_standard_exception_handling parser do
26
+ raise Trollop::HelpNeeded if args.size < 2 # show help screen
27
+ parser.parse args
28
+ end
29
+
30
+ leftovers = parser.leftovers
31
+ @user = leftovers.shift
32
+ @topic = leftovers.shift
33
+ @search_term = @opts[:search]
34
+ end
35
+
36
+ def get_all_topics(user)
37
+ GrepPage::API.sheets(user)
38
+ end
39
+
40
+ def filter_topics(topics, topic_name = '')
41
+ sheet = topics.select{|topic| topic[:name].downcase == topic_name.downcase}.first
42
+ sheet = topics.select{|topic| topic[:name].downcase[topic_name.downcase]}.first unless sheet
43
+ sheet
44
+ end
45
+
46
+ def get_cheats(user, sheet_id)
47
+ GrepPage::API.cheats(user, sheet_id)
48
+ end
49
+
50
+ def filter_cheats(cheats, search_term)
51
+ cheats.select do |cheat|
52
+ (cheat[:description].downcase[@search_term.downcase] ||
53
+ cheat[:command].downcase[@search_term.downcase]) != nil
54
+ end
55
+ end
56
+
57
+ def run!
58
+ headers = ["User: #{@user}", "Topic: #{@topic}"]
59
+ headers << "Search-Term: #{@search_term}" if @search_term
60
+ puts headers.join(", ")
61
+
62
+ begin
63
+ topics = get_all_topics(@user)
64
+ rescue RestClient::ResourceNotFound => ex
65
+ puts "That username does not exist"
66
+ exit 1
67
+ end
68
+
69
+ topic = filter_topics(topics, @topic)
70
+ if topic.nil? || topic.empty?
71
+ puts "Can't find that topic. Choose one of the following"
72
+ puts topics.map{|topic| topic[:name]}
73
+ exit 1
74
+ end
75
+
76
+ cheats = get_cheats(@user, topic[:id])
77
+ cheats = filter_cheats(cheats, @search_term) if @search_term
78
+
79
+ GrepPage::Formatter.cheat_rows(cheats, @search_term)
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,3 @@
1
+ module GrepPage
2
+ VERSION = "0.0.2"
3
+ end
data/lib/grepg.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'grepg/api'
2
+ require 'grepg/parser'
3
+ require 'grepg/formatter'
@@ -0,0 +1,37 @@
1
+ require 'json'
2
+ require_relative '../../lib/grepg.rb'
3
+
4
+ describe GrepPage::API do
5
+ let(:user_name) {TEST_USER}
6
+ let(:sheet_id) {TEST_SHEET_ID}
7
+
8
+ context "Sheet Fetching" do
9
+ let(:api) {GrepPage::API}
10
+ it "checks for a JSON array" do
11
+ sheets = api.sheets(user_name)
12
+ expect(sheets.class).to eq(Array)
13
+ end
14
+
15
+ it "checks for name and id field in each sheet" do
16
+ sheets = api.sheets(user_name)
17
+ expect(sheets.first[:name]).to be_truthy
18
+ expect(sheets.first[:id]).to be_truthy
19
+ end
20
+ end
21
+
22
+ context "Cheats Fetching" do
23
+ let(:api) {GrepPage::API}
24
+ it "checks for a JSON array" do
25
+ sheets = api.cheats(user_name, sheet_id)
26
+ expect(sheets.class).to eq(Array)
27
+ end
28
+
29
+ it "checks for name and id field in each cheat" do
30
+ sheets = api.cheats(user_name, sheet_id)
31
+ expect(sheets.first[:command]).to be_truthy
32
+ expect(sheets.first[:description]).to be_truthy
33
+ end
34
+ end
35
+ end
36
+
37
+
@@ -0,0 +1,22 @@
1
+ require 'json'
2
+ require_relative '../../lib/grepg.rb'
3
+
4
+ describe GrepPage::Parser do
5
+ describe '.initialize' do
6
+ context "expected behavior" do
7
+ it "returns a set of cheats when username and topic are given" do
8
+ parser = GrepPage::Parser.new(['kdavis', 'css'])
9
+ output = capture_stdout { parser.run! }
10
+ expect(output).to match(/colors/)
11
+ end
12
+
13
+ it "returns a set of cheats when username, topic and search term are given" do
14
+ parser = GrepPage::Parser.new(['kdavis', 'css', '-s', 'colors'])
15
+ output = capture_stdout { parser.run! }
16
+ expect(output).to match(/colors/)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+
@@ -0,0 +1,52 @@
1
+ require 'webmock/rspec'
2
+ # This should almost always remain true. We should test locally
3
+ TEST_LOCALLY = true
4
+ # The user whose cheats we will access
5
+ TEST_USER = 'kdavis'
6
+ # The sheet_id of the test user
7
+ TEST_SHEET_ID = '68'
8
+ # Disable outgoing network connections
9
+ TEST_LOCALLY ? WebMock.disable_net_connect! : WebMock::allow_net_connect!
10
+
11
+ # TODO: This is probably not a good place to put the stubbing logic
12
+ RSpec.configure do |config|
13
+ config.before(:each) do
14
+ if(TEST_LOCALLY)
15
+ configure_sheets_endpoint
16
+ configure_cheats_endpoint
17
+ end
18
+ end
19
+
20
+ def configure_sheets_endpoint
21
+ access_token = GrepPage::API::ACCESS_TOKEN
22
+
23
+ # Assuming that these are the responses returned by the API
24
+ sheets_body = %Q[
25
+ [{"id":68,"name":"CSS","user_id":18,"created_at":"2016-03-16T11:46:27-07:00","updated_at":"2016-03-16T11:46:27-07:00"},{"id":69,"name":"ES6","user_id":18,"created_at":"2016-03-16T11:49:30-07:00","updated_at":"2016-03-16T11:49:30-07:00"},{"id":70,"name":"UNIX","user_id":18,"created_at":"2016-03-16T11:55:25-07:00","updated_at":"2016-03-16T11:55:25-07:00"},{"id":71,"name":"GIT","user_id":18,"created_at":"2016-03-16T12:25:25-07:00","updated_at":"2016-03-16T12:25:25-07:00"}]
26
+ ].strip
27
+ stub_request(:get, "https://www.greppage.com/api/users/#{TEST_USER}/sheets_with_stats").
28
+ with(:headers => {"Authorization" => GrepPage::API::ACCESS_TOKEN}).
29
+ to_return(status: 200, body: sheets_body, headers: {})
30
+ end
31
+
32
+ def configure_cheats_endpoint
33
+ # Assuming that these are the responses returned by the API
34
+ cheats_body = %Q[
35
+ [{"id":718,"description":"color palette for web safe colors","command":"http://0xrgb.com/#flat","sheet_id":1,"created_at":"2015-12-19T12:55:19-08:00","updated_at":"2015-12-19T12:55:19-08:00"},{"id":728,"description":"Bootstrap text formatting styles","command":"http://www.w3schools.com/bootstrap/bootstrap_ref_css_text.asp","sheet_id":1,"created_at":"2015-12-29T21:16:34-08:00","updated_at":"2015-12-29T21:16:34-08:00"}]
36
+ ].strip
37
+ stub_request(:get, "https://www.greppage.com/api/users/#{TEST_USER}/sheets/#{TEST_SHEET_ID}/cheats").
38
+ with(:headers => {"Authorization" => GrepPage::API::ACCESS_TOKEN}).
39
+ to_return(status: 200, body: cheats_body, headers: {})
40
+ end
41
+ end
42
+
43
+ def capture_stdout(&block)
44
+ original_stdout = $stdout
45
+ $stdout = fake = StringIO.new
46
+ begin
47
+ yield
48
+ ensure
49
+ $stdout = original_stdout
50
+ end
51
+ fake.string
52
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grepg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Yash Ranadive
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ description: A ruby client to access greppage.com from the commandline
28
+ email:
29
+ - yash.ranadive@gmail.com
30
+ executables:
31
+ - grepg
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - README.md
39
+ - Rakefile
40
+ - bin/grepg
41
+ - grepg.gemspec
42
+ - lib/grepg.rb
43
+ - lib/grepg/api.rb
44
+ - lib/grepg/formatter.rb
45
+ - lib/grepg/parser.rb
46
+ - lib/grepg/version.rb
47
+ - spec/grepg/api_spec.rb
48
+ - spec/grepg/parser_spec.rb
49
+ - spec/spec_helper.rb
50
+ homepage: https://www.greppage.com
51
+ licenses: []
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.4.5.1
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: A ruby client to access greppage.com from the commandline
73
+ test_files:
74
+ - spec/grepg/api_spec.rb
75
+ - spec/grepg/parser_spec.rb
76
+ - spec/spec_helper.rb