从rails应用程序在heroku上使用api时出现问题

时间:2015-11-24 18:00:46

标签: ruby-on-rails api heroku

对于学校的项目我正在尝试使用Bandsintown api来显示用户在我的网站上关注的艺术家即将举行的音乐会。我能够以我想要的方式使用api开发,但是当涉及我的生产中的heroku网站时,没有显示任何错误,实际上我只收到一个空数组。也许你需要Bandsintown的许可才能在heroku上使用他们的api?我试过联系他们但还没有听到回复。

我的问题是,为什么我能够从Bandsintown获得开发但不是生产中的响应?一般来说是网络开发的新手,所以我以前没有碰到过这个......

这是我的代码:

calendar_controller.rb

class CalendarController < ApplicationController
  require 'uri'

  def index
    @user = current_user
    @hash_version_array = []

    @user.follows.each do |follow|
      response = HTTParty.get("http://api.bandsintown.com/artists/#{URI.escape(follow.artist_name)}/events/search.json?api_version=2.0&app_id=#{app_id}&location=use_geoip")
      @hash_version = JSON.parse(response.body)

      @hash_version_array << @hash_version if @hash_version != []
    end

    @hash_version_array
  end
end

日历/ index.html.erb

<h1><%= @user.name %>'s Calendar</h1>
<div>
  <h4 class="inline"><%= link_to "Calendar" %></h4>
  <h4 class="inline"><%= link_to "List" %></h4>
  <h4 class="inline"><%= link_to "Map" %></h4>
</div>
<br>

<% @hash_version_array.each do |date| %>
  <h3 class="artist_name"><%= date[0]["formatted_datetime"] %></h3>
  <% date[0]["artists"].each do |info| %>
    <h1 class="artist_name"><%= info["name"] %></h1>
    <br>
    <%= image_tag("#{info['thumb_url']}", class: 'artist_img') %>
  <% end %>

  <br>
  <br>

  <%= date[0]["venue"]["name"] %>
  <br>
  <%= date[0]["formatted_location"] %>, <%= date[0]["venue"]["country"] %>
  <br>
  Tickets <%= date[0]["ticket_status"].titleize %> <%= link_to("Buy", "#{date[0]['ticket_url']}") %>
  <br>
  <hr class="artists_hr">
<% end %>

Bandsintown在开发过程中的响应:

From: /Users/jasonquaccia/Sites/code/projects/capstone/the-music-project/app/controllers/calendar_controller.rb @ line 15 CalendarController#index:

     4: def index
     5:   @user = current_user
     6:   @hash_version_array = []
     7: 
     8:   @user.follows.each do |follow|
     9:     response = HTTParty.get("http://api.bandsintown.com/artists/#{URI.escape(follow.artist_name)}/events/search.json?api_version=2.0&app_id=el_proyecto_de_la_musica&location=use_geoip")
    10:     @hash_version = JSON.parse(response.body)
    11: 
    12:     @hash_version_array << @hash_version if @hash_version != []
    13:   end
    14: 
 => 15:   binding.pry
    16:   @hash_version_array
    17: end

[1] pry(#<CalendarController>)> @hash_version_array
=> [[{"id"=>10847723,
   "title"=>"Metallica @ AT & T Park in San Francisco, CA",
   "datetime"=>"2016-02-06T19:00:00",
   "formatted_datetime"=>"Saturday, February 6, 2016 at 7:00PM",
   "formatted_location"=>"San Francisco, CA",
   "ticket_url"=>"http://www.bandsintown.com/event/10847723/buy_tickets?app_id=el_proyecto_de_la_musica&artist=Metallica&came_from=67",
   "ticket_type"=>"Tickets",
   "ticket_status"=>"available",
   "on_sale_datetime"=>"2015-11-06T10:00:00",
   "facebook_rsvp_url"=>"http://www.bandsintown.com/event/10847723?app_id=el_proyecto_de_la_musica&artist=Metallica&came_from=67",
   "description"=>"CBS RADIO&#39;s The Night Before with Metallica and Cage the Elephant",
   "artists"=>
    [{"name"=>"Metallica",
      "mbid"=>"65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab",
      "image_url"=>"https://s3.amazonaws.com/bit-photos/large/4304043.jpeg",
      "thumb_url"=>"https://s3.amazonaws.com/bit-photos/thumb/4304043.jpeg",
      "facebook_tour_dates_url"=>"http://www.bandsintown.com/Metallica/facebookapp?came_from=67",
      "facebook_page_url"=>nil,
      "tracker_count"=>2341049,
      "url"=>"Metallica",
      "website"=>"http://metallica.com"}],
   "venue"=>{"name"=>"AT & T Park", "city"=>"San Francisco",     "region"=>"CA", "country"=>"United States", "latitude"=>37.7784059, "longitude"=>-122.3894401}}]]

1 个答案:

答案 0 :(得分:0)

找出为什么我在Heroku上制作时没有得到任何东西,与location=use_geoip在这一行上有关:

response = HTTParty.get("http://api.bandsintown.com/artists/#{URI.escape(follow.artist_name)}/events/search.json?api_version=2.0&app_id=#{app_id}&location=use_geoip")

这适用于本地,因为它可以引用我的位置,然后搜索Bandsintown以获取正确的信息。但是,在Heroku上,它不知道如何正确引用位置。因此,我没有收到任何结果。