Skip to content

Instantly share code, notes, and snippets.

@joost
Created July 22, 2014 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joost/e32daf904ed8bd171974 to your computer and use it in GitHub Desktop.
Save joost/e32daf904ed8bd171974 to your computer and use it in GitHub Desktop.
Supersimple http://pixabay.com/api/docs/ Ruby API client.
require 'hashie'
require 'faraday'
require 'faraday_middleware'
# Supersimple http://pixabay.com/api/docs/ client.
# Usage:
# result = PixabayClient.new(username: 'someone', key: 'your_key').get(search_term: 'beach')
# result.body # Mashified results
# See: https://gist.github.com/joost/e32daf904ed8bd171974
class PixabayClient
def initialize(options = {})
@username = options[:username]
@key = options[:key]
end
API_URL = "http://pixabay.com/api/"
def connection
@connection ||= Faraday.new(url: API_URL, params: {username: @username, key: @key}) do |conn|
conn.adapter Faraday.default_adapter
conn.response :mashify
conn.response :json
end
end
def get(params = {})
connection.get('', params)
rescue Faraday::ParsingError
nil
end
end # PixabayClient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment