gouf
7/24/2016 - 6:17 PM

Mechanize による診断メーカーの情報取得とオストンインゲーム例

Mechanize による診断メーカーの情報取得とオストンインゲーム例

require 'mechanize'

class ShindanScrape
  SHINDAN_URL = 'https://shindanmaker.com'.freeze

  def initialize(shindan_id, user_name)
    @user_name = user_name
    agent = Mechanize.new
    @page = agent.get("#{SHINDAN_URL}/#{shindan_id}")
  end

  def shindan
    shindan_result(filled_form)
  end

  private

  def filled_form
    form = @page.form_with(id: 'form')
    form.field_with(name: 'u') do |input|
      input.value = @user_name
    end
    form
  end

  def shindan_result(filled_form)
    page = filled_form.submit
    page.css('#copy_text_140').text
  end
end
require_relative 'shindan_scrape'

module Ofuton
  def self.in(user_name, hide_url: true)
    result = ShindanScrape.new(362_791, user_name).shindan
    if hide_url
      cutoff_last_url(result)
    else
      result
    end
  end

  def self.cutoff_last_url(result)
    result.lines.first(result.lines.size - 1).join
  end
end

puts Ofuton.in('gouf')
# =>
# [____]  (:3 っ)=≡=-・∴
# goufのオストンチャレンジ!
# ↓
# [____]  (:3 っ) (失敗:-122cm)床で寝てください
# https://shindanmaker.com/362791