0

我正在尝试使用 homeaway API 在 html 页面上显示一些数据。

我的 search_results 文件看起来像这个 priceRanges=[#, #, #] 而不是我想要的 3 个价格。

我在网上查看了转换为整数的方法,但这无济于事。我只是想知道这个数字是否已从服务器中扣留/我的堆栈/代码有问题,因为老实说我已经搜索了几个小时但一无所获。

这是我的代码

搜索结果

<html>
  <head>
    <title>Search results for <%= "#{session[:me].first_name} #{session[:me].last_name}" %></title>
  </head>
  <body>
    <h3>Search results for <%= "#{session[:me].first_name} #  {session[:me].last_name}" %></h3>
    <br/>

    <%   end %></p>
    <p>Here are your search results:
      <ul>
        <% @search_results.each do |result| %>
          <li>
            <% if result.has?(:thumbnail) %>
              <img src="<%= result.thumbnail.secureUri %>"/>
            <% end %>
            <p><%= result %></p>
            <p>Price Range: <% a = result.priceRanges.to_a
              b = a.at(1).to_i
            %></p>
            <p><%= "#{b}" %>
            <p>Bedrooms: <%= result.bedrooms %></p>
            <p>Bathrooms: <%= result.bathrooms %></p>
            <p>Amount: <% puts result.amount %> <% result.currency %></p>
            <a href="/listing/<%= result.listingId %>"><%= result.headline %></a>
            <a href="<%=result.listingUrl %>">View on Homeaway</a>
            <p><%= result.Latitude %></p>
            <p><%= result.displayLongitude %></p>    </li>
        <% end %>
      </ul>

      <br/><br/>
      <a href="/">go back</a>
    </body>
  </html>

后端的另一个文件

require 'homeaway_api'
require 'sinatra/base'
require 'webrick'
require 'webrick/https'
require 'openssl'
require 'pp'

class HomeAwayApiTutorial < Sinatra::Base
  def create_client
    client_id = 'personal id'
    client_secret = 'my secret'
    HomeAway::API::Client.new :client_id => client_id, :client_secret =>     client_secret
  end

  enable :sessions

  get '/' do
    code = params[:code]

    if session[:client].nil? && (code.nil? || code.empty?)
      client = create_client
      session[:client] = client

      redirect to(client.auth_url)
    else
      client = session[:client]
      client.oauth_code = code if code
      session[:me] = client.me
      @me = session[:me]

      erb :index
    end
  end

  get '/search' do
    search_query = params[:q]
    redirect to ('/') if session[:client].nil? || search_query.nil?

    client = session[:client]
    @search_results = client.search(q: search_query)

    erb :search_results
  end

  get '/listing/:id' do |id|
    redirect to ('/') if session[:client].nil? || id.nil? || id.empty?

    client = session[:client]
    @listing = client.listing(id, %w(AVAILABILITY DETAILS LOCATION PHOTOS RATES REVIEWS))

    erb :listing_details
  end

  get '/quote' do
    search_query = params[:q]
    redirect to ('/') if session[:client].nil? || search_query.nil?

    client = session[:client]
    listing_id = params[:listing_id]
    adults_count = params[:adults_count]
    start_date = params[:start_date]
    end_date = params[:end_date]
    listing = client.listing(listing_id)
    unit_id = listing.units.first.unitNumber

    erb :quote_results
  end

  get '/bookIt' do
    redirect to ('/') if session[:client].nil?

    client = session[:client]
    listing_id = params[:listing_id]
    priceRanges = params[:priceRanges]
    displayLatitude = params[:displayLatitude]
    displayLongitude = params[:displayLongitude]
    adults_count = params[:adults_count]
    start_date = params[:start_date]
    end_date = params[:end_date]
    listing = client.listing(listing_id)
    unit_id = listing.units.first.unitNumber
    book_stay = client.book_stay(listing_id, unit_id, adults_count.to_i, start_date, end_date)

    halt 404, "property not available to book" unless book_stay.booking_url
    redirect to(book_stay.booking_url)
  end

  get '/writeReview/:listing_id' do |listing_id|
    redirect to ('/') if session[:client].nil?

    client = session[:client]
    @listing = client.listing(listing_id, 'DETAILS')

    erb :write_review
  end

  post '/submitReview' do
    redirect to ('/') if session[:client].nil?

    client = session[:client]
    listing_id = params[:listing_id]
    arrival_date = params[:arrival_date]
    rating = params[:rating]
    headline = params[:headline]
    body = params[:body]
    listing = client.listing(listing_id)
    unit_id = listing.units.first.unitNumber

    client.submit_review(headline, body, 'en', arrival_date, rating, listing_id, unit_id)

    redirect to ("/listing/#{listing_id}")
  end
end

webrick_options = {
 :Port => 4567,
 :BindAddress => '127.0.0.1',
 :Logger => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG),
 :DocumentRoot => "/ruby/htdocs",
 :SSLEnable => true,
 :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
 :SSLCertificate => OpenSSL::X509::Certificate.new(File.open('localhost.crt').read),
 :SSLPrivateKey => OpenSSL::PKey::RSA.new(File.open('localhost.key').read),
 :SSLCertName => [["CN", WEBrick::Utils::getservername]],
 :app => HomeAwayApiTutorial
}

Rack::Server.start webrick_options

我们刚刚添加的第一个处理程序将服务 req

4

0 回答 0