Skip to content

Instantly share code, notes, and snippets.

View nderkach's full-sized avatar
✏️

Nikolay Derkach nderkach

✏️
View GitHub Profile
@nderkach
nderkach / reviews.txt
Last active November 26, 2017 10:15
Airbnb API reviews response
{
'reviews':[
{
'author':{
'first_name':'Maryann',
'has_profile_pic':True,
'id':40613795,
'picture_url':'https://a0.muscache.com/im/pictures/c77f54a1-c3d0-4742-93af-f2e53b8f5145.jpg?aki_policy=profile_x_medium',
'smart_name':'Maryann',
'thumbnail_url':'https://a0.muscache.com/im/pictures/c77f54a1-c3d0-4742-93af-f2e53b8f5145.jpg?aki_policy=profile_small'
@nderkach
nderkach / schema.py
Last active November 26, 2017 11:09
Graphene schema for Airbnb reviews
from graphene import ObjectType, String, Boolean, ID, Field, Int
class User(ObjectType):
first_name = String()
has_profile_pic = Boolean()
id = ID()
picture_url = String()
smart_name = String()
thumbnail_url = String()
from graphene import ObjectType, List
class Query(ObjectType):
reviews = List(Review, id=Int(required=True))
@nderkach
nderkach / json2obj.py
Created November 26, 2017 11:55
How to convert JSON data into a Python object
# stolen from https://stackoverflow.com/questions/6578986/how-to-convert-json-data-into-a-python-object/15882054#15882054
def _json_object_hook(d):
return namedtuple('X', d.keys())(*d.values())
def json2obj(data):
return json.loads(data, object_hook=_json_object_hook)
class Query(ObjectType):
reviews = List(Review, id=Int(required=True))
def resolve_reviews(self, args, context, info):
reviews = api_call(args.get("id"))["reviews"]
return json2obj(json.dumps(reviews))
@nderkach
nderkach / flask_graphql.py
Last active November 26, 2017 12:04
A simple Flask app serving GraphQL API
from flask import Flask
from schema import Query
from flask_graphql import GraphQLView
from graphene import Schema
import os
view_func = GraphQLView.as_view(
'graphql', schema=Schema(query=Query), graphiql=True)
// get realtime database pointer
let ref = Database.database().reference()
// get currently authenticated user
guard let currentUser = Auth.auth().currentUser else {
return
}
// store any data at a given path
ref.child("users/\(currentUser.uid)/twitter/token").setValue(authToken)
func updateLocation(_ location: CLLocation) {
if let uid = Auth.auth().currentUser?.uid {
let latitude = location.coordinate.latitude + (Double(arc4random()) / Double(UINT32_MAX) / 10 - 0.05)
let longitude = location.coordinate.longitude + (Double(arc4random()) / Double(UINT32_MAX) / 10 - 0.05)
self.ref.child("users/\(uid)/location").setValue(["lat": latitude, "lon": longitude])
}
}
function getCountry(addrComponents) {
var address = {};
for (var i = 0; i < addrComponents.length; i++) {
if (addrComponents[i].types[0] == "country") {
address["country_short"] = addrComponents[i].short_name;
address["country"] = addrComponents[i].long_name;
}
if (addrComponents[i].types[0] == "locality") {
address["city"] = addrComponents[i].long_name;
}
{
"users" : {
"HvzLGDoF2yZKOFywjuWVGEafEhs1" : {
"location" : {
"lat" : 37.37016786494953,
"lon" : -122.06975715302978
},
"public" : {
"city" : "Los Altos",
"country" : "United States",