Skip to content

Instantly share code, notes, and snippets.

@finsterthecat
finsterthecat / moustache.py
Last active December 23, 2020 18:37
Replace all template variables in input file with configured values from the JSON config
import json
import re
import sys
#
# Replace all template variables in input file with configured values
# from the JSON config. Template variables are distiguished by enclosing
# double braces. eg {{ template.variable }}. This would match templatevalue
# in JSON config {"template": {"variable": "templatevalue"}}
#
@finsterthecat
finsterthecat / google_interview.rb
Last active February 9, 2021 06:23
Solves the problem that is the basis of the example Google Interview here: https://www.youtube.com/watch?v=XKu_SEDAykw
#
# Google interview question: find all combinations that sum up to a value
#
require 'set'
def summables(vector, remainder, so_far)
return Set.new if remainder < 0 #did not find a sum
return [so_far].to_set if remainder == 0 #found a sum
car = vector[0]
/*
* Used in our tests to make the RESTful calls
*/
private ResultActions invokeAllHeroes() throws Exception {
return mvc.perform(get(BASE_URL).accept(MediaType.APPLICATION_JSON));
}
private ResultActions invokeSearchHeroes(String term) throws Exception {
return mvc.perform(get(BASE_URL + "search/name?contains=" + term).accept(MediaType.APPLICATION_JSON));
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
public class HeroesBackendApplicationTests {
private static final String BASE_URL = "/heroes/";
private static final Logger LOG = LoggerFactory.getLogger(HeroesBackendApplicationTests.class);
@Autowired
package io.navan.heroesbackend;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation...
@PostMapping(consumes = "application/json", produces = "application/json")
@ResponseStatus(HttpStatus.CREATED)
public Hero createHero(Hero hero) {
LOG.debug("createHero: {}", hero.getName());
Hero createdHero = heroRepository.save(hero);
LOG.debug("Created hero {} with id {}", createdHero.getName(), createdHero.getId());
return createdHero;
}
@PostMapping(consumes = "application/json", produces = "application/json")
@ResponseStatus(HttpStatus.CREATED)
public Hero createHero(Hero hero) {
LOG.debug("createHero: {}", hero.getName());
Hero createdHero = heroRepository.save(hero);
LOG.debug("Created hero {} with id {}", createdHero.getName(), createdHero.getId());
return createdHero;
}
package io.navan.heroesbackend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration;
@SpringBootApplication(exclude = RepositoryRestMvcAutoConfiguration.class)
public class HeroesBackendApplication {
public static void main(String[] args) {
package io.navan.heroesbackend;
import java.util.Arrays;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
package io.navan.heroesbackend;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
import io.navan.heroesbackend.Hero;
@Configuration
public class RepositoryConfig extends RepositoryRestConfigurerAdapter {