Sha256: 27846784237e5050bc0722b59c7671e897c8077db61b5cd6f1d60491fab17f5e

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require "uri"
require "json"

module SyoboiCalendar
  class Agent
    BASE_URL   = "http://cal.syoboi.jp"
    SEARCH_URL = BASE_URL + "/find"
    JSON_URL   = BASE_URL + "/json.php"
    LOGIN_URL  = BASE_URL + "/usr"

    # login is option to search with user channel setting
    def initialize(opts = {})
      if opts[:user] && opts[:pass]
        login(opts[:user], opts[:pass])
      end
    end

    def login(user, pass)
      page = get LOGIN_URL
      form = page.forms[1]
      form.usr  = user
      form.pass = pass
      mechanize.submit(form)
      @logined = true
    end

    def login?
      !!@logined
    end

    def json(query)
      page = get JSON_URL, query
      JSON.parse(page.content)
    end

    def search(query)
      get SEARCH_URL, query
    end

    private

    def get(url, hash = {})
      mechanize.get(url + querinize(hash))
    end

    # change hash into URL query string
    def querinize(hash)
      "?" + hash.map { |k, v| "#{k}=#{URI.encode(v.to_s)}" }.join("&")
    end

    def mechanize
      @mechanize ||= Mechanize.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
syoboi_calendar-0.0.1 lib/syoboi_calendar/agent.rb