Giter Club home page Giter Club logo

ipify.clj's Introduction

ipify.clj

CircleCI Dependencies Status GitHub license

An unofficial client library for https://www.ipify.org: A Simple IP Address API.

Clojars Project

Dependencies

This library can worked on both Clojure/ClojureSript, you need following minimal version:

  • Clojure 1.8.0 ↑
  • ClojureScript 1.9.473 ↑

Usage (Clojure)

In Clojure, we use synchronize method to retrive data from https://api.ipify.org.

(ns ipify-test.core
  (:require [coldnew.ipify :as ipify]))

(defn -main []
  ;; default return edn data
  (println "Public ip (default):" (ipify/get-public-ip))    ; => {:ip "98.207.254.136"}
  ;; You can specify the return type you want
  (println "Public ip (text):" (ipify/get-public-ip :text)) ; => "98.207.254.136"
  (println "Public ip (json):" (ipify/get-public-ip :json)) ; => {\"ip\": \"98.207.254.136\"}
  (println "Public ip (edn):"  (ipify/get-public-ip :edn))  ; => {:ip "98.207.254.136"}
  ) 

Usage (ClojureScript on Browser/Node.js)

ClojureScript use asynchronize method instead, it will return core.async channel. When a request has completed or failed it is put on that channel. You can take the response from that channel with the <! function within a go block.

(ns ipify-test.core
  (:require-macros [cljs.core.async.macros :refer [go]])
  (:require [coldnew.ipify :as ipify]
            [cljs.core.async :as async]))

(go
  ;; return EDN format (default)
  (let [rsp (async/<! (ipify/get-public-ip))]
    (.log js/console "public ip (default): " rsp))  ; => {:ip "98.207.254.136"}

  ;; return EDN format (edn)
  (let [rsp (async/<! (ipify/get-public-ip :edn))]
    (.log js/console "public ip (edn): " rsp))      ; => {:ip "98.207.254.136"}
  
  ;; return TEXT format (text)
  (let [rsp (async/<! (ipify/get-public-ip :text))]
    (.log js/console "public ip (text): " rsp))     ; => "98.207.254.136"

  ;; return JSON-string format (json)
  (let [rsp (async/<! (ipify/get-public-ip :json))]
    (.log js/console "public ip (json): " rsp))     ; => {\"ip\": \"98.207.254.136\"}
  )

Q & A

Q: Why I can't see return result on my browser ?

A: When use on browser, we use jsonp to fetch data. Please make sure your browser's adblock doesn't disable the jsonp call.

License

Copyright © 2017 Yen-Chin, Lee <[email protected]>

Distributed under the MIT License.

ipify.clj's People

Contributors

coldnew avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

plumpmath

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.