Skip to content

Instantly share code, notes, and snippets.

Avatar

Filippos Vasilakis vasilakisfil

View GitHub Profile
@vasilakisfil
vasilakisfil / gist:9722841
Last active Aug 29, 2015
Share state between ruby objects
View gist:9722841
class TestClass
@class_var_alternative = "class_var_alternative"
class << self
attr_accessor :class_var_alternative
end
end
puts TestClass.class_var_alternative
TestClass.class_var_alternative = "new value"
@vasilakisfil
vasilakisfil / PacketInfo.java
Created Apr 21, 2014
Useless Java getters/setters
View PacketInfo.java
package server;
public class PacketInfo {
private String sipAddress;
private String branch;
private String viaAddress;
public String receiverUser;
public String senderUsername;
public String senderAddress;
View authentication_helper.rb
module AuthenticationHelper
def sign_in(user)
header('Authorization', "Token token=\"#{user.authentication_token}\", user_email=\"#{user.email}\"")
end
def create_and_sign_in_user
user = FactoryGirl.create(:user)
sign_in(user)
return user
end
View users_spec.rb
require 'rails_helper'
describe Api::V1::UsersController, type: :api do
context :index do
before do
5.times{ FactoryGirl.create(:user) }
5.times{ FactoryGirl.create(:admin) }
FactoryGirl.create(:super_admin)
sign_in(User.regular.last)
end
@vasilakisfil
vasilakisfil / example.rb
Last active Aug 29, 2015
namespaced mixins
View example.rb
#Mixins as I sometimes want them. Proper Composition.
#It probably requires different approach on how we write mixins atm.
#@vasilakisfil
module NamespacedMixin
module ClassMethods
def namespace(name, as:)
namespace = as
namespaced_class = name.split('::').inject(Object) {|o,c| o.const_get c}
@vasilakisfil
vasilakisfil / 0_reuse_code.js
Last active Aug 29, 2015
Here are some things you can do with Gists in GistBox.
View 0_reuse_code.js
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@vasilakisfil
vasilakisfil / put jsonapi
Created Nov 26, 2014
PUT relationship in JSONAPI
View put jsonapi
PUT /articles/1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"articles": {
"title": "Rails is a Melting Pot",
"links": {
"author": "1"
}
@vasilakisfil
vasilakisfil / console.rb
Last active Aug 29, 2015
Ruby hashes inconsistency
View console.rb
# copy and store in another variable:
[16] pry(main)> hash1 = {a: 'asdasd', b: 'asdasd'}
=> {:a=>"asdasd", :b=>"asdasd"}
[17] pry(main)> hash2 = hash1
=> {:a=>"asdasd", :b=>"asdasd"}
[18] pry(main)> hash2.delete(:a)
=> "asdasd"
[19] pry(main)> hash2
=> {:b=>"asdasd"}
[20] pry(main)> hash1
@vasilakisfil
vasilakisfil / rspec-api.rb
Last active Aug 29, 2015
rspec API helpers
View rspec-api.rb
class ObjectHash
attr_accessor :hash
def initialize(hash)
@hash = HashWithIndifferentAccess.new(hash)
end
def method_missing(name)
return hash[name] if hash.key? name
View rspec_api_helper.rb
#rspec/support/rspec_api_helper.rb
module RspecApiHelper
module ExampleMethods
def objectize_resources(json, root: root)
array = []
array_hash = HashWithIndifferentAccess.new(MultiJson.load(json))
if root
array_hash = array_hash[root]
end