Skip to content

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/tlaws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
module Tlaws
# Your code goes here...
end

require_relative 'tlaws/numbers'
Copy link

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".

82 changes: 82 additions & 0 deletions lib/tlaws/numbers.rb
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 == :[]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ifs look bad in declarative API description. TLAW supports (or intended to support, at least, check it!) continuing of descriptions in several blocks, so it should be written like this:

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}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TLAW should support splat arguments (e.g. [42..56, 77, 285, 770..777] without second brackets), but currently it does not. Please add GitHub issue in TLAW repo, and implement it, or ask me to :)

}
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
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-keyword arguments don't seem like a good solution to me. random(10) intuitively reads like "random 0..10", not "random 10..Infinity". And the most useful call sequence ("give me random number less than some boundary") looks the most ugly: random(nil, 10)

end_path += '&min={min}&max={max}'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need it.

end

endpoint :math, '/math' + end_path do
Copy link

Choose a reason for hiding this comment

The 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
46 changes: 43 additions & 3 deletions spec/tlaws_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of it should go to spec/tlaws/numbers_spec.rb


types = %w!math year date trivia!

describe '.random' do

it "get an answer" do
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About testing strategy in total:

  1. I believe that at least on early stages it should be as short "sanity checks" as possible, our primary goal is to not get bored with it, make sure it works.
  2. Make it as readable and writable as possible, for easier update.
  3. Make it fast.

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
10 changes: 5 additions & 5 deletions tlaws.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Empty file added {
Empty file.