-
-
Save octplane/c05859ed765e2f37507c to your computer and use it in GitHub Desktop.
Supersimple http://pixabay.com/api/docs/ Ruby API 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
require 'hashie' | |
require 'faraday' | |
require 'faraday_middleware' | |
# Supersimple http://pixabay.com/api/docs/ client. | |
# Usage: | |
# result = PixabayClient.new(key: 'your_key').get(search_term: 'beach') | |
# result.body # Mashified results | |
# See: https://gist.github.com/joost/e32daf904ed8bd171974 | |
class PixabayClient | |
def initialize(options = {}) | |
@key = options[:key] | |
end | |
API_URL = "http://pixabay.com/api/" | |
def connection | |
@connection ||= Faraday.new(url: API_URL, params: {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