0

I'm trying to use the homeaway API to display some data on an html page.

my search_results file looks like this priceRanges=[#, #, #] as opposed to 3 prices that I want.

I've looked online in terms of convert to integer, but that does nothing. I'm just wondering if this number has been withheld from the server/there is something wrong with my stack/code, as I honestly have searched for a couple of hours and found nothing.

This is my code

search_results

<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>

The other file that is on the back end

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

The first handler we just added will service req

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
EntropicFox
  • 727
  • 5
  • 11
  • What is the value of `priceRanges`? – Jordan Running Mar 23 '16 at 16:12
  • I don't know, the API documentation says its a list not a string. But I'm not sure what to do, I searched convert list to string ruby and found nothing. What do you think? – EntropicFox Mar 24 '16 at 11:24
  • 1
    Try doing something like `
    <%= result.priceRanges.pretty_inspect %>
    `. It should print a more readable representation of the object. Then come back and edit your question to include what's printed.
    – Jordan Running Mar 24 '16 at 14:06
  • cool I get something like [{"currencyUnits"=>"GBP", "from"=>130.0, "periodType"=>"NIGHTLY-WEEKDAY", "to"=>130.0}, {"currencyUnits"=>"GBP", "from"=>800.0, "periodType"=>"WEEKLY", "to"=>800.0}, {"currencyUnits"=>"GBP", "from"=>2499.9866, "periodType"=>"MONTHLY", "to"=>2499.9866}] How would I select the first (from) element – EntropicFox Mar 24 '16 at 17:39

0 Answers0