Skip to content

Instantly share code, notes, and snippets.

View Bajena's full-sized avatar
🎩
Working on stuff

Jan Bajena Bajena

🎩
Working on stuff
View GitHub Profile
@Bajena
Bajena / RetriableRequestsBatch.gs
Created August 25, 2019 16:23
Retriable requests batch for Google Apps Scripts
/**
* Calls provided HTTP requests batch and retries in case of errors
*
* @param {object} fetchService - service for performing HTTP requests. Defaults to UrlFetchApp provided by Google.
* @param {Array<object>} requests - Array of request param objects
* (https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchurl-params)
*
* @return {object} RetriableRequestsBatch.
*/
function RetriableRequestsBatch(fetchService, requests) {
class Loader
def load
Enumerator.new { |main_enum| stream(main_enum) }
end
private
def stream(main_enum)
reader = nil
file_uri.open do |file|
@Bajena
Bajena / config.yml
Created July 22, 2018 11:09
Build + deploy Google Data Studio connector in CircleCI
# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
build:
docker:
- image: circleci/node:7.10
@Bajena
Bajena / Oauth.gs
Created June 23, 2018 19:16
Google script functions for performing simple Spotify Oauth flow
var oauth = {};
/** @const */
oauth.OAUTH_CLIENT_ID = 'OAUTH_CLIENT_ID';
/** @const */
oauth.OAUTH_CLIENT_SECRET = 'OAUTH_CLIENT_SECRET';
/**
* This builds an OAuth2 service for connecting to Spotify
@Bajena
Bajena / DataCache.gs
Created June 24, 2018 18:21
DataCache with chunking
/**
* Constructor for DataCache.
* More info on caching: https://developers.google.com/apps-script/reference/cache/cache
*
* @param {object} cacheService - GDS caching service
* @param {Date} startDate - beggining of GDS request interval
* @param {Date} endDate - end of GDS request interval
*
* @return {object} DataCache.
*/
# frozen_string_literal: true
require "net/ftp"
class Loader
def load
Enumerator.new { |main_enum| stream(main_enum) }
end
private
@Bajena
Bajena / sparse_fieldsets.rb
Last active October 11, 2019 10:56
Using sparse fieldsets in ActiveModelSerializers
class BaseApiController
def render_json_api(options)
render(
options.merge(included).merge(fields: fields).merge(adapter: :json_api)
)
end
# Allows limiting fields in given resource type's payloads.
# `fields` param is sent as a hash in which:
# - keys are singular or plural resource names (e.g. `accounts` or `account`)
class BaseSerializer < ActiveModel::Serializer
include AmsLazyRelationships::Core
end
class PostSerializer < BaseSerializer
attributes :id, :title
lazy_belongs_to :user
end
class PostSerializer < ActiveModel::Serializer
attributes :id, :title
belongs_to :user
def self.lazy_user(post)
BatchLoader.for(post.user_id).batch do |user_ids|
User.where(id: user_ids)
end
end
users = posts.map { |post| post.user_lazy }
users.each { |user| puts "#{user.name}" } # SELECT * FROM users WHERE id IN (1, 2, 3)
users.each { |user| puts "#{user.address}" } # Pobierze użytkowników z cache'u