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

Numbers: random numbers facts #1

wants to merge 3 commits into from

Conversation

yuri-val
Copy link
Contributor

@yuri-val yuri-val commented Dec 1, 2017

Prototype numbersapi

Copy link

@zverok zverok left a comment

Choose a reason for hiding this comment

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

Let's use this simple case to decide on two important things:

  1. How should they be tested? At least some "sanity tests" are necessary (e.g. that it really successfully calls API not fails into 404 or something)
  2. How should they be documented? So Tlaws user can look somewhere for quick-reference for the API wrapper.

Facts about numbers.
}

param :numb, required: true
Copy link

Choose a reason for hiding this comment

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

Let's keep it number, as in original docs. Two chars is not that much economy :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix it

desc %Q{
Get an interesting fact about the random number.
}
end
Copy link

Choose a reason for hiding this comment

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

I believe the same endpoints repeats for one number ([]), and for the random number? Then let's make it DRY somehow (var.1: just do something like %i[random, []].each do |namespace| ..., var.2: try to introduce some solution in TLAW itself, that will allow to describe "shared endpoints" (like "shared examples" in RSpec), and then just include_endpoints "number endpoints" in DSL?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, got it

@yuri-val
Copy link
Contributor Author

yuri-val commented Dec 6, 2017

@zverok, made some changes and added tests. Please review it.

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


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

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 :)

}
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)

param :max, keyword: false, default: nil, desc: %Q{
Maximum number for random
}
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_path += '&min={min}&max={max}'
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.

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


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 ;)

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 ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants