This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This code is license under the MIT license | |
# Full text: https://opensource.org/licenses/MIT | |
# Author Stanko K.R. <[email protected]> | |
# Usage: | |
# ```ruby | |
# user.avatar = S3UrlToShrineObjectConverter.call( | |
# arguments[:input][:avatar][:url], | |
# name: arguments[:input][:avatar][:filename] | |
# ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2018 Stanko K.R. <[email protected]> | |
# | |
# 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(',', '') } \ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch_transactions = | |
ApiClient.new(api_token).method(:get) | |
>> JSON.method(:parse) | |
>> (-> response { response['data']['transactions'] }) | |
fetch_transaction.call('/api/current_user/transactions') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array = [1,2,3,4,5] | |
array.map # => #<Enumerator: ...> | |
array.map { |i| i * 2 } # => [2, 4, 6, 8, 10] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |