Skip to content

Instantly share code, notes, and snippets.

View alvises's full-sized avatar

Alvise Susmel alvises

View GitHub Profile
-(void)presentPopoverFromView:(UIView *)fromView
onModalViewController:(UIViewController*)modalViewController {
[self presentPopoverFromView:fromView];
 
//converting the origin of the popover from the old parent view to the new modal view
self.origin = [modalViewController.view convertPoint:self.origin fromView:_parentView];
 
//the new parent view is the modal view
[self.view removeFromSuperview];
<HTML>
<head>
<title>GeoIP test</title>
<script src="http://geoip.poeticoding.com/geoip.js" type="text/javascript"></script>
</head>
<body>
<div id="details">Loading...</div>
</body>
<script>
var geoip = new GeoIP();
function GeoIP(forcedIP) {
var locateUrl = "http://geoip.poeticoding.com/locate";
//locate function, success or fail callbacks will be triggered
//once the request to the server is finished
this.locate = function(successCallback,failCallback) {
var xhr = new XMLHttpRequest();
if(forcedIP) locateUrl += "?ip="+forcedIP;
xhr.open("GET", locateUrl, true);
xhr.timeout = 60000;
xhr.onload = function (e) {
app.get('/locate', function(req, res) {
//getting the ip of the client from the request headers or remoteAddress
var ip = req.param("ip");
ip = ip || req.headers['x-forwarded-for'] || req.connection.remoteAddress;
var net = ip.split(".").slice(0,3).join(".")+".0"; //aaa.bbb.ccc.0
mongodb.connect("mongodb://127.0.0.1:27017/poetic_geoips",function(err,db){
if(err) throw err;
var ips = db.collection("ips");
var express = require('express');
var mongodb = require('mongodb');
var app = express();
//ENABLE CORS
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
@alvises
alvises / index.html
Last active September 16, 2016 15:52
Need a GeoIP Service? Build Your Own One with NodeJS and MongoDB
<html>
<head>
<title>MaxMind GeoIP test</title>
<script type="text/javascript" src="//js.maxmind.com/js/apis/geoip2/v2.0/geoip2.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script>
</head>
<body>
<h1>Loading...</h1>
<script type="text/javascript">
$(document).ready(function(){
@alvises
alvises / setup.ex
Created September 15, 2016 16:32
supervised genstage, producer, producer_consumer, consumer
{:ok, p} = Producer.start_link
workers_num = 20
workers = Enum.map(1..workers_num, fn (n)->
worker(ProducerConsumer, [worker_name(n)], id: n)
end)
worker_names = Enum.map 1..8, &worker_name/1
#consumer subscribes to the producer names passed
consumers = [worker(Consumer, [worker_names])]
@alvises
alvises / supervised_genstages.ex
Created September 15, 2016 13:30
supervised genstages
{:ok, p} = Producer.start_link
children = [
worker(ProducerConsumer, [:pc_1], id: 1),
worker(ProducerConsumer, [:pc_2], id: 2)
]
{:ok, sup} = Supervisor.start_link children, strategy: :one_for_one
{:ok, consumer} = Consumer.start [:pc_1, :pc_2]
@alvises
alvises / app.ex
Last active March 14, 2018 09:15
GenStage and twitter stream
defmodule Twitter do
def start(_type, _args) do
import Supervisor.Spec
workers = [
worker(Twitter.Producer, ["#Rio2016"]),
worker(Twitter.Consumer, [], id: 1),
worker(Twitter.Consumer, [], id: 2),
worker(Twitter.Consumer, [], id: 3)
var url, contentType;
casper.start(....)
//download time
//waitForResource because sometime the server is redirecting multiple time
//the browser, so we need the final url to download it as pdf (or render it as html)
.waitForResource(function testResource(resource) {
console.log("resource: "+resource.status+" : "+resource.contentType+" : "+resource.url);
if(resource.status == 200) {
url = resource.url;