나는 레일스 3 모델에서 nokogiri로 XML을 파싱하는 메소드를 가지고있다. 콘솔에서이 메서드를 호출하여 해당 메서드를 테스트하려면 어떻게해야합니까?콘솔에서 레일스 3 모델 메소드를 호출하는 방법
class Podcast < ActiveRecord::Base
validates_uniqueness_of :name
serialize :hosts
def generate_list
# fetch the top 300 podcasts from itunes
itunes_top_300 = Nokogiri.HTML(open("http://itunes.apple.com/us/rss/toppodcasts/limit=300/explicit=true/xml"))
# parse the returned xml
itunes_top_300.xpath('//feed/entry').map do |entry|
new_name = entry.xpath("./name").text
podcast = Podcast.find(:all, :conditions => {:name => new_name})
if podcast.nil?
podcast = Podcast.new(
:name => entry.xpath("./name").text,
:itunesurl => entry.xpath("./link/@href").text,
:category => entry.xpath("./category/@term").text,
:hosts => entry.xpath("./artist").text,
:description => entry.xpath("./summary").text,
:artwork => entry.xpath("./image[@height='170']").text
)
podcast.save
else
podcast.destroy
end
end
end
end
편집 : 와우, 조회수 1000
여기에 전체 클래스 (나는 generate_list를 호출하기 위해 노력하고있어)입니다. 이 질문이 사람들을 도왔을 때 도움이 되었기를 바랍니다. 1 년 전만해도 인스턴스 메소드와 클래스 메소드의 차이점을 이해할 수 없었습니다. 이제 복잡한 서비스 지향 응용 프로그램과 백엔드를 루비, 레일 및 기타 여러 언어/프레임 워크로 작성하고 있습니다. 스택 오버플로가 그 이유입니다. 사람들이 문제를 해결하고 해결책을 이해할 수있게 해 주신 것에 대해이 공동체에 많은 관심을 가져 주셔서 감사합니다.
Podcast.first.generate_list
당신은 다음과 같이 선언하여 클래스 메소드를해야한다 :
def self.generate_list
을 그리고 전화 :
이 메소드가 정의하는 방법을 우리에게 보여주십시오. –