1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

beatportのトラック検索APIを叩く

Last updated at Posted at 2012-12-10

beatportでトラック検索用のAPIを叩こうとしたらドキュメントがわかりづらかったのでrailsでやったときのメモ。

API叩くクラスをモデル的に作成

beatport.rb
require 'nokogiri'
require 'open-uri'

class Beatport 
  def self.search(query)
    url = BEATPORT_API_URL #'http://api.beatport.com/catalog/search?query='
    keywords = URI.encode(query)
    options = '&facets[]=fieldType:track&format=xml&v=2.0&page=0&perPage=3&sortBy=releaseDate%20desc' #option指定

    uri = URI(url + keywords + options)

    doc = Nokogiri::XML(uri.read)
    return doc 
  end
end

コントローラー側で呼び出す

test_controller.rb
class TestController < ApplicationController
  
  #===検索フォーム
  #
  #
  def form
  end
  
  #===表示
  #
  def show
    if params['search'].present?   
      #beatport
      @beatport = Beatport.search(params['search'])
    else
      @errors = 'error'
      render :action => :form
    end
  end
end

viewはaudioタグ使って表示。

show_html.erb抜粋
      <script type="text/javascript" src="http://api.html5media.info/1.1.4/html5media.min.js"></script>
  
      <% @beatport.search("document").xpath(".//track") .each do |doc| %>
        <audio src="<%= doc['url']  %>" preload="none" controls>Your browser can not open audio file. オーディオファイルを読み込めません</audio>
        <br />
      <% end %>
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?