Code Monkey home page Code Monkey logo

urule-scala's Introduction

urule-scala

Extract attributes from URL according to custom rule.

Installation

SBT

resolvers += "Urule Scala Maven Repository" at "https://yubessy.github.io/urule-scala/mvn/"

libraryDependencies += "com.github.yubessy" %% "urule" % "0.0.3"

Usage

Quickstart

Write your own rule in JSON, YAML or any other format that can be parsed into JSON-like structured object:

- host: '^example\.com$'
  attrs:
    host: example.com
  rules:
    - path: '^/foo'
      attrs:
        path: foo

    - path: '^/bar'
      attrs:
        path: bar

- host: '^another\.example\.com$'
  attrs:
    host: example.com
    subdomain: another

Build rule and apply it to URL:

// Prepare parser on your need
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.scala.DefaultScalaModule
val mapper = new ObjectMapper(new YAMLFactory())
mapper.registerModule(DefaultScalaModule)

// Build rule
import com.github.yubessy.urule.Rule
val text = Source.fromFile("example.yaml").mkString
val raw = mapper.readValue(text, classOf[Seq[Map[String, _]]])
val rule = Rule(raw)

// Apply rule to URL and get values

rule.applyTo("http://example.com/foo")
// => Some(Result(None,Map(host -> example.com, path -> foo)))

rule.applyTo("http://another.example.com/")
// => Some(Result(None,Map(host -> example.com, subdomain -> another)))

rule.applyTo("http://other.com/")
// => None

Detail

No docs are available currently. See test cases for detailed usage.

Build and Publish

Use Github Pages as Maven Repository

$ git checkout master
$ sbt publish
$ git checkout gh-pages
$ git add mvn/
$ git commit -m "vx.x.x"
$ git push origin gh-pages

urule-scala's People

Contributors

yubessy avatar

Stargazers

 avatar  avatar onunu avatar taise avatar

Watchers

James Cloos avatar taise avatar  avatar

urule-scala's Issues

Can't use regexp named capturing in Scala version <2.11

Since Scala version <2.11 doesn't support named capturing in regex pattern, Extractor cannot get these values.

Current:

import com.github.yubessy.urule.Rule

// indexed group works
val r = Rule(Map("host" -> """.+\.(?<tld>com|org)""", "category" -> """$host.1"""))
r.applyTo("http://example.com/")
// => Some(Result(Some(com),Map()))

// named group doesn't work...
val r = Rule(Map("host" -> """.+\.(?<tld>com|org)""", "category" -> """$host.tld"""))
r.applyTo("http://example.com/")
// => None

Expected:

import com.github.yubessy.urule.Rule

// named group should work
val r = Rule(Map("host" -> """.+\.(?<tld>com|org)""", "category" -> """$host.tld"""))
r.applyTo("http://example.com/")
// => Some(Result(Some(com),Map()))

url.pathでクエリパラメータが取れない

case Some(re) => re.findFirstMatchIn(uri.path).map(x => Some(x))

ここのurl.pathが、クエリパラメータを返してくれないみたいなので、

- caption: la

  rules:
    - path: '^/inquiry/?\?.*id(?:\[\])?=(?:\d+)'
      category: entry
    - path: '^/inquiry/$'
      category: thanks

  cases:
    - url: http://example.com/inquiry/?id=4868161
      category: entry

こんな感じのテストケースが、

(uri:,http://example.com/inquiry/?id=4868161)
(category:,None)
(uri.path:,/inquiry/)
(re.regex:,^/inquiry/?\?.*id(?:\[\])?=(?:\d+))
(uri.path:,/inquiry/)
(re.regex:,^/inquiry/$)
(uri:,http://example.com/inquiry/?id=4868161)
(category:,Some(thanks))

こんな感じで失敗します。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.