-
Notifications
You must be signed in to change notification settings - Fork 2
Numbers: random numbers facts #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
module Tlaws | ||
# Your code goes here... | ||
end | ||
|
||
require_relative 'tlaws/numbers' | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require 'tlaw' | ||
|
||
|
||
module Tlaws | ||
class Numbers < TLAW::API | ||
define do | ||
desc %Q{ | ||
Wrapper for [Numbers API](http://numbersapi.com). Facts about numbers. | ||
} | ||
|
||
docs 'http://numbersapi.com' | ||
|
||
base 'http://numbersapi.com' | ||
|
||
namespaces = { | ||
random: nil, | ||
:[] => '/{number}' | ||
} | ||
|
||
namespaces.each_pair do |ns, path| | ||
|
||
namespace ns, path do | ||
|
||
end_path = '?json' | ||
|
||
if ns == :[] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
namespace :[], '/numbers' do
desc ...
param ...
end
namespace :random do
desc ...
param ...
end
%i[[], random].each do |ns|
namespace ns do
...other code that is common for both of them
end
end |
||
desc %Q{ | ||
Facts about numbers. | ||
example: numbers_api[42].{type} | ||
} | ||
param :number, required: true, desc: %Q{ | ||
The number you need to know about | ||
Can be a: | ||
* singe number - numbers_api[42].{type} | ||
* range of numbers - numbers_api[42..56].{type} | ||
* combined array - numbers_api[[42..56, 77, 285, 770..777]].{type} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TLAW should support splat arguments (e.g. |
||
} | ||
else | ||
desc %Q{ | ||
Facts about random numbers. | ||
Can be called in such ways: | ||
* numbers_api.random.{type} | ||
* numbers_api.random(min).{type} | ||
where: min - lower bound for a random number | ||
* numbers_api.random(min, max).{type} | ||
where: min - lower bound for a random number | ||
where: max - upper bound for a random number | ||
} | ||
param :min, keyword: false, default: nil, desc: %Q{ | ||
Minimal number for random | ||
} | ||
param :max, keyword: false, default: nil, desc: %Q{ | ||
Maximum number for random | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-keyword arguments don't seem like a good solution to me. |
||
end_path += '&min={min}&max={max}' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need it. |
||
end | ||
|
||
endpoint :math, '/math' + end_path do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please post TLAW GitHub issue for default always present GET argument. |
||
desc %Q{ | ||
Get an interesting mathematical fact about the number. | ||
} | ||
end | ||
endpoint :year, '/year' + end_path do | ||
desc %Q{ | ||
Get an interesting fact about the year. | ||
} | ||
end | ||
endpoint :date, '/date' + end_path do | ||
desc %Q{ | ||
Get an interesting fact about the date number. | ||
} | ||
end | ||
endpoint :trivia, '/trivia' + end_path do | ||
desc %Q{ | ||
Get an interesting fact about the number. | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,47 @@ | |
expect(Tlaws::VERSION).not_to be nil | ||
end | ||
|
||
it "does something useful" do | ||
expect(false).to eq(true) | ||
end | ||
let(:numbersapi){ Tlaws::Numbers.new } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of it should go to |
||
|
||
types = %w!math year date trivia! | ||
|
||
describe '.random' do | ||
|
||
it "get an answer" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something bad goes on with indentation here :) I strongly recommend add rubocop to the repo on the earliest stages ;) |
||
res = numbersapi.random.math | ||
expect(res).to be_instance_of Hash | ||
end | ||
|
||
types.each do |type| | ||
describe ".#{type}" do | ||
it "get a '#{type}' response on '#{type}' request" do | ||
res = numbersapi.random.send(type) | ||
expect(res["type"]).to eq type | ||
end | ||
end | ||
describe ".#{type}(min, max)" do | ||
it "get a random '#{type}' number in a range" do | ||
min = 10 | ||
max = 20 | ||
res = numbersapi.random(min, max).send(type) | ||
expect(res["number"]).to be_between(min, max) | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe '[number]' do | ||
it "get an answer" do | ||
res = numbersapi[42].math | ||
expect(res).to be_instance_of Hash | ||
end | ||
types.each do |type| | ||
describe ".#{type}" do | ||
it "get a '#{type}' response on '#{type}' request" do | ||
expect(numbersapi[15].send(type)["type"]).to eq type | ||
end | ||
end | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. About testing strategy in total:
So, the following seems to be a better strategy for this case (though not usual for RSpec in general): describe Tlaws::Numbers do
let(:api) { described_class.new }
it 'works' do
expect(api[20].year).to include(something something)
expect(api[20].day).to include(something something)
expect(api.random(max: 3)).to ...
expect(api.random(min: 200)).to ...
end
end WDYT? ...and add VCR, of course ;) |
||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,12 @@ require "tlaws/version" | |
Gem::Specification.new do |spec| | ||
spec.name = "tlaws" | ||
spec.version = Tlaws::VERSION | ||
spec.authors = ["TODO: Write your name"] | ||
spec.email = ["TODO: Write your email address"] | ||
spec.authors = ["Yuri Valigursky"] | ||
spec.email = ["[email protected]"] | ||
|
||
spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.} | ||
spec.description = %q{TODO: Write a longer description or delete this line.} | ||
spec.homepage = "TODO: Put your gem's website or public repo URL here." | ||
spec.summary = %q{A repository for TLAW-based thin API wrappers, for all the world} | ||
spec.description = %q{A repository for TLAW-based thin API wrappers, for all the world} | ||
spec.homepage = "https://github.com/molybdenum-99/tlaws" | ||
spec.license = "MIT" | ||
|
||
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should require them all here. Imagine some of the wrappers depend on other libraries (like nokogiri, or some geo-services, or even SQL databases) — we don't want all of it to be required at once. Typical use case is "I install entire Tlaws (say, 30 wrappers), but use only wrappers X and Y, so I require only them".