Advertisement
Guest User

Untitled

a guest
May 10th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns hello-test.core
  2.   (:require [clojure.spec.alpha :as s])
  3.   (:require [clojure.string :as str]))
  4.  
  5. (defn raw-param-to-typed [line]
  6.   (let [[name value] (str/split line #"=")]
  7.     {:name name :value value}))
  8.  
  9. (defn split-raw-params [line]
  10.   (str/split line #"&"))
  11.  
  12. (defn split-raw-path [line]
  13.   (str/split line #"/"))
  14.  
  15. (defn pattern-to-command [str-pattern]
  16.   (let [[_ command pattern] (re-matches #"(\w+)\(([A-Za-z0-9\/\?=\.]+)\)" str-pattern)]
  17.     {:command (keyword command) :pattern pattern}))
  18.  
  19. (defn param-to-keyword [[f & tail]]
  20.   (when (and (= \? f) (seq? tail))
  21.     (keyword (apply str tail))))
  22.  
  23. (defn scramble-url [url]
  24.   (let [url-pattern #"(?:http[s]?:\/\/)?(?:www\.)?([A-Za-z0-9\-\.]+\.\w+)(?:\/?)((?:[A-Za-z\-0-9\.]+(?:\/)?)*)\??((?:(?:\w+)=(?:\w+)&?)*)"
  25.         [_ host path params] (re-matches url-pattern url)
  26.         query-map (map raw-param-to-typed (split-raw-params params))]
  27.     {:host host :path path :queryparams query-map}))
  28.  
  29. (scramble-url "http://twitter.com/bradfitz/status/562360748727611392")
  30.  
  31. (defn build-path-component
  32.   [pattern-comp url-comp]
  33.   (if (= (first pattern-comp) \?)
  34.     {:type :path-param :result [(param-to-keyword pattern-comp) url-comp]}
  35.     {:type :path-match :result (= pattern-comp url-comp)}))
  36.  
  37. (defn get-path
  38.   [pattern path]
  39.   (let [paths-compnents (split-raw-path path)
  40.         pattern-compnents (split-raw-path pattern)
  41.         paths (map build-path-component pattern-compnents paths-compnents)]
  42.     (when (and (= (count paths-compnents) (count pattern-compnents))
  43.                (every? true? (map :result (filter #(= (:type %) :path-match) paths))))
  44.       (map :result (filter #(= (:type %) :path-param) paths)))))
  45.  
  46. (defn get-queryparams
  47.   [pattern url-queryparams]
  48.   (let [[name key] (str/split pattern #"=")
  49.         url-param (->> url-queryparams
  50.                        (filter #(= name (:name %)))
  51.                        (first))]
  52.     [(param-to-keyword key) (:value url-param)]))
  53.  
  54. (defn apply-command
  55.   [cmd url]
  56.   (let [{:keys [command pattern]} cmd
  57.         url-info (scramble-url url)]
  58.     (cond
  59.       (= command :host) (when (= (:host url-info) pattern)
  60.                           (list [:host pattern]))
  61.       (= command :path) (get-path pattern (:path url-info))
  62.       (= command :queryparam) (list (get-queryparams pattern (:queryparams url-info)))
  63.       :else nil)))
  64.  
  65. (defn recognize
  66.   [commands url]
  67.   (let [components (apply concat (map #(apply-command % url) commands))]
  68.     (when (every? #(some? (second %)) components)
  69.       components)))
  70.  
  71. (defn new-pattern [str-pattern]
  72.   (map (comp pattern-to-command str/trim) (str/split str-pattern #";")))
  73.  
  74. (recognize (new-pattern "host(twitter) path(shots/?id) queryparam(offset=?offset)")
  75.            "https://twitter.com/shots/1905065-Travel-Icons-pack?list=users&offset=1")
  76.  
  77. (def twitter (new-pattern "host(twitter.com); path(?user/status/?id);"))
  78. (recognize twitter "http://twitter.com/bradfitz/status/562360748727611392")
  79. ;; => [[:id 562360748727611392] [:user "bradfitz"]]
  80.  
  81. (def dribbble (new-pattern "host(dribbble.com); path(shots/?id); queryparam(offset=?offset);"))
  82. (recognize dribbble "https://dribbble.com/shots/1905065-Travel-Icons-pack?list=users&offset=1")
  83. ;; => [[:id "1905065-Travel-Icons-pack"] [:offset "1"]]
  84. (recognize dribbble "https://twitter.com/shots/1905065-Travel-Icons-pack?list=users&offset=1")
  85. ;; => nil ;; host mismatch
  86. (recognize dribbble "https://dribbble.com/shots/1905065-Travel-Icons-pack?list=users")
  87. ;; => nil ;; offset queryparam missing
  88.  
  89. (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