Last active
August 29, 2015 13:57
-
-
Save crm114/9754676 to your computer and use it in GitHub Desktop.
MAAS API ruby client
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
# gem install httparty | |
require 'httparty' | |
module MAAS | |
class API | |
include HTTParty | |
base_uri 'http://marsweather.ingenology.com/v1' | |
def initialize(params = nil) | |
@params = params | |
@results = nil | |
end | |
def archive | |
if @params | |
@results = self.class.get('/archive', query: @params) | |
else | |
raise ArgumentError.new("You need to pass parameters to perform this query") | |
end | |
end | |
def next_page | |
if @results && @results["next"] | |
page_number = parse_page(@results["next"]) | |
@params[:page]= page_number | |
@results = self.class.get('/archive', query: @params) | |
else | |
perform_query_msg | |
end | |
end | |
def prev_page | |
if @results && @results["previous"] | |
page_number = parse_page(@results["previous"]) | |
@params[:page]= page_number | |
puts @params.inspect | |
@results = self.class.get('/archive', query: @params) | |
else | |
perform_query_msg | |
end | |
end | |
def latest | |
self.class.get('/latest') | |
end | |
private | |
def perform_query_msg | |
"Perform a query first" | |
end | |
def parse_page(page_link) | |
CGI::parse(URI.parse(page_link).query)["page"].first.to_i | |
end | |
end | |
end | |
# client = MAAS::API.new | |
# latest_results = client.latest | |
# archive_results = client.archive({max_temp: 8}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment