Last active
December 12, 2020 22:42
-
-
Save triangletodd/3f32609116bc663fd22df301c72b5840 to your computer and use it in GitHub Desktop.
Ruby SOMA.fm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
$:.push(File.expand_path('../', __FILE__)) | |
require 'soma_fm' | |
puts SomaFM.to_json | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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