Skip to content

Instantly share code, notes, and snippets.

View stevo's full-sized avatar

Błażej Kosmowski stevo

View GitHub Profile
@stevo
stevo / sync_hotels_spec.rb
Created May 23, 2018 12:41
The BDD story
# 1) All right... my task is to sync hotels with Cloudbeds. So probably this will be a rake task, but
# for simplicity I will just choose service as my outermost layer! Lets's dive in! I love BDD!
module Cloudbeds
describe SyncHotels do
# 2) it is a service, so to keep convention I'll test .call method
# Some small description of what I want it to do won't hurt as well.
# Scratch that! Description is essential!
describe '.call' do
it 'upserts hotels based on data returned from CloudBeds' do
# 7) Uh oh! I have realized that we will need to refresh a token once in a while.
@stevo
stevo / .rubocop.yml
Created May 21, 2018 06:58
Standardized rubocop config
# To make it easier to find descriptions and add exceptions
AllCops:
DisplayCopNames: true
Exclude:
- 'bin/**/*'
- 'vendor/**/*'
- 'db/schema.rb'
- 'tmp/**/*'
- 'node_modules/**/*'
@stevo
stevo / res_3.rb
Created May 8, 2018 09:32
An opinionated guide to readable RSpec (part 2 of 2)
module Support
module RequestHelpers
def get_json(path)
get path, headers: headers
end
def json_response
@json_response ||= HashWithIndifferentAccess.new(JSON.parse(response.body))
end
@stevo
stevo / res_2.rb
Created May 8, 2018 09:31
An opinionated guide to readable RSpec (part 2 of 2)
get_json '/v1/days'
expect(json_response[:data]).to include(
{ date: '2018-04-20', sunset_at: '19:47' },
{ date: '2018-04-21', sunset_at: '19:48' },
)
@stevo
stevo / res_1.rb
Created May 8, 2018 09:31
An opinionated guide to readable RSpec (part 2 of 2)
allow(ENV).to receive(:fetch).with('API_KEY') { '123' }
get '/v1/days', { 'Content-Type' => 'application/json', 'X-Api-Key' => api_key }
expect(JSON.parse(response.body)['data']).to include(
{ 'date' => '2018-04-20', 'sunset_at' => '19:47' },
{ 'date' => '2018-04-21', 'sunset_at' => '19:48' },
)
@stevo
stevo / format_2.rb
Created May 8, 2018 09:30
An opinionated guide to readable RSpec (part 2 of 2)
stub_request(:get, 'https://hotels.cloudbeds.com/api/v1.1/getRoomTypes') { {
body:
{
'success' => true,
'data' => [{
'roomTypeID' => '713', 'propertyID' => '276', 'roomTypeName' => 'AAA',
'roomTypeNameShort' => 'AAA', 'roomTypeDescription' => 'asdfa', 'isPrivate' => false,
'maxGuests' => '1', 'adultsIncluded' => '1', 'childrenIncluded' => 0,
'roomsAvailable' => 3, 'roomRate' => 120, 'roomTypeUnits' => 10
}],
@stevo
stevo / format_1.rb
Created May 8, 2018 09:29
An opinionated guide to readable RSpec (part 2 of 2)
stub_request(:get, 'https://hotels.cloudbeds.com/api/v1.1/getRoomTypes') { {
body:
{
'success' => true,
'data' => [
{
'roomTypeID' => '713',
'propertyID' => '276',
'roomTypeName' => 'AAA',
'roomTypeNameShort' => 'AAA',
@stevo
stevo / abstract.rb
Created May 8, 2018 09:28
An opinionated guide to readable RSpec (part 2 of 2)
describe '#nightly_price_for' do
context 'when given night is not booked' do
context 'when given night is weekend night' do
context 'when weekend nightly rate is available for period' do
it 'returns weekend nightly rate for period' do
rates_table = <<~RATE_PLAN
Rate period | Nightly | Weekend Night | Weekly | Monthly
---------------------------------------------------------------------
2017/07/20 - 2017/07/31 | 500 | 600 | 2800 | 30000
2017/08/01 - 2017/08/10 | 200 | 400 (Sat,Sun) | 1400 | 20000
@stevo
stevo / helper_2.rb
Created May 8, 2018 09:27
An opinionated guide to readable RSpec (part 2 of 2)
context 'when notifications service is disabled' do
it 'does not create project deletion notification for user' do
allow(notifier_client_double).to receive(:active?) { false }
user = create(:user)
project = create(:project, name: "Zebra", owner: user)
expect { DestroyProject.call(project) }.to_not change { Notification.count }
end
end
@stevo
stevo / helper_1.rb
Created May 8, 2018 09:26
An opinionated guide to readable RSpec (part 2 of 2)
context 'when notifications service is disabled' do
it 'does not create project deletion notification for user' do
api_user = create(:user, :api)
api_user_account = create(:account, user: api_user)
notifier_client = instance_double(NotifierClient)
allow(NotifierClient).to receive(:for).with(api_user_account) { notifier_client }
allow(notifier_client).to receive(:active?) { false }
user = create(:user)
project = create(:project, name: "Zebra", owner: user)