Skip to content

Instantly share code, notes, and snippets.

@joost
Created June 25, 2014 14:36
Show Gist options
  • Save joost/4fe9f70b0de4bc97d40f to your computer and use it in GitHub Desktop.
Save joost/4fe9f70b0de4bc97d40f to your computer and use it in GitHub Desktop.
Super simple Ruby Icons8 API (http://api.icons8.com/) client
require 'hashie'
require 'faraday'
require 'faraday_middleware'
# Supersimple http://api.icons8.com/ client.
# Usage:
# Icons8Client.new.search('icon')
# See: https://gist.github.com/joost/e149404f645f33ba1939
class Icons8Client
def initialize(options = {})
@platform = options[:platform] # Can be 'win8', 'ios7', 'android'
end
API_URL = "http://api.icons8.com/api/iconsets/"
def connection
@connection ||= Faraday.new(url: API_URL, params: {platform: @platform}.delete_if {|k,v| v.blank?}) do |conn|
conn.adapter Faraday.default_adapter
conn.response :mashify
conn.response :xml
end
end
def get(path, params = {})
connection.get(path, params)
rescue Faraday::ParsingError
nil
end
# Returns Array of icon Hashie's.
def search(term)
response = get('search', term: term)
result = response.body.icons8.result.search.icon
return [] if result.nil?
return result.is_a?(Array) ? result : [result]
end
end # Icons8Client
@pvolyntsev
Copy link

Good one! Where do you use it?

@pvolyntsev
Copy link

It seems to me your code doesn't work

def search(term)
      response = get('https://api.icons8.com/api/iconsets/search', term: term) # should use HTTPS and full URL maybe
      ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment