Skip to content

Commit e3c8d8b

Browse files
committed
adding documentation
1 parent abdf345 commit e3c8d8b

17 files changed

+1748
-69
lines changed

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ gemspec
66

77
group :test, :development do
88
gem 'pry-byebug', '~> 3.10', '>= 3.10.1'
9+
gem 'rake', '~> 13.0', '>= 13.0.6'
910
gem 'rubocop', '~> 1.45', '>= 1.45.1'
1011
gem 'rubocop-rake', '~> 0.6.0'
12+
gem 'ruby-progressbar', '~> 1.8', '>= 1.8.1'
1113
end

Gemfile.lock

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ GEM
2828
byebug (~> 11.0)
2929
pry (>= 0.13, < 0.15)
3030
rainbow (3.1.1)
31+
rake (13.0.6)
3132
regexp_parser (2.7.0)
3233
rexml (3.2.5)
3334
rubocop (1.45.1)
@@ -53,8 +54,10 @@ PLATFORMS
5354
DEPENDENCIES
5455
lnd-client!
5556
pry-byebug (~> 3.10, >= 3.10.1)
57+
rake (~> 13.0, >= 13.0.6)
5658
rubocop (~> 1.45, >= 1.45.1)
5759
rubocop-rake (~> 0.6.0)
60+
ruby-progressbar (~> 1.8, >= 1.8.1)
5861

5962
BUNDLED WITH
6063
2.4.4

README.md

+13-64
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
This is a low-level client library. For a better experience, you may want to check out the [Lighstorm](https://github.com/icebaker/lighstorm) abstraction.
66

77
- [Usage](#usage)
8-
- [Channel Arguments](#channel-arguments)
98
- [Documentation](#documentation)
109
- [Development](#development)
1110
- [Upgrading gRPC Proto Files](#upgrading-grpc-proto-files)
11+
- [Generating Documentation](#generating-documentation)
1212
- [Publish to RubyGems](#publish-to-rubygems)
1313

1414
## Usage
@@ -53,71 +53,9 @@ client.router.subscribe_htlc_events do |data|
5353
end
5454
```
5555

56-
### Channel Arguments
57-
58-
```ruby
59-
require 'lnd-client'
60-
61-
puts LNDClient.version # => 0.0.5
62-
63-
client = LNDClient.new(
64-
certificate_path: '/lnd/tls.cert',
65-
macaroon_path: '/lnd/data/chain/bitcoin/mainnet/admin.macaroon',
66-
socket_address: '127.0.0.1:10009'
67-
)
68-
69-
client.lightning(
70-
channel_args: { 'grpc.max_receive_message_length' => 1024 * 1024 * 50 }
71-
)
72-
73-
graph = client.lightning.describe_graph
74-
75-
graph.nodes # => [...]
76-
graph.edges # => [...]
77-
```
78-
7956
### Documentation
8057

81-
```ruby
82-
require 'lnd-client'
83-
84-
puts LNDClient.version # => 0.0.5
85-
86-
client = LNDClient.new(
87-
certificate_path: '/lnd/tls.cert',
88-
macaroon_path: '/lnd/data/chain/bitcoin/mainnet/admin.macaroon',
89-
socket_address: '127.0.0.1:10009'
90-
)
91-
92-
client.doc.services # => ['lightning', 'router']
93-
94-
client.lightning.doc.available_methods # =>
95-
# ['abandon_channel',
96-
# 'add_invoice',
97-
# 'bake_macaroon',
98-
# 'batch_open_channel',
99-
# 'channel_acceptor',
100-
# 'channel_balance',
101-
# 'check_macaroon_permissions',
102-
# 'close_channel',
103-
# 'closed_channels',
104-
# # ...
105-
# 'get_node_info'
106-
# ]
107-
108-
client.lightning.doc.describe(:get_node_info) # =>
109-
# { method: 'get_node_info',
110-
# input: { pub_key: '', include_channels: false},
111-
# output: { node: nil, num_channels: 0, total_capacity: 0, channels: []}}
112-
113-
client.lightning.doc.grpc(:get_node_info)
114-
# #<struct GRPC::RpcDesc
115-
# name=:GetNodeInfo,
116-
# input=Lnrpc::NodeInfoRequest,
117-
# output=Lnrpc::NodeInfo,
118-
# marshal_method=:encode,
119-
# unmarshal_method=:decode>
120-
```
58+
Check the [full documentation](#documentation).
12159

12260
## Development
12361

@@ -141,6 +79,17 @@ rubocop -A
14179
```sh
14280
bundle exec rake grpc:upgrade
14381
```
82+
83+
### Generating Documentation
84+
85+
```sh
86+
bundle exec rake grpc:docs
87+
88+
npm i docsify-cli -g
89+
90+
docsify serve ./docs
91+
```
92+
14493
### Publish to RubyGems
14594

14695
```sh

Rakefile

+7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
# frozen_string_literal: true
22

3+
require_relative './controllers/documentation'
34
require_relative './controllers/grpc_generator'
45

56
namespace :grpc do
7+
desc 'Generate gRPC Documentation'
8+
task :docs do
9+
require 'ruby-progressbar'
10+
LNDClientInternal::DocumentationController.generate!
11+
end
12+
613
desc 'Upgrade lnd Protocol Buffers for gGRPC'
714
task :upgrade do
815
LNDClientInternal::GrpcGeneratorController.upgrade!

controllers/documentation.rb

+82-3
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,100 @@
11
# frozen_string_literal: true
22

3+
require 'securerandom'
4+
35
require_relative '../logic/string'
6+
require_relative '../components/grpc'
47

58
module LNDClientInternal
69
class DocumentationController
10+
PROGRESS = true
11+
PATH = 'docs/README.md'
12+
KEY = '<!-- [INJECT:GRP:DOCS] -->'
13+
714
attr_reader :available_methods
815

9-
def initialize(service)
10-
@service = service
16+
def self.format(code)
17+
id = SecureRandom.hex(32)
18+
path = "temp/#{id}.rb"
19+
File.write("temp/#{id}.rb", code.gsub(', ', ",\n"))
20+
`rubocop -A -c docs/.rubocop.yml #{path}`
21+
output = File.read("temp/#{id}.rb")
22+
File.delete("temp/#{id}.rb")
23+
output.strip
24+
end
25+
26+
def self.generate!
27+
services = LNDClientInternal::GRPC::SERVICES.keys.map(&:to_sym)
28+
29+
if PROGRESS
30+
total = services.sum do |service|
31+
new(
32+
LNDClientInternal::GRPC::SERVICES[service].const_get(:Service)
33+
).available_methods.size * 2
34+
end
35+
progressbar = ProgressBar.create(total: total, format: '%a %e |%b>>%i| %P% | %c of %C')
36+
end
37+
38+
content = ''
39+
40+
services.each do |service|
41+
content += "## #{service}\n"
42+
doc = new(LNDClientInternal::GRPC::SERVICES[service].const_get(:Service))
43+
44+
doc.available_methods.each do |method_name|
45+
content += "\n### #{method_name}\n"
46+
47+
url = "https://lightning.engineering/api-docs/api/lnd/#{service}/#{method_name.gsub('_', '-')}/index.html"
48+
49+
content += "\n[lightning.engineering/#{service}/#{method_name.gsub('_', '-')}](#{url})\n"
50+
51+
description = doc.describe(method_name)
52+
content += "\n```ruby\n"
53+
54+
code = "client.#{service}.#{method_name}"
55+
56+
code += "(\n#{description[:input].inspect}\n)" if !description[:input].nil? && !description[:input].empty?
57+
58+
code += " do |data|\n puts data.inspect # => { ... }\nend" if method_name =~ /^subscribe/
59+
60+
content += if !description[:input].nil? && !description[:input].empty?
61+
"#{self.format(code)}\n```\n"
62+
else
63+
"#{code}\n```\n"
64+
end
65+
66+
progressbar.increment if PROGRESS
67+
68+
if !description[:output].nil? && !description[:output].empty?
69+
code = self.format(description[:output].inspect)
70+
content += "\nOutput:\n```ruby\n#{code}\n```\n"
71+
end
72+
73+
progressbar.increment if PROGRESS
74+
end
75+
end
76+
77+
document = File.read(PATH)
78+
79+
parts = document.split(KEY)
80+
81+
File.write(PATH, "#{parts[0]}#{KEY}\n\n#{content}\n#{KEY}#{parts[2]}")
82+
83+
progressbar.finish
84+
85+
puts "\nDocumentation updated: #{PATH}\n"
86+
end
87+
88+
def initialize(grpc_service)
89+
@grpc_service = grpc_service
1190
@descriptions = {}
1291
@grpc = {}
1392

1493
build!
1594
end
1695

1796
def build!
18-
@available_methods = @service.service.rpc_descs.values.map do |desc|
97+
@available_methods = @grpc_service.rpc_descs.values.map do |desc|
1998
method_name = LNDClientInternal::StringLogic.underscore(desc.name.to_s)
2099

21100
build_description!(method_name, desc)

controllers/service.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def initialize(client, rpc, params = {})
1515
client.config.credentials,
1616
**params
1717
)
18-
@doc = LNDClientInternal::DocumentationController.new(self)
18+
@doc = LNDClientInternal::DocumentationController.new(@service)
1919
end
2020

2121
def call!(method_key, desc, *args, &block)

docs/.nojekyll

Whitespace-only changes.

docs/.rubocop.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
AllCops:
2+
TargetRubyVersion: 3.0.0
3+
NewCops: enable
4+
5+
Style/FrozenStringLiteralComment:
6+
Enabled: false
7+
8+
Layout/LineLength:
9+
Max: 60

0 commit comments

Comments
 (0)