Skip to content

Instantly share code, notes, and snippets.

View monorkin's full-sized avatar
👻
Boo!

Stanko Krtalić monorkin

👻
Boo!
View GitHub Profile
# Copyright 2018 Stanko K.R. <me@stanko.io>
#
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject
# to the following conditions:
require 'net/http'
require 'uri'
require 'json'
fetch =
self.method(:URI) \
>> Net::HTTP.method(:get) \
>> JSON.method(:parse) \
>> -> response { response.dig('bpi', 'EUR', 'rate') || '0' } \
>> -> value { value.gsub(',', '') } \
f = -> x { x + 2 }
g = -> x { x * 2 }
# h is the composition of g and f
h = f << g
# h is the same as -> x { (x * 2) + 2 }
[1, 2, 3].map(&h) # => [4, 6, 8]
# This is exactly the same as
[1, 2, 3].map(&g).map(&f) # => [4, 6, 8]
fetch_transactions =
ApiClient.new(api_token).method(:get)
>> JSON.method(:parse)
>> (-> response { response['data']['transactions'] })
fetch_transaction.call('/api/current_user/transactions')
f = -> x { x + 2 }
g = -> x { x * 2 }
# h is the composition of f and g
h = f >> g
# h is the same as -> x { (x + 2) * 2 }
[1, 2, 3].map(&h) # => [6, 8, 10]
# This is exactly the same as
[1, 2, 3].map(&f).map(&g) # => [6, 8, 10]
array = [1,2,3,4,5]
array.map # => #<Enumerator: ...>
array.map { |i| i * 2 } # => [2, 4, 6, 8, 10]
class MyAppApiClient
attr_reader :json_client
attr_reader :xml_client
def initialize(api_token)
@json_client = ApiClient.new(API_KEY, JSON.method(:parse).to_proc)
@xml_client = ApiClient.new(API_KEY, XML.method(:parse).to_proc)
end
def current_account
# Wrapper around a HTTP library
class ApiClient; ... end
# Knows how to decode JSON responses from the API
class JSONApiClient < ApiClient; ... end
# Knows how to decode XML responses from the API
class XMLApiClient < ApiClient; ... end
# Exposes an API's endpoints as methods on an object
@monorkin
monorkin / error_objects_builder.rb
Created November 19, 2018 08:46
A simple yet effective GraphQL error builder for Ruby
# frozen_string_literal: true
# BasicService implements `self.call(*args)` so that you can
# use `ErrorObjectsBuilder.call(errors)` instead of
# `ErrorObjectsBuilder.new(errors).call`
class ErrorObjectsBuilder < BasicService
Error = Struct.new(:key, :index, :messages, :suberrors)
def initialize(errors, camelize_keys = true)
@errors = errors
var evtSource = new EventSource('/stream');
evtSource.onmessage = function(e) {
console.log('DATA', e.data);
}