Ejemplo n.º 1
0
class FeatureService(object):

    @staticmethod
    def get_instance():
        try:
            return FeatureService.__instance
        except AttributeError:
            current_app.logger.info('initializing feature service')
            FeatureService.__instance = FeatureService()
            FeatureService.__instance.load()
        return FeatureService.__instance

    endpoint = None
    qp = None

    def __init__(self, config_endpoint=CONFIG_ENDPOINT):
        self.endpoint = config_endpoint
        self.qp = QueryParser(['css', 'css3'], ['2d'])

    def load(self, config_endpoint=None):
        endpoint = config_endpoint or self.endpoint
        try:
            r = requests.get(endpoint)
            self.parse(r.json())
            return True
        except requests.exceptions.RequestException as e:
            current_app.logger.error(
                'Error loading config for FeatureService: %r' %
                e.message
            )
            return False

    def parse(self, data):
        try:
            for item in data:
                slug = item.get('name').split('.json')[0]
                self.qp.add_valid_slug(slug)
        except AttributeError as e:
            current_app.logger.error(
                'Error parsing config for FeatureService: %r' %
                e.message
            )
            current_app.logger.error(e.message)

    def get_feature(self, slug):
        if self.qp.is_valid_slug(slug):
            feature = FeatureModel(slug)
            feature.load()
            return feature
        return None

    def search(self, query):
        slug = self.qp.get_slug(query)
        if slug:
            feature = FeatureModel(slug)
            feature.load()
            return feature
        return None
Ejemplo n.º 2
0
def test_prefixes():
    qp = QueryParser(['css'])
    qp.add_valid_slug('css-transitions')
    qp.add_valid_slug('css-boxshadow')
    qp.add_valid_slug('svg-css')
    assert_equal(qp.get_slug('Trans itions'), 'css-transitions')
    assert_equal(qp.get_slug(' Box shadOw '), 'css-boxshadow')
    assert_equal(qp.get_slug('svg css'), 'svg-css')
Ejemplo n.º 3
0
 def __init__(self, config_endpoint=CONFIG_ENDPOINT):
     self.endpoint = config_endpoint
     self.qp = QueryParser(['css', 'css3'], ['2d'])
def test_minify():
    qp = QueryParser(['css'])
    assert_equal(qp.minify('svg-css'), 'svgcss')
Ejemplo n.º 5
0
def test_convert_to_slug():
    qp = QueryParser()
    qp.add_valid_slug('ambient-light')
    qp.add_valid_slug('arrow-functions')
    assert_equal(qp.get_slug('Ambient Light'), 'ambient-light')
    assert_equal(qp.get_slug("arrowfunctions"), 'arrow-functions')
Ejemplo n.º 6
0
def test_suffix_removal():
    qp = QueryParser([], ['2d'])
    qp.add_valid_slug('transforms2d')
    assert_equal(qp.get_slug("transforms"), "transforms2d")
Ejemplo n.º 7
0
def test_space_removal():
    qp = QueryParser()
    qp.add_valid_slug('arrow-functions')
    assert_equal(qp.get_slug("arrow   functions"), 'arrow-functions')
Ejemplo n.º 8
0
def test_hyphen_removal():
    qp = QueryParser()
    qp.add_valid_slug('websockets')
    assert_equal(qp.get_slug('web-sockets'), 'websockets')