require "rosie_agenda/version"
require 'mechanize'

URL = "http://www.watertownsaturdaytoastmasters.org/index.cgi"

module RosieAgenda
  class User
    attr_reader :user_name

    def initialize(user_name, password)
      @user_name = user_name
      @password = password
      @logged_in = false
      @fth = RosieAgenda::FTH.new
    end

    def login
      return if @logged_in
      @fth.authenticate_user(self)
    end

  end

  class FTH
    def initialize
      @fth = Mechanize.new
    end

    def authenticate_user(user)
      id = member_id(user.user_name)
      true
    end

    private

    def member_id(name)
      page = @fth.get URL, action: "membersearch", term: name
      response = JSON.parse(page.content)[0]
      id = response["value"].match(/«(\d*)»/)[1]
      id || (raise "Unable to find member id in /#{response['value']}/")
    end
  end
end

require "rosie_agenda/version"
require 'mechanize'

URL = "http://www.watertownsaturdaytoastmasters.org/index.cgi"

module RosieAgenda
  class User
    attr_reader :user_name

    def initialize(user_name, password)
      @user_name = user_name
      @password = password
      @logged_in = false
      @fth = RosieAgenda::FTH.new
    end

    def login
      return if @logged_in
      @fth.authenticate_user(self)
    end

  end

  class FTH
    def initialize
      @fth = Mechanize.new
    end

    def authenticate_user(user)
      id = member_id(user.user_name)
      true
    end

    private

    def member_id(name)
      page = @fth.get URL, action: "membersearch", term: name
      # This parse could be avoided (since we're matching on unique chevrons)
      # but #parse produces a UTF-8 string which matches the UTF-8 regex.
      response = JSON.parse(page.content)[0]
      id = response["value"].match(/«(\d*)»/)[1]
      id || (raise "Unable to find member id in /#{response['value']}/")
    end
  end
end

Document the reason for JSON parsing.
Document the reason for JSON parsing.

The page.content could be directly matched via the unique
chevrons, but the content is ASCII-8 and the regex is UTF-8
(Ruby default now). I jiggled both sides and never found a simpler
way of getting the encoding to match. So I’m leaving the 
JSON parse.