ramnagar 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/Gemfile +6 -0
- data/README.md +46 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/ramnagar +5 -0
- data/lib/ramnagar.rb +12 -0
- data/lib/ramnagar/cli.rb +46 -0
- data/lib/ramnagar/client.rb +71 -0
- data/lib/ramnagar/converter.rb +22 -0
- data/lib/ramnagar/version.rb +3 -0
- data/ramnagar.gemspec +26 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0ecc795eefa8811d0d1e98e0517dbbceb405a03701df41ae423e41b13e67a32a
|
4
|
+
data.tar.gz: dc6cf0e9899b3dd52dac6dc1f408a88f318e40dec8ee6caf5e89b3b13e51a837
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f68fa57e698d5fac4f6390d7601126fea1c51ebde7861cbeb0bf7a05af7b4a84b278c8a81b2fe9857cacf3d05785c2d1d3aa44d78afed2a7efc2cd20dec95828
|
7
|
+
data.tar.gz: a9faf2c7f889acfa0f92071dff9adde9917c5e560edeb733dbc589af71abef8a97707bba1097415b23e287f5bb65fb668ce4de529a0d288bba22839585651423
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Ramnagar
|
2
|
+
|
3
|
+
A converter from search query to GitHub's Notification query.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ramnagar'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ramnagar
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
It supports only `user:USER_NAME` query. It converts `user:USER_NAME` to `repo:REPO1 repo:REPO2 ...`.
|
24
|
+
|
25
|
+
```bash
|
26
|
+
$ GITHUB_ACCESS_TOKEN=XXXXX ramnagar user:rubocop-hq
|
27
|
+
repo:rubocop-hq/rubocop-rubycw repo:rubocop-hq/rubocop-rake repo:rubocop-hq/rubocop-extension-generator ....
|
28
|
+
```
|
29
|
+
|
30
|
+
Note that it slices query if the query is over than 1024 characters because of GitHub's limitation.
|
31
|
+
|
32
|
+
## Development
|
33
|
+
|
34
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
35
|
+
|
36
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/pocke/ramnagar.
|
41
|
+
|
42
|
+
|
43
|
+
## Naming
|
44
|
+
|
45
|
+
I found the name with Wikipedia's random article feature.
|
46
|
+
https://en.wikipedia.org/wiki/Ramnagar,_Nawalparasi
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ramnagar"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/ramnagar
ADDED
data/lib/ramnagar.rb
ADDED
data/lib/ramnagar/cli.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module Ramnagar
|
2
|
+
class CLI
|
3
|
+
# GitHub's limitation.
|
4
|
+
# GitHub truncates query that is over 1024 characters.
|
5
|
+
MAX_QUERY_LENGTH = 1024
|
6
|
+
|
7
|
+
def initialize(argv)
|
8
|
+
@argv = argv
|
9
|
+
end
|
10
|
+
|
11
|
+
def run
|
12
|
+
github_access_token = ENV['GITHUB_ACCESS_TOKEN']
|
13
|
+
|
14
|
+
result = []
|
15
|
+
@argv.each do |q|
|
16
|
+
result.concat(Converter.convert(q, github_access_token: github_access_token))
|
17
|
+
end
|
18
|
+
|
19
|
+
slice_queries(result).each do |slice|
|
20
|
+
puts slice
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private def slice_queries(queries)
|
25
|
+
buf = +""
|
26
|
+
res = []
|
27
|
+
queries = queries.dup
|
28
|
+
|
29
|
+
while q = queries.pop
|
30
|
+
if (buf + q).size + 1 > MAX_QUERY_LENGTH
|
31
|
+
res << buf
|
32
|
+
buf = +""
|
33
|
+
else
|
34
|
+
if buf.empty?
|
35
|
+
buf << q
|
36
|
+
else
|
37
|
+
buf << ' ' << q
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
res << buf unless buf.empty?
|
42
|
+
|
43
|
+
res
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module Ramnagar
|
2
|
+
class Client
|
3
|
+
class RequestError < StandardError
|
4
|
+
def initialize(errors)
|
5
|
+
@errors = errors
|
6
|
+
end
|
7
|
+
|
8
|
+
def message
|
9
|
+
@errors.map{|e| e[:message]}.join("\n")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
USER_REPOSITORIES_QUERY = <<~GRAPHQL
|
14
|
+
query($login: String!, $first: Int!, $after: String) {
|
15
|
+
repositoryOwner(login: $login) {
|
16
|
+
repositories(first: $first, after: $after) {
|
17
|
+
nodes {
|
18
|
+
nameWithOwner
|
19
|
+
viewerSubscription
|
20
|
+
}
|
21
|
+
pageInfo {
|
22
|
+
endCursor
|
23
|
+
hasNextPage
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
}
|
28
|
+
GRAPHQL
|
29
|
+
|
30
|
+
def initialize(github_access_token:)
|
31
|
+
@github_access_token = github_access_token
|
32
|
+
end
|
33
|
+
|
34
|
+
def repositories(user:)
|
35
|
+
paginate do |after|
|
36
|
+
resp = req(query: USER_REPOSITORIES_QUERY, variables: { login: user, first: 100, after: after })
|
37
|
+
resp[:data][:repositoryOwner][:repositories]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private def req(query:, variables: {})
|
42
|
+
http = Net::HTTP.new('api.github.com', 443)
|
43
|
+
http.use_ssl = true
|
44
|
+
header = {
|
45
|
+
"Authorization" => "Bearer #{github_access_token}",
|
46
|
+
'Content-Type' => 'application/json',
|
47
|
+
}
|
48
|
+
resp = http.request_post('/graphql', JSON.generate({ query: query, variables: variables }), header)
|
49
|
+
JSON.parse(resp.body, symbolize_names: true).tap do |content|
|
50
|
+
raise RequestError.new(content[:errors]) if content[:errors]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private def paginate(after = nil, &block)
|
55
|
+
has_next_page = true
|
56
|
+
nodes = []
|
57
|
+
|
58
|
+
while has_next_page
|
59
|
+
connection = block.call(after)
|
60
|
+
nodes.concat connection[:nodes]
|
61
|
+
has_next_page = connection[:pageInfo][:hasNextPage]
|
62
|
+
after = connection[:pageInfo][:endCursor]
|
63
|
+
end
|
64
|
+
|
65
|
+
nodes
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
attr_reader :github_access_token
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Ramnagar
|
2
|
+
module Converter
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def convert(query, github_access_token:)
|
6
|
+
case query
|
7
|
+
when /^user:/
|
8
|
+
convert_user_query(query, github_access_token: github_access_token)
|
9
|
+
else
|
10
|
+
raise "unexpected: #{query}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private def convert_user_query(query, github_access_token:)
|
15
|
+
user = query[/^user:(.+)/, 1]
|
16
|
+
client = Client.new(github_access_token: github_access_token)
|
17
|
+
client.repositories(user: user)
|
18
|
+
.select { |repo| repo[:viewerSubscription] == 'SUBSCRIBED' }
|
19
|
+
.map { |repo| "repo:" + repo[:nameWithOwner] }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/ramnagar.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative 'lib/ramnagar/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "ramnagar"
|
5
|
+
spec.version = Ramnagar::VERSION
|
6
|
+
spec.authors = ["Masataka Pocke Kuwabara"]
|
7
|
+
spec.email = ["kuwabara@pocke.me"]
|
8
|
+
|
9
|
+
spec.summary = %q{}
|
10
|
+
spec.description = %q{}
|
11
|
+
spec.homepage = "https://github.com/pocke/ramnagar"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
16
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ramnagar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Masataka Pocke Kuwabara
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-19 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ''
|
14
|
+
email:
|
15
|
+
- kuwabara@pocke.me
|
16
|
+
executables:
|
17
|
+
- ramnagar
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".gitignore"
|
22
|
+
- Gemfile
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- bin/console
|
26
|
+
- bin/setup
|
27
|
+
- exe/ramnagar
|
28
|
+
- lib/ramnagar.rb
|
29
|
+
- lib/ramnagar/cli.rb
|
30
|
+
- lib/ramnagar/client.rb
|
31
|
+
- lib/ramnagar/converter.rb
|
32
|
+
- lib/ramnagar/version.rb
|
33
|
+
- ramnagar.gemspec
|
34
|
+
homepage: https://github.com/pocke/ramnagar
|
35
|
+
licenses: []
|
36
|
+
metadata:
|
37
|
+
homepage_uri: https://github.com/pocke/ramnagar
|
38
|
+
source_code_uri: https://github.com/pocke/ramnagar
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.3.0
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubygems_version: 3.2.0.pre1
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: ''
|
58
|
+
test_files: []
|