2015-01-05 5 views
1

웹 사이트의 콘텐츠를 가져 와서 제출 양식에 입력하고 해당 정보를 json으로 저장하고 싶습니다. 내 db에 저장할 수 있습니다. HTTParty를 사용하려하지만 데이터를 가져 오는 방법을 구현하는 방법이 확실하지 않습니다. 여기까지 내가 지금까지 가지고있는 것이있다.httparty를 사용하여 웹 사이트에서 콘텐츠를 얻으려면 어떻게해야합니까?

내 controller.rb입니다 컨트롤러

class UrlsController < ApplicationController 
    before_action :set_url, only: [:show, :edit, :update, :destroy] 
    #require "addressable/uri" 
    #Addressable::URI.parse(url) 

    # GET /urls 
    # GET /urls.json 
    def index 
    @urls = Url.all 
    end 

    # GET /urls/1 
    # GET /urls/1.json 
    def show 
    end 

    # GET /urls/new 
    def new 
    @url = Url.new 
    end 

    # GET /urls/1/edit 
    def edit 
    end 

    def uri?(string) 
    uri = URI.parse(string) 
    %w(http https).include?(uri.scheme) 
    rescue URI::BadURIError 
     false 
    rescue URI::InvalidURIError 
     false 
    end 

    # POST /urls 
    # POST /urls.json 
    def create 
    @url = Url.new(url_params) 
    @app_url = params[:url] 

    respond_to do |format| 
     if @url.save 
     format.html { redirect_to @url, notice: 'Url was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @url } 
     wordcount 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @url.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def wordcount 
    # Choose the URL to visit 
    @app_url = @url 

    @words = HTTParty.get(@app_url) 

    # Trick to pretty print headers 
    @wordcount = Hash[*@words] 
    end 

    # PATCH/PUT /urls/1 
    # PATCH/PUT /urls/1.json 
    def update 
    respond_to do |format| 
     if @url.update(url_params) 
     format.html { redirect_to @url, notice: 'Url was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @url.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /urls/1 
    # DELETE /urls/1.json 
    def destroy 
    @url.destroy 
    respond_to do |format| 
     format.html { redirect_to urls_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_url 
     @url = Url.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def url_params 
     params.require(:url).permit(:url) 
    end 
end 

. 줄에서나쁜 인수 (예상 URI 개체 또는 URI 문자열) '을 받고 있어요 유효한 URL 폼에 URL을 넣고 URL에서 원하는 콘텐츠를 가져 와서 그 정보를 저장해야합니다. 이 같은

+0

귀하의 요점은, 왜 스택에 코드를 넣지 유효하지 않은 것 같다? – neo

+0

왜 데이터를 JSON에 저장 한 다음 데이터베이스에 저장해야하나요? 따라서 데이터를 다시 사용하기가 어렵습니다. 데이터에서 JSON을 쉽게 만들 수 있습니다. –

+0

나중에 입력하고 볼 수있는 각 사이트에서 단어 목록을 가져오고 싶습니다. 가장 좋은 방법이 아닌 해시에 저장하고 있습니까? – user3443619

답변

0

시도 뭔가 :

response = HTTParty.get('https://google.com') 

puts response.body, response.code, response.message, response.headers.inspect 

귀하의 질문에 당신은 다음과 같은 방법을 구현하는 새로운 클래스를 만들거나 도우미에 넣을 수있는 답변을.

include HTTParty

def url_getter(url) 
    HTTParty.get(url) 
end 

필요하고 그것을 호출 할 수 있습니다 :

url_getter(@app_url) 
+0

그래서 매번 다른 URL을 어떻게 구현합니까? 응답 = HTTParty.get (@app_url) ?? – user3443619

+0

수정 사항을 보려면 @app_url이 문자열이고 전체 URL이 포함되어 있는지 확인하십시오. – neo

+0

양식 입력에서 URL을 가져 와서 여기에 연결하는 방법을 알아낼 수 없습니다. '정의되지 않은 메소드'url'for ' – user3443619

관련 문제