Skip to content

Commit

Permalink
docs and extra test suites
Browse files Browse the repository at this point in the history
  • Loading branch information
nu7hatch committed Jul 19, 2012
1 parent 5500f76 commit e92bfe2
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 7 deletions.
9 changes: 4 additions & 5 deletions apps/core/loggers/signup_logger.rb
@@ -1,3 +1,5 @@
require 'reusable/helpers/email_helpers'

module Airstrip
class SignupLogger < ActiveSupport::LogSubscriber
def error(event)
Expand All @@ -9,16 +11,13 @@ def error(event)
def success(event)
signup = event.payload[:signup]

escaped_email = escape_email(signup.email.to_s)
logger.info color("SIGNUP Success, #{escaped_email.bold}", GREEN)
anon_email = EmailHelpers.anonimize_email(signup.email.to_s)
logger.info color("SIGNUP Success, #{anon_email.bold}", GREEN)
end

private

def escape_email(email)
name, domain = email.split('@')
name = name.size > 3 ? name.to(2) + '...' : name.first + '...'
return [ name, domain ].join('@')
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions apps/core/models/signup.rb
Expand Up @@ -12,6 +12,8 @@ module Airstrip
# referer :string(255)
# lat :float
# lon :float
# city :string(255)
# country :string(255)
# created_at :timestamp, not null
#
class Signup < ActiveRecord::Base
Expand Down
12 changes: 12 additions & 0 deletions lib/core_ext/array_to_csv.rb
@@ -1,6 +1,18 @@
require 'csv'

class Array
# Public: Converts array of hashes to csv format.
#
# options - A Hash with conversion settings.
# csv_options - A Hash with csv builder options.
#
# Usage
#
# list = [{:foo => 1, :bar => 2}, {:foo => 3, :bar => 4}]
# list.to_csv # => "foo,bar\n1,2\n3,4\n"
# list.to_csv(:headers => false) # => "1,2\n3,4\n"
#
# Returns a string with csv data.
def to_csv(options = {}, csv_options = {})
return '' if empty?

Expand Down
9 changes: 9 additions & 0 deletions lib/reusable/actions/test_action.rb
@@ -1,4 +1,13 @@
module Reusable
# Public: Provides action which runs frontend tests suite.
#
# Usage
#
# class App < Sinatra::Application
# include Reusable::EnvCondition
# include Reusable::TestAction
# end
#
module TestAction
def self.extended(app)
app.get "/tests", :only_env => ['development', 'test'] do
Expand Down
10 changes: 10 additions & 0 deletions lib/reusable/application.rb
@@ -1,4 +1,14 @@
module Reusable
# Public: Extended, preconfigured Sinatra application class.
#
# Usage
#
# class App < Reusable::Application
# set :root, File.dirname(__FILE__)
# end
#
# App will use ROOT/views, ROOT/assets and ROOT/assets/static
# as views, assets and public folder respectively.
class Application < Sinatra::Application
set(:public_folder) { File.join(root, 'assets/static') }
set(:views) { File.join(root, 'views') }
Expand Down
19 changes: 19 additions & 0 deletions lib/reusable/apps_foundation.rb
@@ -1,11 +1,30 @@
module Reusable
# Public: Apps foundation is a base module for the project
# namespace. Extended to the project module provides list of apps
# and handy loader.
#
# Usage
#
# module MyProject
# extend Reusable::AppsFoundation
# end
#
# MyProject.apps += %(core admin)
# MyProject.load_apps "path/to/apps"
#
module AppsFoundation
attr_accessor :apps

# Public: Returns an Array list of configured app names.
def apps
@apps ||= []
end

# Public: Loads apps from given root directory.
#
# from - A String name of the apps root directory.
#
# Returns list of loaded apps.
def load_apps(from)
apps.each do |app|
require File.join(from, app, 'app.rb')
Expand Down
2 changes: 1 addition & 1 deletion lib/reusable/asset_pipeline.rb
@@ -1,7 +1,7 @@
require 'sprockets'

module Reusable
# Internal: Asset pipeline serves all dynamically generated asssets
# Public: Asset pipeline serves all dynamically generated asssets
# through sprockets pipeline.
class AssetPipeline
# List of default asset paths.
Expand Down
13 changes: 13 additions & 0 deletions lib/reusable/conditions/env_condition.rb
@@ -1,4 +1,17 @@
module Reusable
# Public: Provides a Sinatra condition to define environment
# specific actions.
#
# Usage
#
# class App < Sinatra::Application
# include Reusable::EnvCondition
#
# get "/tests", :env_only => %w{development test} do
# erb :tests_suite, :layout => false
# end
# end
#
module EnvCondition
def self.extended(app)
app.set(:only_env) do |*envs|
Expand Down
15 changes: 14 additions & 1 deletion lib/reusable/conditions/json_params_condition.rb
@@ -1,12 +1,25 @@
module Reusable
# Public: Provides a Sinatra condition that parses request.body
# in JSON format and merges it with params.
#
# Usage
#
# class App < Sinatra::Application
# include Reusable::JSONParamsCondition
#
# get "/foo", :json_data_params => true, :provides => json do
# # params[:text] comes from request.body
# { :requested_text => params[:text] }.to_json
# end
# end
#
module JSONParamsCondition
def self.extended(app)
app.set(:json_data_params) do |*args|
condition do
begin
params.merge!(JSON.parse(request.body.read.to_s))
rescue JSON::ParserError
# ...
end

true
Expand Down
11 changes: 11 additions & 0 deletions lib/reusable/conditions/simple_auth_condition.rb
@@ -1,4 +1,15 @@
module Reusable
# Public: Provides dummy authentication combined with AuthHelpers.
#
# Usage
#
# class App < Sinatra::Application
# include SimpleAuthCondition
#
# get "/admin/foo", :auth => true do
# erb "Something only admin can see..."
# end
#
module SimpleAuthCondition
def self.extended(app)
app.set(:auth) do |*envs|
Expand Down
24 changes: 24 additions & 0 deletions lib/reusable/helpers/email_helpers.rb
@@ -0,0 +1,24 @@
module Reusable
# Public: Collection of email related helpers.
module EmailHelpers
extend self

# Public: Returns anonimized version of the email. It truncates
# username to 3 first characters with the dots.
#
# email - A String email to be escaped
#
# Example
#
# include Reusable::EmailHelpers
# anonimize_email("marty@macfly.com") # => "mar...@macfly.com"
#
# Returns an anonizmized email.
def anonimize_email(email)
name, domain = email.split('@')
name = name.size > 3 ? name.to(2) + '...' : name.first + '...'
name.gsub!(/\.{4,}/, '...')
return [ name, domain ].join('@')
end
end
end
26 changes: 26 additions & 0 deletions spec/reusable/apps_foundation_spec.rb
@@ -0,0 +1,26 @@
require File.expand_path("../../spec_helper", __FILE__)
require 'reusable/apps_foundation'

module DummyProject
extend Reusable::AppsFoundation
end

describe Reusable::AppsFoundation do
subject do
DummyProject
end

describe ".apps" do
it "by default is an empty array" do
subject.apps.should == []
end
end

describe ".load_apps" do
it "loads list of registered apps from given directory" do
subject.apps += %w(dummy)
subject.expects(:require).with('/tmp/dummy/app.rb')
subject.load_apps('/tmp')
end
end
end
1 change: 1 addition & 0 deletions spec/reusable/heplers/auth_helpers_spec.rb
@@ -1,4 +1,5 @@
require File.expand_path("../../../spec_helper", __FILE__)
require 'reusable/helpers/auth_helpers'

class DummyAppWithAuthHelpers
include Reusable::AuthHelpers
Expand Down
14 changes: 14 additions & 0 deletions spec/reusable/heplers/email_helpers_spec.rb
@@ -0,0 +1,14 @@
require File.expand_path("../../../spec_helper", __FILE__)
require 'reusable/helpers/email_helpers'

describe Reusable::EmailHelpers do
describe "#anonimize_email" do
it "returns 3 first characters of username with dots if longer than 3 chars" do
subject.anonimize_email('marty@macfly.com').should == 'mar...@macfly.com'
end

it "returns first characted of username with dots if shorther than 3 chars" do
subject.anonimize_email('ba@barracus.com').should == 'b...@barracus.com'
end
end
end

0 comments on commit e92bfe2

Please sign in to comment.