Sha256: 4f471cce34260ffef74c394fa16320a1a118e690f8cf934d2a0a6f5631d6b625

Contents?: true

Size: 1.77 KB

Versions: 7

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module Meroku
  # Logic related to Users login, logout etc
  module User
    extend Meroku::ResponseHandler

    def self.login(email, password)
      data = { 'email' => email, 'password' => password }.to_json
      result = RestClient.post 'https://www.meroku.com/login.json',
                               data,
                               content_type: :json
      process_error(result)
      process_success(result) { |x| save_token(x) }
    end

    def self.save_token(x)
      token = JSON.parse(x).dig('data', 'token')
      IO.write("#{Dir.home}/.meroku_token", token) if token
    end

    def self.logout
      file = "#{Dir.home}/.meroku_token"
      File.delete(file)
      puts 'Logged out'
      raise Meroku::Success
    end

    def self.signup(email, password)
      data = { 'email' => email, 'password' => password }.to_json
      result = RestClient.post 'https://www.meroku.com/registrations.json',
                               data,
                               content_type: :json
      process_error(result)
      process_success(result) { |x| save_token(x) }
    end

    def self.unregister(options)
      data = { 'email' => options[:email], 'password' => options[:password] }
      result = RestClient::Request.execute(
        method: :delete,
        url: 'https://www.meroku.com/registrations.json',
        headers: { params: data }
      )
      process_error(result)
      process_success(result) { delete_token }
    end

    def self.delete_token
      file = "#{Dir.home}/.meroku_token"
      File.delete(file) if File.exist?(file)
    end

    def saved_token
      token_path = Dir.home + '/.meroku_token'
      IO.read(token_path)
    rescue StandardError => e
      raise Meroku::Error, "Login failed #{e.class}"
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
meroku-2.0.31 lib/meroku/user.rb
meroku-2.0.30 lib/meroku/user.rb
meroku-2.0.29 lib/meroku/user.rb
meroku-2.0.28 lib/meroku/user.rb
meroku-2.0.27 lib/meroku/user.rb
meroku-2.0.26 lib/meroku/user.rb
meroku-2.0.25 lib/meroku/user.rb