Skip to content

Instantly share code, notes, and snippets.

@derwiki
Created August 14, 2014 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derwiki/8e6e0a57309c86f85ab5 to your computer and use it in GitHub Desktop.
Save derwiki/8e6e0a57309c86f85ab5 to your computer and use it in GitHub Desktop.
Example thin JSON API client in Ruby, with one second global timeouts
require 'httparty'
class PixelPeeper
include HTTParty
base_uri 'www.pixel-peeper.com'
default_timeout 1 # hard timeout after 1 second
def api_key
ENV['PIXELPEEPER_API_KEY']
end
def base_path
"/rest/?method=list_photos&api_key=#{ api_key }"
end
def handle_timeouts
begin
yield
rescue Net::OpenTimeout, Net::ReadTimeout
{}
end
end
def examples_for_camera(camera_id, options = {})
handle_timeouts do
url = "#{ base_path }&camera=#{ camera_id }"
self.class.get(url, options)['data']['results']
end
end
def examples_for_lens(lens_id, options = {})
handle_timeouts do
url = "#{ base_path }&lens=#{ lens_id }"
self.class.get(url, options)['data']['results']
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment