Skip to content

Commit a4c7470

Browse files
joelvhzverok
authored andcommitted
DSL refactor and minor Ruby improvements and fixes (#8)
* Moved DSL classes to separate files * Backport `yield_self` and replace `derp` * Moved param-related classes into `TLAW::Params` namespace * Move `TLAW::Params::Param#make` to `TLAW::Params#make` `Param` shouldn’t have to know about subclasses * Update gem info * Require Ruby 2.3.0+ * Rename `Params::Param` to `Params::Base` * Require `backports` gem * Changed style back to `fetch -> validate -> create` * Invert logic * Bring back "GET-only APIs" wording * Moved DSL comments to module * Fix Rubocop issues * Fix path to TLAW gem for examples * Fix example * Fix nil check * Revert docs * Fixed Rubocop warnings * Fix Rubocop errors for latest Rubocop version * Expand path to run examples from parent directory * Removed duplicate docs from cherry-pick * Replaced TODO with some dummy docs
1 parent 79f3f16 commit a4c7470

33 files changed

+749
-690
lines changed

.rubocop.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ inherit_from: .rubocop_todo.yml
22
require: rubocop-rspec
33

44
AllCops:
5+
TargetRubyVersion: 2.3.0
56
Include:
67
- 'lib/**/*'
78
Exclude:
@@ -13,6 +14,10 @@ AllCops:
1314
- 'Gemfile'
1415
DisplayCopNames: true
1516

17+
# Ruby 2.3+
18+
FrozenStringLiteralComment:
19+
Enabled: false
20+
1621
# My personal style
1722
# -----------------
1823

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.3.0

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
cache: bundler
22
language: ruby
33
rvm:
4-
- "2.1"
5-
- "2.2"
64
- "2.3"
75
- "2.4"
6+
- "2.5"
87
- jruby-9.1.13.0
98
install:
109
- bundle install --retry=3

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ gemspec
44

55
group :test do
66
gem 'coveralls', require: false
7-
gem 'saharspec', '= 0.0.4'
7+
gem 'saharspec', '0.0.4'
88
end

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
[![Build Status](https://travis-ci.org/molybdenum-99/tlaw.svg?branch=master)](https://travis-ci.org/molybdenum-99/tlaw)
55
[![Coverage Status](https://coveralls.io/repos/molybdenum-99/tlaw/badge.svg?branch=master)](https://coveralls.io/r/molybdenum-99/tlaw?branch=master)
66

7-
**TLAW** (pronounce it like "tea+love"... or whatever) is the last (and
8-
only) API wrapper framework for _get-only APIes_<sup>[*](#get-only-api)</sup>
9-
(think weather, search, economical indicators, geonames and so on).
7+
**TLAW** (pronounce it like "tea+love"... or whatever) is the last (and only)
8+
API wrapper framework you'll ever need for accessing _GET-only APIs_<sup>[*](#get-only-api)</sup>
9+
in a consistent way (think weather, search, economical indicators, geonames and so on).
1010

1111
## Table Of Contents
1212

@@ -302,7 +302,7 @@ post_process_items('foo') {
302302

303303
# More realistic examples:
304304
post_process('meta.count', &:to_i)
305-
post_process('daily') {
305+
post_process_items('daily') {
306306
post_process('date', &Date.method(:parse))
307307
}
308308
post_process('auxiliary_value') { nil } # Nil's will be thrown away completely
@@ -314,9 +314,9 @@ See full post-processing features descriptions in
314314
#### All at once
315315

316316
All described response processing steps are performed in this order:
317+
317318
* parsing and initial flattening of JSON (or XML) hash;
318-
* applying post-processors (and flatten the response after _each_ of
319-
them);
319+
* applying post-processors (and flatten the response after _each_ of them);
320320
* make `DataTable`s from arrays of hashes.
321321

322322
### Documentability

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require 'rubygems/tasks'
33
Gem::Tasks.new
44

55
namespace :doc do
6-
desc "Prints TOC for README.md (doesn't inserts it automatically!)"
6+
desc "Prints TOC for README.md (doesn't insert it automatically!)"
77
task :toc do
88
# NB: really dumb. Yet better than any Solution I can find :(
99
# Grabbing it from project to project.

examples/demo_base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
require 'pp'
1+
$:.unshift File.expand_path('../../lib', __FILE__)
22

3-
$:.unshift 'lib'
43
require 'tlaw'
4+
require 'pp'
55

66
begin
77
require 'dotenv'

lib/tlaw.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
1+
require 'backports/2.5.0/kernel/yield_self'
12
require 'open-uri'
23
require 'json'
34
require 'addressable/uri'
45
require 'addressable/template'
56

6-
# Let no one know! But they in Ruby committee just too long to add
7-
# something like this to the language.
8-
#
9-
# See also https://bugs.ruby-lang.org/issues/12760
10-
#
11-
# @private
12-
class Object
13-
def derp
14-
yield self
15-
end
16-
end
17-
187
# TLAW is a framework for creating API wrappers for get-only APIs (like
198
# weather, geonames and so on) or subsets of APIs (like getting data from
209
# Twitter).
@@ -55,8 +44,7 @@ module TLAW
5544
require_relative 'tlaw/util'
5645
require_relative 'tlaw/data_table'
5746

58-
require_relative 'tlaw/param'
59-
require_relative 'tlaw/param_set'
47+
require_relative 'tlaw/params'
6048

6149
require_relative 'tlaw/api_path'
6250
require_relative 'tlaw/endpoint'

lib/tlaw/api_path.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require_relative 'params/set'
12
require 'forwardable'
23

34
module TLAW
@@ -54,7 +55,7 @@ def inherit(namespace, **attrs)
5455

5556
# @private
5657
def params_from_path!
57-
Addressable::Template.new(path).keys.each do |key| # rubocop:disable Performance/HashEachMethods
58+
Addressable::Template.new(path).keys.each do |key|
5859
param_set.add key.to_sym, keyword: false
5960
end
6061
end
@@ -67,13 +68,13 @@ def setup_parents(parent)
6768

6869
# @private
6970
def symbol=(sym)
70-
@symbol = sym
7171
@path ||= "/#{sym}"
72+
@symbol = sym
7273
end
7374

74-
# @return [ParamSet]
75+
# @return [Params::Set]
7576
def param_set
76-
@param_set ||= ParamSet.new
77+
@param_set ||= Params::Set.new
7778
end
7879

7980
# @private

lib/tlaw/data_table.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ def inspect
109109
end
110110

111111
# @private
112-
def pretty_print(pp)
113-
pp.text("#<#{self.class.name}[#{keys.join(', ')}] x #{size}>")
112+
def pretty_print(printer)
113+
printer.text("#<#{self.class.name}[#{keys.join(', ')}] x #{size}>")
114114
end
115115
end
116116
end

0 commit comments

Comments
 (0)