Skip to content

Commit

Permalink
Add API documentation (targetting rubydoc.info mostly) (#27)
Browse files Browse the repository at this point in the history
Currently writing an article on C extensions, I saw that we miss correct documentation
here (rubydoc.info/gems/fast-polylines). I've wrote some documentation, mostly
targetting the C extension to both provide an example for the article and improve our doc :)
  • Loading branch information
BuonOmo committed Jan 12, 2022
1 parent 297c5bb commit 32e0349
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*.gem
.rubocop-*
Gemfile.lock
/.yardoc
/yardoc
*.bundle
7 changes: 7 additions & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--markup markdown
--output-dir ./yardoc
lib/**/*.rb
ext/**/*.c
-
README.md
CHANGELOG.md
41 changes: 39 additions & 2 deletions ext/fast_polylines/fast_polylines.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/**
* Document-module: FastPolylines
*
* Implementation of the [Google polyline algorithm](https://code.google.com/apis/maps/documentation/utilities/polylinealgorithm.html).
*
* Install it with `gem install fast-polylines`, and then:
*
* require "fast_polylines"
*
* FastPolylines.encode([[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]])
* # "_p~iF~ps|U_ulLnnqC_mqNvxq`@"
*
* FastPolylines.decode("_p~iF~ps|U_ulLnnqC_mqNvxq`@")
* # [[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]]
*
* You can set an arbitrary precision for your coordinates to be encoded/decoded. It may be from 1
* to 13 decimal digits. However, 13 may be too much.
*
* [![https://xkcd.com/2170/](https://imgs.xkcd.com/comics/coordinate_precision.png)](https://www.explainxkcd.com/wiki/index.php/2170:_Coordinate_Precision)
*/

#include <ruby.h>

// An encoded number can have at most _precision_ characters. However,
Expand Down Expand Up @@ -46,7 +67,15 @@ static inline uint _get_precision(VALUE value) {
return (uint)precision;
}

static inline VALUE
/**
* call-seq:
* FastPolylines.decode(polyline, precision = 5) -> [[lat, lng], ...]
*
* Decode a polyline to a list of coordinates (lat, lng tuples). You may
* set an arbitrary coordinate precision, however, it **must match** the precision
* that was used for encoding.
*/
static VALUE
rb_FastPolylines__decode(int argc, VALUE *argv, VALUE self) {
rb_check_arity(argc, 1, 2);
Check_Type(argv[0], T_STRING);
Expand Down Expand Up @@ -103,7 +132,15 @@ _polyline_encode_number(char *chunks, int64_t number) {
return i;
}

static inline VALUE
/**
* call-seq:
* FastPolylines.encode([[lat, lng], ...], precision = 5) -> string
*
* Encode a list of coordinates to a polyline, you may give a specific precision
* if you want to retain more (or less) than 5 digits of precision. The maximum
* is 13, and may really be [too much](https://xkcd.com/2170/).
*/
static VALUE
rb_FastPolylines__encode(int argc, VALUE *argv, VALUE self) {
rb_check_arity(argc, 1, 2);
Check_Type(argv[0], T_ARRAY);
Expand Down
4 changes: 2 additions & 2 deletions fast-polylines.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Gem::Specification.new do |spec|
spec.license = "MIT"
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.6")

spec.files = Dir["{lib,ext}/**/*.{rb,c}"] + %w(README.md CHANGELOG.md)
spec.files = Dir["{lib,ext}/**/*.{rb,c}"] + %w(README.md CHANGELOG.md .yardopts)
spec.extensions = ["ext/fast_polylines/extconf.rb"]
spec.test_files =Dir["spec/**/*"] + %w(.rspec)
spec.test_files = Dir["spec/**/*"] + %w(.rspec)
spec.require_paths = "lib"

spec.add_development_dependency("benchmark-ips", "~> 2.7")
Expand Down
10 changes: 6 additions & 4 deletions lib/fast_polylines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
require "fast_polylines/fast_polylines"

module FastPolylines::Encoder
module_function def encode(points, precision = 1e5)
# @deprecated Use {FastPolylines.encode} instead.
def self.encode(points, precision = 1e5)
warn "Deprecated use of `FastPolylines::Encoder.encode`, " \
"use `FastPolylines.encode`."
"use `FastPolylines.encode`."
FastPolylines.encode(points, Math.log10(precision))
end
end

module FastPolylines::Decoder
module_function def decode(polyline, precision = 1e5)
# @deprecated Use {FastPolylines.decode} instead.
def self.decode(polyline, precision = 1e5)
warn "Deprecated use of `FastPolylines::Decoder.decode`, " \
"use `FastPolylines.decode`."
"use `FastPolylines.decode`."
FastPolylines.decode(polyline, Math.log10(precision))
end
end

0 comments on commit 32e0349

Please sign in to comment.