Class: NounProjectApi::IconsRetriever

Inherits:
Retriever
  • Object
show all
Defined in:
lib/noun-project-api/icons_retriever.rb

Overview

Retrieve icons.

Constant Summary collapse

API_PATH =
"/icons/".freeze

Instance Attribute Summary

Attributes included from Connection

#access_token, #secret, #token

Instance Method Summary collapse

Methods included from Connection

#initialize

Instance Method Details

#find(term, limit = nil, offset = nil, page = nil) ⇒ Object

Finds multiple icons based on the term

  • term - search term

  • limit - limit the amount of results

  • offset - offset the results

  • page - page number



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/noun-project-api/icons_retriever.rb', line 13

def find(term, limit = nil, offset = nil, page = nil)
  fail(ArgumentError, "Missing search term") unless term

  search = OAuth::Helper.escape(term)
  search += "?limit_to_public_domain=#{NounProjectApi.configuration.public_domain ? 1 : 0}"

  args = {
    "limit" => limit,
    "offset" => offset,
    "page" => page
  }.reject { |_, v| v.nil? }
  args.each { |k, v| search += "&#{k}=#{v}" } if args.size > 0

  result = access_token.get("#{API_BASE}#{API_PATH}#{search}")
  fail(ArgumentError, "Bad request") unless %w(200 404).include? result.code

  if result.code == "200"
    JSON.parse(result.body)["icons"].map { |icon| Icon.new(icon) }
  else
    []
  end
end

#recent_uploads(limit = nil, offset = nil, page = nil) ⇒ Object

List recent uploads

  • limit - limit the amount of results

  • offset - offset the results

  • page - page number



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/noun-project-api/icons_retriever.rb', line 40

def recent_uploads(limit = nil, offset = nil, page = nil)
  args = {
    "limit" => limit,
    "offset" => offset,
    "page" => page
  }.reject { |_, v| v.nil? }
  if args.size > 0
    search = "?"
    args.each { |k, v| search += "#{k}=#{v}&" }
  else
    search = ""
  end

  result = access_token.get("#{API_BASE}#{API_PATH}recent_uploads#{search}")
  fail(ArgumentError, "Bad request") unless result.code == "200"

  JSON.parse(result.body)["recent_uploads"].map { |icon| Icon.new(icon) }
end