Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns hello-test.core
- (:require [clojure.spec.alpha :as s])
- (:require [clojure.string :as str]))
- (defn raw-param-to-typed [line]
- (let [[name value] (str/split line #"=")]
- {:name name :value value}))
- (defn split-raw-params [line]
- (str/split line #"&"))
- (defn split-raw-path [line]
- (str/split line #"/"))
- (defn pattern-to-command [str-pattern]
- (let [[_ command pattern] (re-matches #"(\w+)\(([A-Za-z0-9\/\?=\.]+)\)" str-pattern)]
- {:command (keyword command) :pattern pattern}))
- (defn param-to-keyword [[f & tail]]
- (when (and (= \? f) (seq? tail))
- (keyword (apply str tail))))
- (defn scramble-url [url]
- (let [url-pattern #"(?:http[s]?:\/\/)?(?:www\.)?([A-Za-z0-9\-\.]+\.\w+)(?:\/?)((?:[A-Za-z\-0-9\.]+(?:\/)?)*)\??((?:(?:\w+)=(?:\w+)&?)*)"
- [_ host path params] (re-matches url-pattern url)
- query-map (map raw-param-to-typed (split-raw-params params))]
- {:host host :path path :queryparams query-map}))
- (scramble-url "http://twitter.com/bradfitz/status/562360748727611392")
- (defn build-path-component
- [pattern-comp url-comp]
- (if (= (first pattern-comp) \?)
- {:type :path-param :result [(param-to-keyword pattern-comp) url-comp]}
- {:type :path-match :result (= pattern-comp url-comp)}))
- (defn get-path
- [pattern path]
- (let [paths-compnents (split-raw-path path)
- pattern-compnents (split-raw-path pattern)
- paths (map build-path-component pattern-compnents paths-compnents)]
- (when (and (= (count paths-compnents) (count pattern-compnents))
- (every? true? (map :result (filter #(= (:type %) :path-match) paths))))
- (map :result (filter #(= (:type %) :path-param) paths)))))
- (defn get-queryparams
- [pattern url-queryparams]
- (let [[name key] (str/split pattern #"=")
- url-param (->> url-queryparams
- (filter #(= name (:name %)))
- (first))]
- [(param-to-keyword key) (:value url-param)]))
- (defn apply-command
- [cmd url]
- (let [{:keys [command pattern]} cmd
- url-info (scramble-url url)]
- (cond
- (= command :host) (when (= (:host url-info) pattern)
- (list [:host pattern]))
- (= command :path) (get-path pattern (:path url-info))
- (= command :queryparam) (list (get-queryparams pattern (:queryparams url-info)))
- :else nil)))
- (defn recognize
- [commands url]
- (let [components (apply concat (map #(apply-command % url) commands))]
- (when (every? #(some? (second %)) components)
- components)))
- (defn new-pattern [str-pattern]
- (map (comp pattern-to-command str/trim) (str/split str-pattern #";")))
- (recognize (new-pattern "host(twitter) path(shots/?id) queryparam(offset=?offset)")
- "https://twitter.com/shots/1905065-Travel-Icons-pack?list=users&offset=1")
- (def twitter (new-pattern "host(twitter.com); path(?user/status/?id);"))
- (recognize twitter "http://twitter.com/bradfitz/status/562360748727611392")
- ;; => [[:id 562360748727611392] [:user "bradfitz"]]
- (def dribbble (new-pattern "host(dribbble.com); path(shots/?id); queryparam(offset=?offset);"))
- (recognize dribbble "https://dribbble.com/shots/1905065-Travel-Icons-pack?list=users&offset=1")
- ;; => [[:id "1905065-Travel-Icons-pack"] [:offset "1"]]
- (recognize dribbble "https://twitter.com/shots/1905065-Travel-Icons-pack?list=users&offset=1")
- ;; => nil ;; host mismatch
- (recognize dribbble "https://dribbble.com/shots/1905065-Travel-Icons-pack?list=users")
- ;; => nil ;; offset queryparam missing
- (def dribbble2 (new-pattern "host(dribbble.com); path(shots/?id); queryparam(offset=?offset); queryparam(list=?type);")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement