Skip to content

Instantly share code, notes, and snippets.

@triangletodd
Last active December 12, 2020 22:42
Show Gist options
  • Save triangletodd/3f32609116bc663fd22df301c72b5840 to your computer and use it in GitHub Desktop.
Save triangletodd/3f32609116bc663fd22df301c72b5840 to your computer and use it in GitHub Desktop.
Ruby SOMA.fm
#!/usr/bin/env ruby
$:.push(File.expand_path('../', __FILE__))
require 'soma_fm'
puts SomaFM.to_json
require 'json'
require 'yaml'
require 'net/http'
require 'nokogiri'
class SomaFM
BASE_URL='http://somafm.com'
API_URL='http://api.somafm.com/channels.json'
API_URI=URI.parse(API_URL)
## Class methods
def self.to_h
new.to_h
end
def self.to_yaml
new.to_yaml
end
def self.to_json
new.to_json
end
## Instance methods
def initialize
@data = document
end
def to_h
@data
end
def to_json
@data.to_json
end
def to_yaml
@data.to_yaml
end
def fetch!
@data = document
end
private
def response
Net::HTTP.get_response(API_URI)
end
def document
JSON.parse(response.body)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment