module Twimock::Config

Constants

AVAILABLE_TYPE

Public Instance Methods

database() click to toggle source
# File lib/twimock/config.rb, line 23
def database
  default_database
end
default_database() click to toggle source
# File lib/twimock/config.rb, line 19
def default_database
  Twimock::Database.new
end
load_users(ymlfile) click to toggle source
# File lib/twimock/config.rb, line 33
def load_users(ymlfile)
  load_data = YAML.load_file(ymlfile)
  raise Twimock::Errors::IncorrectDataFormat.new "data is not Array" unless load_data.kind_of?(Array)

  load_data.each do |app_data|
    data = Hashie::Mash.new(app_data)
    app_id     = data.id
    api_key    = data.api_key
    api_secret = data.api_secret
    users      = data.users

    # Validate data format
    [:id, :api_key, :api_secret, :users].each {|key| validate_format(key, data.send(key)) }
    users.each do |user|
      [:id, :name, :password].each {|key| validate_format(key, user.send(key)) }
    end

    # Create application and user record
    app = Twimock::Application.create!({ id: app_id, api_key: api_key, api_secret: api_secret })
    users.each do |options|
      access_token = AccessToken.new
      access_token.string = options.delete(:access_token)
      access_token.secret = options.delete(:access_token_secret)
      user = Twimock::User.new(options)
      user.save! unless Twimock::User.find_by_id(user.id)
      unless Twimock::AccessToken.find_by_string(access_token.string)
        access_token.user_id = user.id
        access_token.application_id = app_id
        access_token.save!
      end
    end
  end
end
reset_database() click to toggle source
# File lib/twimock/config.rb, line 27
def reset_database
  db = Twimock::Database.new
  db.disconnect!
  db.drop
end

Private Instance Methods

available?(key, value) click to toggle source
# File lib/twimock/config.rb, line 76
def available?(key, value)
  return false unless AVAILABLE_TYPE[key].any? { |t| value.kind_of?(t) }
  case value
  when String, Array
     value.present?
  when Integer
     value >= 0
  end
end
validate_format(key, value) click to toggle source
# File lib/twimock/config.rb, line 86
def validate_format(key, value)
  raise Twimock::Errors::IncorrectDataFormat.new "format of #{key} is incorrect" unless available?(key, value)
end