module Jafry::Utilator

Bunch of functions to work with encode/decode JSON and files @since 0.2.0

Private Instance Methods

build_schemes_path(scheme) click to toggle source

Method for creating full path to scheme file

@private @param scheme [String] Scheme name @return [String] Full path to file

# File lib/jafry/utilator.rb, line 15
def build_schemes_path(scheme)
  "#{Jafry.schemes_path}/#{scheme}.json".gsub(/\/{2,}/, '/')
end
encode_json(hash) click to toggle source

Encodes JSON

@private @param [Hash] hash Hash to convert to JSON @return [String] Parsed json

# File lib/jafry/utilator.rb, line 54
def encode_json(hash)
  Yajl::Encoder.encode hash
end
not_exists_message() click to toggle source

Specify message will be shown when scheme file not exist

@private @return [String] Message

# File lib/jafry/utilator.rb, line 34
def not_exists_message
  "Please, create correct scheme file first"
end
parse_json(json) click to toggle source

Parses JSON

@private @param [String] json JSON to parse @return [Hash] Parsed json

# File lib/jafry/utilator.rb, line 44
def parse_json(json)
  Yajl::Parser.new.parse(json)
end
parse_scheme(file_name) click to toggle source

Creates Hash from JSON-based scheme file

@private @param [String] file_name Scheme file name @return [Hash] Parsed scheme

# File lib/jafry/utilator.rb, line 64
def parse_scheme(file_name)
  begin
    raise not_exists_message unless scheme_file_exists?(file_name)
    parse_json(File.read(build_schemes_path(file_name)))
  rescue Exception => e
    puts e.message
    puts e.backtrace
  end
end
scheme_file_exists?(scheme_name) click to toggle source

Method for check scheme file existance

@private @param scheme_name [String] Scheme name @return [Boolean] True or false

# File lib/jafry/utilator.rb, line 25
def scheme_file_exists?(scheme_name)
  File.exists?(build_schemes_path(scheme_name))
end
wrap(wrapper, hash) click to toggle source

Wraps hash in specified wrapper

@private @param [String] wrapper Namespace hash will be wrapped in @param [Hash] hash Hash to wrap @return [Hash] Wrapped hash

@example Wrap

wrap('baz', {foo: 'bar'}) #=> {'baz' => {foo: 'bar'}}
# File lib/jafry/utilator.rb, line 84
def wrap(wrapper, hash)
  {"#{wrapper}" => hash}
end