noun-project-api 0.2.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/noun-project-api.rb +10 -8
- data/lib/noun-project-api/base_item.rb +26 -0
- data/lib/noun-project-api/collection.rb +34 -0
- data/lib/noun-project-api/collection_retriever.rb +12 -0
- data/lib/noun-project-api/connection.rb +1 -1
- data/lib/noun-project-api/icon.rb +8 -21
- data/lib/noun-project-api/icon_retriever.rb +4 -12
- data/lib/noun-project-api/icons_retriever.rb +20 -12
- data/lib/noun-project-api/reporter.rb +5 -5
- data/lib/noun-project-api/retriever.rb +10 -0
- data/spec/lib/noun-project-api/base_item_spec.rb +11 -0
- data/spec/lib/noun-project-api/collection_retriever_spec.rb +85 -0
- data/spec/lib/noun-project-api/collection_spec.rb +64 -0
- data/spec/lib/noun-project-api/icon_retriever_spec.rb +12 -12
- data/spec/lib/noun-project-api/icon_spec.rb +16 -16
- data/spec/lib/noun-project-api/icons_retriever_spec.rb +19 -19
- data/spec/lib/noun-project-api/reporter_spec.rb +16 -16
- data/spec/lib/noun-project-api/retriever_spec.rb +4 -4
- data/spec/lib/nount_project_api_spec.rb +3 -3
- data/spec/support/fakes.rb +81 -0
- metadata +12 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31efd2c37c5ac37ddb6ac38054485ab56a13fb65
|
4
|
+
data.tar.gz: e2de0f1c8e3e52d09ad8a57d4854fa1f66eeb121
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2aae5a8fa9d877dcdaa83cea76a7de58ec4a777671522e1600d89ea0808582471d51f8f360dc7ddbe4ab99baf9a52972156977a79434e63a961008483ff03593
|
7
|
+
data.tar.gz: a3f9934c896120832073dfbe595aab89fd6edc7de5c5eb5a5222ec9fa192fdd8bb6a28400763a1a3117e8f862eaba446b7c3daebd35dc8257ca9a37b5db5c134
|
data/lib/noun-project-api.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
1
|
+
require "oauth"
|
2
|
+
require "json"
|
3
|
+
require "noun-project-api/connection"
|
4
|
+
require "noun-project-api/icon_retriever"
|
5
|
+
require "noun-project-api/reporter"
|
6
|
+
require "noun-project-api/icons_retriever"
|
7
|
+
require "noun-project-api/icon"
|
8
|
+
require "noun-project-api/collection_retriever"
|
9
|
+
require "noun-project-api/collection"
|
8
10
|
|
9
11
|
# Top level name space for the entire Gem.
|
10
12
|
module NounProjectApi
|
11
|
-
API_BASE =
|
13
|
+
API_BASE = "http://api.thenounproject.com".freeze
|
12
14
|
|
13
15
|
def self.configuration
|
14
16
|
@configuration ||= Configuration.new
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module NounProjectApi
|
2
|
+
# A basis to Items returned by the noun project.
|
3
|
+
class BaseItem
|
4
|
+
attr_accessor :original_hash
|
5
|
+
ITEM_NAME = nil
|
6
|
+
|
7
|
+
def initialize(origin)
|
8
|
+
fail(NotImplementedError, "Must use a subclass") if self.class::ITEM_NAME.nil?
|
9
|
+
|
10
|
+
origin = JSON.parse(origin) if origin.is_a? String
|
11
|
+
origin = origin.delete(
|
12
|
+
self.class::ITEM_NAME
|
13
|
+
) if origin.key? self.class::ITEM_NAME
|
14
|
+
|
15
|
+
@original_hash = origin
|
16
|
+
end
|
17
|
+
|
18
|
+
def id
|
19
|
+
original_hash["id"].to_i
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_json
|
23
|
+
JSON.dump(to_hash)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "noun-project-api/base_item"
|
2
|
+
|
3
|
+
module NounProjectApi
|
4
|
+
# A single Collection as an abstracted ruby object.
|
5
|
+
class Collection < BaseItem
|
6
|
+
ITEM_NAME = "collection".freeze
|
7
|
+
|
8
|
+
def author_id
|
9
|
+
original_hash["author_id"].to_i
|
10
|
+
end
|
11
|
+
|
12
|
+
def author_name
|
13
|
+
original_hash["author"]["name"]
|
14
|
+
end
|
15
|
+
|
16
|
+
def icon_count
|
17
|
+
original_hash["icon_count"].to_i
|
18
|
+
end
|
19
|
+
|
20
|
+
def is_published?
|
21
|
+
original_hash["is_published"].to_i == 1
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_hash
|
25
|
+
{
|
26
|
+
id: id,
|
27
|
+
author_id: author_id,
|
28
|
+
author_name: author_name,
|
29
|
+
icon_count: icon_count,
|
30
|
+
is_published: is_published?
|
31
|
+
}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "noun-project-api/collection"
|
2
|
+
require "noun-project-api/retriever"
|
3
|
+
|
4
|
+
module NounProjectApi
|
5
|
+
# Retrieve a collection.
|
6
|
+
class CollectionRetriever < Retriever
|
7
|
+
API_PATH = "/collection/".freeze
|
8
|
+
ITEM_CLASS = Collection
|
9
|
+
|
10
|
+
alias_method :find_by_slug, :find
|
11
|
+
end
|
12
|
+
end
|
@@ -6,7 +6,7 @@ module NounProjectApi
|
|
6
6
|
def initialize(token, secret)
|
7
7
|
@token = token
|
8
8
|
@secret = secret
|
9
|
-
fail(ArgumentError,
|
9
|
+
fail(ArgumentError, "Missing token or secret") unless @token && @secret
|
10
10
|
|
11
11
|
@access_token = OAuth::AccessToken.new(OAuth::Consumer.new(token, secret))
|
12
12
|
end
|
@@ -1,36 +1,27 @@
|
|
1
|
+
require "noun-project-api/base_item"
|
2
|
+
|
1
3
|
module NounProjectApi
|
2
4
|
# A single Icon as an abstracted ruby object.
|
3
|
-
class Icon
|
5
|
+
class Icon < BaseItem
|
4
6
|
PREVIEW_SIZE_200 = 200
|
5
7
|
PREVIEW_SIZE_42 = 42
|
6
8
|
PREVIEW_SIZE_84 = 84
|
7
9
|
|
8
|
-
PUBLIC_DOMAIN_LICENSE =
|
9
|
-
|
10
|
-
attr_accessor :original_hash
|
11
|
-
|
12
|
-
def initialize(origin)
|
13
|
-
origin = JSON.parse(origin) if origin.is_a? String
|
14
|
-
origin = origin.delete('icon') if origin.key? 'icon'
|
15
|
-
|
16
|
-
@original_hash = origin
|
17
|
-
end
|
10
|
+
PUBLIC_DOMAIN_LICENSE = "public-domain".freeze
|
18
11
|
|
19
|
-
|
20
|
-
original_hash['id'].to_i
|
21
|
-
end
|
12
|
+
ITEM_NAME = "icon".freeze
|
22
13
|
|
23
14
|
def public_domain?
|
24
|
-
original_hash[
|
15
|
+
original_hash["license_description"] == PUBLIC_DOMAIN_LICENSE
|
25
16
|
end
|
26
17
|
|
27
18
|
def svg_url
|
28
|
-
original_hash[
|
19
|
+
original_hash["icon_url"]
|
29
20
|
end
|
30
21
|
|
31
22
|
def preview_url(size = PREVIEW_SIZE_200)
|
32
23
|
if size == PREVIEW_SIZE_200
|
33
|
-
original_hash[
|
24
|
+
original_hash["preview_url"]
|
34
25
|
else
|
35
26
|
original_hash["preview_url_#{size}"]
|
36
27
|
end
|
@@ -44,9 +35,5 @@ module NounProjectApi
|
|
44
35
|
preview_url_42: preview_url(PREVIEW_SIZE_42)
|
45
36
|
}
|
46
37
|
end
|
47
|
-
|
48
|
-
def to_json
|
49
|
-
JSON.dump(to_hash)
|
50
|
-
end
|
51
38
|
end
|
52
39
|
end
|
@@ -1,19 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require "noun-project-api/icon"
|
2
|
+
require "noun-project-api/retriever"
|
2
3
|
|
3
4
|
module NounProjectApi
|
4
5
|
# Retrieve an icon.
|
5
6
|
class IconRetriever < Retriever
|
6
|
-
API_PATH =
|
7
|
-
|
8
|
-
# Find an icon based on it's id.
|
9
|
-
def find(id)
|
10
|
-
fail(ArgumentError, 'Missing id/slug') unless id
|
11
|
-
|
12
|
-
result = access_token.get("#{API_BASE}#{API_PATH}#{id}")
|
13
|
-
fail(ArgumentError, 'Bad request') unless result.code == '200'
|
14
|
-
|
15
|
-
Icon.new(result.body)
|
16
|
-
end
|
7
|
+
API_PATH = "/icon/".freeze
|
8
|
+
ITEM_CLASS = Icon
|
17
9
|
|
18
10
|
alias_method :find_by_slug, :find
|
19
11
|
end
|
@@ -1,9 +1,9 @@
|
|
1
|
-
require
|
1
|
+
require "noun-project-api/retriever"
|
2
2
|
|
3
3
|
module NounProjectApi
|
4
4
|
# Retrieve icons.
|
5
5
|
class IconsRetriever < Retriever
|
6
|
-
API_PATH =
|
6
|
+
API_PATH = "/icons/".freeze
|
7
7
|
|
8
8
|
# Finds multiple icons based on the term
|
9
9
|
# * term - search term
|
@@ -11,19 +11,23 @@ module NounProjectApi
|
|
11
11
|
# * offset - offset the results
|
12
12
|
# * page - page number
|
13
13
|
def find(term, limit = nil, offset = nil, page = nil)
|
14
|
-
fail(ArgumentError,
|
14
|
+
fail(ArgumentError, "Missing search term") unless term
|
15
15
|
|
16
16
|
search = OAuth::Helper.escape(term)
|
17
17
|
search += "?limit_to_public_domain=#{NounProjectApi.configuration.public_domain ? 1 : 0}"
|
18
18
|
|
19
|
-
args = {
|
19
|
+
args = {
|
20
|
+
"limit" => limit,
|
21
|
+
"offset" => offset,
|
22
|
+
"page" => page
|
23
|
+
}.reject { |_, v| v.nil? }
|
20
24
|
args.each { |k, v| search += "&#{k}=#{v}" } if args.size > 0
|
21
25
|
|
22
26
|
result = access_token.get("#{API_BASE}#{API_PATH}#{search}")
|
23
|
-
fail(ArgumentError,
|
27
|
+
fail(ArgumentError, "Bad request") unless %w(200 404).include? result.code
|
24
28
|
|
25
|
-
if result.code ==
|
26
|
-
JSON.parse(result.body)[
|
29
|
+
if result.code == "200"
|
30
|
+
JSON.parse(result.body)["icons"].map { |icon| Icon.new(icon) }
|
27
31
|
else
|
28
32
|
[]
|
29
33
|
end
|
@@ -34,18 +38,22 @@ module NounProjectApi
|
|
34
38
|
# * offset - offset the results
|
35
39
|
# * page - page number
|
36
40
|
def recent_uploads(limit = nil, offset = nil, page = nil)
|
37
|
-
args = {
|
41
|
+
args = {
|
42
|
+
"limit" => limit,
|
43
|
+
"offset" => offset,
|
44
|
+
"page" => page
|
45
|
+
}.reject { |_, v| v.nil? }
|
38
46
|
if args.size > 0
|
39
|
-
search =
|
47
|
+
search = "?"
|
40
48
|
args.each { |k, v| search += "#{k}=#{v}&" }
|
41
49
|
else
|
42
|
-
search =
|
50
|
+
search = ""
|
43
51
|
end
|
44
52
|
|
45
53
|
result = access_token.get("#{API_BASE}#{API_PATH}recent_uploads#{search}")
|
46
|
-
fail(ArgumentError,
|
54
|
+
fail(ArgumentError, "Bad request") unless result.code == "200"
|
47
55
|
|
48
|
-
JSON.parse(result.body)[
|
56
|
+
JSON.parse(result.body)["recent_uploads"].map { |icon| Icon.new(icon) }
|
49
57
|
end
|
50
58
|
end
|
51
59
|
end
|
@@ -3,18 +3,18 @@ module NounProjectApi
|
|
3
3
|
class Reporter
|
4
4
|
include Connection
|
5
5
|
|
6
|
-
API_PATH =
|
6
|
+
API_PATH = "/notify/publish".freeze
|
7
7
|
|
8
8
|
def report_used(ids)
|
9
9
|
ids = [ids] if ids.is_a?(String) || ids.is_a?(Fixnum)
|
10
|
-
fail(ArgumentError,
|
10
|
+
fail(ArgumentError, "Missing ids") if ids.nil? || ids.empty?
|
11
11
|
|
12
12
|
result = access_token.post(
|
13
13
|
"#{API_BASE}#{API_PATH}",
|
14
|
-
{ icons: ids.join(
|
15
|
-
|
14
|
+
{ icons: ids.join(",") }.to_json,
|
15
|
+
"Accept" => "application/json", "Content-Type" => "application/json"
|
16
16
|
)
|
17
|
-
result.code ==
|
17
|
+
result.code == "200"
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
@@ -2,5 +2,15 @@ module NounProjectApi
|
|
2
2
|
# A base class for different retriever classes.
|
3
3
|
class Retriever
|
4
4
|
include Connection
|
5
|
+
|
6
|
+
# Find an item based on it's id.
|
7
|
+
def find(id)
|
8
|
+
fail(ArgumentError, "Missing id/slug") unless id
|
9
|
+
|
10
|
+
result = access_token.get("#{API_BASE}#{self.class::API_PATH}#{id}")
|
11
|
+
fail(ArgumentError, "Bad request") unless result.code == "200"
|
12
|
+
|
13
|
+
self.class::ITEM_CLASS.new(result.body)
|
14
|
+
end
|
5
15
|
end
|
6
16
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "ostruct"
|
3
|
+
|
4
|
+
RSpec.describe NounProjectApi::BaseItem do
|
5
|
+
it "raises an error on direct initialization" do
|
6
|
+
data = JSON.parse(Fakes::Results::ICON_VALID)
|
7
|
+
expect {
|
8
|
+
NounProjectApi::BaseItem.new(JSON.dump(data["icon"]))
|
9
|
+
}.to raise_error(NotImplementedError)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "ostruct"
|
3
|
+
|
4
|
+
RSpec.describe NounProjectApi::CollectionRetriever do
|
5
|
+
before :each do
|
6
|
+
@collection = NounProjectApi::CollectionRetriever.new(Faker::Internet.password(16), Faker::Internet.password(16))
|
7
|
+
@valid_hash = JSON.parse(Fakes::Results::COLLECTION_VALID)
|
8
|
+
@valid_response = OpenStruct.new(
|
9
|
+
body: Fakes::Results::COLLECTION_VALID,
|
10
|
+
code: "200"
|
11
|
+
)
|
12
|
+
|
13
|
+
@missing_response = OpenStruct.new(
|
14
|
+
code: "404"
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
context "id" do
|
19
|
+
it "raises an error when no id is provided" do
|
20
|
+
expect { @collection.find(nil) }.to raise_error(ArgumentError)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns a proper result with a correct id" do
|
24
|
+
id = 1
|
25
|
+
expect(@collection.access_token).to receive(
|
26
|
+
:get
|
27
|
+
).with(
|
28
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::CollectionRetriever::API_PATH}#{id}"
|
29
|
+
).and_return(
|
30
|
+
@valid_response
|
31
|
+
)
|
32
|
+
|
33
|
+
result = @collection.find(id)
|
34
|
+
expect(result).to be_a(NounProjectApi::Collection)
|
35
|
+
expect(result.original_hash).to eq(@valid_hash["collection"])
|
36
|
+
end
|
37
|
+
|
38
|
+
it "raises an error with a missing id" do
|
39
|
+
id = 1
|
40
|
+
expect(@collection.access_token).to receive(
|
41
|
+
:get
|
42
|
+
).with(
|
43
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::CollectionRetriever::API_PATH}#{id}"
|
44
|
+
).and_return(
|
45
|
+
@missing_response
|
46
|
+
)
|
47
|
+
|
48
|
+
expect { @collection.find(id) }.to raise_error(ArgumentError)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "slug" do
|
53
|
+
it "raises an error when no slug is provided" do
|
54
|
+
expect { @collection.find_by_slug(nil) }.to raise_error(ArgumentError)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "returns a proper result with a correct slug" do
|
58
|
+
slug = "existing_slug"
|
59
|
+
expect(@collection.access_token).to receive(
|
60
|
+
:get
|
61
|
+
).with(
|
62
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::CollectionRetriever::API_PATH}#{slug}"
|
63
|
+
).and_return(
|
64
|
+
@valid_response
|
65
|
+
)
|
66
|
+
|
67
|
+
result = @collection.find(slug)
|
68
|
+
expect(result).to be_a(NounProjectApi::Collection)
|
69
|
+
expect(result.original_hash).to eq(@valid_hash["collection"])
|
70
|
+
end
|
71
|
+
|
72
|
+
it "raises an error with a missing slug" do
|
73
|
+
slug = "missing_slug"
|
74
|
+
expect(@collection.access_token).to receive(
|
75
|
+
:get
|
76
|
+
).with(
|
77
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::CollectionRetriever::API_PATH}#{slug}"
|
78
|
+
).and_return(
|
79
|
+
@missing_response
|
80
|
+
)
|
81
|
+
|
82
|
+
expect { @collection.find_by_slug(slug) }.to raise_error(ArgumentError)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "ostruct"
|
3
|
+
|
4
|
+
RSpec.describe NounProjectApi::CollectionRetriever do
|
5
|
+
it "raises an error on empty initialization input" do
|
6
|
+
expect { NounProjectApi::Collection.new }.to raise_error(ArgumentError)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "accepts JSON string input" do
|
10
|
+
data = JSON.parse(Fakes::Results::COLLECTION_VALID)
|
11
|
+
expect { NounProjectApi::Collection.new(JSON.dump(data["collection"])) }.to_not raise_error
|
12
|
+
end
|
13
|
+
|
14
|
+
it "accepts JSON string input with collection encapsulation" do
|
15
|
+
expect { NounProjectApi::Collection.new(Fakes::Results::COLLECTION_VALID) }.to_not raise_error
|
16
|
+
end
|
17
|
+
|
18
|
+
it "accepts hash input" do
|
19
|
+
data = JSON.parse(Fakes::Results::COLLECTION_VALID)
|
20
|
+
expect { NounProjectApi::Collection.new(data) }.to_not raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
context "exposed attributes" do
|
24
|
+
before :each do
|
25
|
+
@json = JSON.parse(Fakes::Results::COLLECTION_VALID)
|
26
|
+
@valid_collection = NounProjectApi::Collection.new(Fakes::Results::COLLECTION_VALID)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "id as number" do
|
30
|
+
expect(@valid_collection.id).to be_a(Fixnum)
|
31
|
+
expect(@valid_collection.id).to eq(@json["collection"]["id"].to_i)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "author id as number" do
|
35
|
+
expect(@valid_collection.author_id).to eq(@json["collection"]["author_id"].to_i)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "author name" do
|
39
|
+
expect(@valid_collection.author_name).to eq(@json["collection"]["author"]["name"])
|
40
|
+
end
|
41
|
+
|
42
|
+
it "icon count as number" do
|
43
|
+
expect(@valid_collection.icon_count).to eq(@json["collection"]["icon_count"].to_i)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "is published" do
|
47
|
+
expect(@valid_collection.is_published?).to eq(@json["collection"]["is_published"].to_i == 1)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "builds a simple hash" do
|
51
|
+
expect(@valid_collection.to_hash).to eq({
|
52
|
+
id: @valid_collection.id,
|
53
|
+
author_id: @valid_collection.author_id,
|
54
|
+
author_name: @valid_collection.author_name,
|
55
|
+
icon_count: @valid_collection.icon_count,
|
56
|
+
is_published: @valid_collection.is_published?
|
57
|
+
})
|
58
|
+
end
|
59
|
+
|
60
|
+
it "json formats the hash" do
|
61
|
+
expect(@valid_collection.to_json).to eq JSON.dump @valid_collection.to_hash
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "spec_helper"
|
2
|
+
require "ostruct"
|
3
3
|
|
4
4
|
RSpec.describe NounProjectApi::IconRetriever do
|
5
5
|
before :each do
|
@@ -7,20 +7,20 @@ RSpec.describe NounProjectApi::IconRetriever do
|
|
7
7
|
@valid_hash = JSON.parse(Fakes::Results::ICON_VALID)
|
8
8
|
@valid_response = OpenStruct.new(
|
9
9
|
body: Fakes::Results::ICON_VALID,
|
10
|
-
code:
|
10
|
+
code: "200"
|
11
11
|
)
|
12
12
|
|
13
13
|
@missing_response = OpenStruct.new(
|
14
|
-
code:
|
14
|
+
code: "404"
|
15
15
|
)
|
16
16
|
end
|
17
17
|
|
18
18
|
context "id" do
|
19
|
-
it
|
19
|
+
it "raises an error when no id is provided" do
|
20
20
|
expect { @icon.find(nil) }.to raise_error(ArgumentError)
|
21
21
|
end
|
22
22
|
|
23
|
-
it
|
23
|
+
it "returns a proper result with a correct id" do
|
24
24
|
id = 1
|
25
25
|
expect(@icon.access_token).to receive(
|
26
26
|
:get
|
@@ -35,7 +35,7 @@ RSpec.describe NounProjectApi::IconRetriever do
|
|
35
35
|
expect(result.original_hash).to eq(@valid_hash["icon"])
|
36
36
|
end
|
37
37
|
|
38
|
-
it
|
38
|
+
it "raises an error with a missing id" do
|
39
39
|
id = 1
|
40
40
|
expect(@icon.access_token).to receive(
|
41
41
|
:get
|
@@ -50,12 +50,12 @@ RSpec.describe NounProjectApi::IconRetriever do
|
|
50
50
|
end
|
51
51
|
|
52
52
|
context "slug" do
|
53
|
-
it
|
53
|
+
it "raises an error when no slug is provided" do
|
54
54
|
expect { @icon.find_by_slug(nil) }.to raise_error(ArgumentError)
|
55
55
|
end
|
56
56
|
|
57
|
-
it
|
58
|
-
slug =
|
57
|
+
it "returns a proper result with a correct slug" do
|
58
|
+
slug = "existing_slug"
|
59
59
|
expect(@icon.access_token).to receive(
|
60
60
|
:get
|
61
61
|
).with(
|
@@ -69,8 +69,8 @@ RSpec.describe NounProjectApi::IconRetriever do
|
|
69
69
|
expect(result.original_hash).to eq(@valid_hash["icon"])
|
70
70
|
end
|
71
71
|
|
72
|
-
it
|
73
|
-
slug =
|
72
|
+
it "raises an error with a missing slug" do
|
73
|
+
slug = "missing_slug"
|
74
74
|
expect(@icon.access_token).to receive(
|
75
75
|
:get
|
76
76
|
).with(
|
@@ -1,21 +1,21 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "spec_helper"
|
2
|
+
require "ostruct"
|
3
3
|
|
4
4
|
RSpec.describe NounProjectApi::IconRetriever do
|
5
|
-
it
|
5
|
+
it "raises an error on empty initialization input" do
|
6
6
|
expect { NounProjectApi::Icon.new }.to raise_error(ArgumentError)
|
7
7
|
end
|
8
8
|
|
9
|
-
it
|
9
|
+
it "accepts JSON string input" do
|
10
10
|
data = JSON.parse(Fakes::Results::ICON_VALID)
|
11
11
|
expect { NounProjectApi::Icon.new(JSON.dump(data["icon"])) }.to_not raise_error
|
12
12
|
end
|
13
13
|
|
14
|
-
it
|
14
|
+
it "accepts JSON string input with icon encapsulation" do
|
15
15
|
expect { NounProjectApi::Icon.new(Fakes::Results::ICON_VALID) }.to_not raise_error
|
16
16
|
end
|
17
17
|
|
18
|
-
it
|
18
|
+
it "accepts hash input" do
|
19
19
|
data = JSON.parse(Fakes::Results::ICON_VALID)
|
20
20
|
expect { NounProjectApi::Icon.new(data) }.to_not raise_error
|
21
21
|
end
|
@@ -26,42 +26,42 @@ RSpec.describe NounProjectApi::IconRetriever do
|
|
26
26
|
@valid_icon = NounProjectApi::Icon.new(Fakes::Results::ICON_VALID)
|
27
27
|
end
|
28
28
|
|
29
|
-
it
|
29
|
+
it "public domain" do
|
30
30
|
expect(@valid_icon.public_domain?).to eq(@json["icon"]["license_description"] == "public-domain")
|
31
31
|
end
|
32
32
|
|
33
|
-
it
|
33
|
+
it "SVG url" do
|
34
34
|
expect(@valid_icon.svg_url).to eq(@json["icon"]["icon_url"])
|
35
35
|
end
|
36
36
|
|
37
|
-
it
|
37
|
+
it "nil on missing SVG url" do
|
38
38
|
@json["icon"].delete("icon_url")
|
39
39
|
icon = NounProjectApi::Icon.new(@json)
|
40
40
|
expect(icon.svg_url).to be_nil
|
41
41
|
end
|
42
42
|
|
43
|
-
it
|
43
|
+
it "preview url defaults at 200 size" do
|
44
44
|
expect(@valid_icon.preview_url).to eq(@json["icon"]["preview_url"])
|
45
45
|
end
|
46
46
|
|
47
|
-
it
|
47
|
+
it "preview url for 200 size" do
|
48
48
|
expect(@valid_icon.preview_url(NounProjectApi::Icon::PREVIEW_SIZE_200)).to eq(@json["icon"]["preview_url"])
|
49
49
|
end
|
50
50
|
|
51
|
-
it
|
51
|
+
it "preview url for 84 size" do
|
52
52
|
expect(@valid_icon.preview_url(NounProjectApi::Icon::PREVIEW_SIZE_84)).to eq(@json["icon"]["preview_url_84"])
|
53
53
|
end
|
54
54
|
|
55
|
-
it
|
55
|
+
it "preview url for 42 size" do
|
56
56
|
expect(@valid_icon.preview_url(NounProjectApi::Icon::PREVIEW_SIZE_42)).to eq(@json["icon"]["preview_url_42"])
|
57
57
|
end
|
58
58
|
|
59
|
-
it
|
59
|
+
it "id as number" do
|
60
60
|
expect(@valid_icon.id).to be_a(Fixnum)
|
61
61
|
expect(@valid_icon.id).to eq(@json["icon"]["id"].to_i)
|
62
62
|
end
|
63
63
|
|
64
|
-
it
|
64
|
+
it "builds a simple hash" do
|
65
65
|
expect(@valid_icon.to_hash).to eq({
|
66
66
|
id: @valid_icon.id,
|
67
67
|
preview_url_200: @valid_icon.preview_url(NounProjectApi::Icon::PREVIEW_SIZE_200),
|
@@ -70,7 +70,7 @@ RSpec.describe NounProjectApi::IconRetriever do
|
|
70
70
|
})
|
71
71
|
end
|
72
72
|
|
73
|
-
it
|
73
|
+
it "json formats the hash" do
|
74
74
|
expect(@valid_icon.to_json).to eq JSON.dump @valid_icon.to_hash
|
75
75
|
end
|
76
76
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
RSpec.describe NounProjectApi::IconsRetriever do
|
4
4
|
before :each do
|
@@ -10,7 +10,7 @@ RSpec.describe NounProjectApi::IconsRetriever do
|
|
10
10
|
valid_hash = JSON.parse(Fakes::Results::ICONS_RECENT_VALID)
|
11
11
|
valid_response = OpenStruct.new(
|
12
12
|
body: Fakes::Results::ICONS_RECENT_VALID,
|
13
|
-
code:
|
13
|
+
code: "200"
|
14
14
|
)
|
15
15
|
|
16
16
|
expect(@icons.access_token).to receive(
|
@@ -31,7 +31,7 @@ RSpec.describe NounProjectApi::IconsRetriever do
|
|
31
31
|
it "returns the recent uploads and passes limit when passed" do
|
32
32
|
valid_response = OpenStruct.new(
|
33
33
|
body: Fakes::Results::ICONS_RECENT_VALID,
|
34
|
-
code:
|
34
|
+
code: "200"
|
35
35
|
)
|
36
36
|
|
37
37
|
limit = 3
|
@@ -53,18 +53,18 @@ RSpec.describe NounProjectApi::IconsRetriever do
|
|
53
53
|
end
|
54
54
|
|
55
55
|
context "Icons search" do
|
56
|
-
it
|
56
|
+
it "raises an error when no phrase is provided" do
|
57
57
|
expect { @icons.find(nil) }.to raise_error(ArgumentError)
|
58
58
|
end
|
59
59
|
|
60
|
-
it
|
60
|
+
it "properly URI encodes search patterns" do
|
61
61
|
valid_hash = JSON.parse(Fakes::Results::ICONS_VALID)
|
62
62
|
valid_response = OpenStruct.new(
|
63
63
|
body: Fakes::Results::ICONS_VALID,
|
64
|
-
code:
|
64
|
+
code: "200"
|
65
65
|
)
|
66
66
|
|
67
|
-
term =
|
67
|
+
term = "some search with \",] bad chars"
|
68
68
|
expect(@icons.access_token).to receive(
|
69
69
|
:get
|
70
70
|
).with(
|
@@ -80,14 +80,14 @@ RSpec.describe NounProjectApi::IconsRetriever do
|
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
-
it
|
83
|
+
it "returns a proper result with a correct phrase" do
|
84
84
|
valid_hash = JSON.parse(Fakes::Results::ICONS_VALID)
|
85
85
|
valid_response = OpenStruct.new(
|
86
86
|
body: Fakes::Results::ICONS_VALID,
|
87
|
-
code:
|
87
|
+
code: "200"
|
88
88
|
)
|
89
89
|
|
90
|
-
term =
|
90
|
+
term = "some search"
|
91
91
|
expect(@icons.access_token).to receive(
|
92
92
|
:get
|
93
93
|
).with(
|
@@ -103,14 +103,14 @@ RSpec.describe NounProjectApi::IconsRetriever do
|
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
106
|
-
it
|
106
|
+
it "properly handles public domain only config" do
|
107
107
|
valid_hash = JSON.parse(Fakes::Results::ICONS_VALID)
|
108
108
|
valid_response = OpenStruct.new(
|
109
109
|
body: Fakes::Results::ICONS_VALID,
|
110
|
-
code:
|
110
|
+
code: "200"
|
111
111
|
)
|
112
112
|
|
113
|
-
term =
|
113
|
+
term = "some search"
|
114
114
|
expect(@icons.access_token).to receive(
|
115
115
|
:get
|
116
116
|
).with(
|
@@ -128,13 +128,13 @@ RSpec.describe NounProjectApi::IconsRetriever do
|
|
128
128
|
NounProjectApi.configuration.public_domain = false
|
129
129
|
end
|
130
130
|
|
131
|
-
it
|
131
|
+
it "returns a proper result with a correct phrase and passes the args" do
|
132
132
|
valid_response = OpenStruct.new(
|
133
133
|
body: Fakes::Results::ICONS_VALID,
|
134
|
-
code:
|
134
|
+
code: "200"
|
135
135
|
)
|
136
136
|
|
137
|
-
term =
|
137
|
+
term = "some search"
|
138
138
|
limit = 4
|
139
139
|
expect(@icons.access_token).to receive(
|
140
140
|
:get
|
@@ -151,12 +151,12 @@ RSpec.describe NounProjectApi::IconsRetriever do
|
|
151
151
|
end
|
152
152
|
end
|
153
153
|
|
154
|
-
it
|
154
|
+
it "returns an empty array for no result" do
|
155
155
|
missing_response = OpenStruct.new(
|
156
|
-
code:
|
156
|
+
code: "404"
|
157
157
|
)
|
158
158
|
|
159
|
-
term =
|
159
|
+
term = "missing search"
|
160
160
|
expect(@icons.access_token).to receive(
|
161
161
|
:get
|
162
162
|
).with(
|
@@ -1,15 +1,15 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
RSpec.describe NounProjectApi::Reporter do
|
4
|
-
it
|
4
|
+
it "raises an error when initialized without token" do
|
5
5
|
expect { NounProjectApi::Reporter.new(nil, Faker::Internet.password(16)) }.to raise_error(ArgumentError)
|
6
6
|
end
|
7
7
|
|
8
|
-
it
|
8
|
+
it "raises an error when initialized without secret" do
|
9
9
|
expect { NounProjectApi::Reporter.new(Faker::Internet.password(16), nil) }.to raise_error(ArgumentError)
|
10
10
|
end
|
11
11
|
|
12
|
-
it
|
12
|
+
it "initializes the values properly" do
|
13
13
|
token = Faker::Internet.password(16)
|
14
14
|
secret = Faker::Internet.password(16)
|
15
15
|
reporter = NounProjectApi::Reporter.new(token, secret)
|
@@ -18,17 +18,17 @@ RSpec.describe NounProjectApi::Reporter do
|
|
18
18
|
expect(reporter.secret).to eq(secret)
|
19
19
|
end
|
20
20
|
|
21
|
-
context
|
21
|
+
context "reports ids usage" do
|
22
22
|
before :each do
|
23
23
|
token = Faker::Internet.password(16)
|
24
24
|
secret = Faker::Internet.password(16)
|
25
25
|
@reporter = NounProjectApi::Reporter.new(token, secret)
|
26
26
|
end
|
27
27
|
|
28
|
-
it
|
28
|
+
it "reports a singular id" do
|
29
29
|
valid_response = OpenStruct.new(
|
30
30
|
body: Fakes::Results::REPORTED_ONE,
|
31
|
-
code:
|
31
|
+
code: "200"
|
32
32
|
)
|
33
33
|
|
34
34
|
id = 122
|
@@ -38,7 +38,7 @@ RSpec.describe NounProjectApi::Reporter do
|
|
38
38
|
).with(
|
39
39
|
"#{NounProjectApi::API_BASE}#{NounProjectApi::Reporter::API_PATH}",
|
40
40
|
{ icons: id.to_s }.to_json,
|
41
|
-
|
41
|
+
"Accept" => "application/json", "Content-Type" => "application/json"
|
42
42
|
).and_return(
|
43
43
|
valid_response
|
44
44
|
)
|
@@ -47,20 +47,20 @@ RSpec.describe NounProjectApi::Reporter do
|
|
47
47
|
expect(result).to be true
|
48
48
|
end
|
49
49
|
|
50
|
-
it
|
50
|
+
it "reports a singular id for a string" do
|
51
51
|
valid_response = OpenStruct.new(
|
52
52
|
body: Fakes::Results::REPORTED_ONE,
|
53
|
-
code:
|
53
|
+
code: "200"
|
54
54
|
)
|
55
55
|
|
56
|
-
id =
|
56
|
+
id = "122"
|
57
57
|
|
58
58
|
expect(@reporter.access_token).to receive(
|
59
59
|
:post
|
60
60
|
).with(
|
61
61
|
"#{NounProjectApi::API_BASE}#{NounProjectApi::Reporter::API_PATH}",
|
62
62
|
{ icons: id }.to_json,
|
63
|
-
|
63
|
+
"Accept" => "application/json", "Content-Type" => "application/json"
|
64
64
|
).and_return(
|
65
65
|
valid_response
|
66
66
|
)
|
@@ -71,10 +71,10 @@ RSpec.describe NounProjectApi::Reporter do
|
|
71
71
|
|
72
72
|
|
73
73
|
|
74
|
-
it
|
74
|
+
it "reports multiple ids" do
|
75
75
|
valid_response = OpenStruct.new(
|
76
76
|
body: Fakes::Results::REPORTED_ONE,
|
77
|
-
code:
|
77
|
+
code: "200"
|
78
78
|
)
|
79
79
|
|
80
80
|
ids = [122, 4541, 342_11, 4352]
|
@@ -83,8 +83,8 @@ RSpec.describe NounProjectApi::Reporter do
|
|
83
83
|
:post
|
84
84
|
).with(
|
85
85
|
"#{NounProjectApi::API_BASE}#{NounProjectApi::Reporter::API_PATH}",
|
86
|
-
{ icons: ids.join(
|
87
|
-
|
86
|
+
{ icons: ids.join(",") }.to_json,
|
87
|
+
"Accept" => "application/json", "Content-Type" => "application/json"
|
88
88
|
).and_return(
|
89
89
|
valid_response
|
90
90
|
)
|
@@ -1,15 +1,15 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
RSpec.describe NounProjectApi::Retriever do
|
4
|
-
it
|
4
|
+
it "raises an error when initialized without token" do
|
5
5
|
expect { NounProjectApi::Retriever.new(nil, Faker::Internet.password(16)) }.to raise_error(ArgumentError)
|
6
6
|
end
|
7
7
|
|
8
|
-
it
|
8
|
+
it "raises an error when initialized without secret" do
|
9
9
|
expect { NounProjectApi::Retriever.new(Faker::Internet.password(16), nil) }.to raise_error(ArgumentError)
|
10
10
|
end
|
11
11
|
|
12
|
-
it
|
12
|
+
it "initializes the values properly" do
|
13
13
|
token = Faker::Internet.password(16)
|
14
14
|
secret = Faker::Internet.password(16)
|
15
15
|
retriever = NounProjectApi::Retriever.new(token, secret)
|
@@ -1,11 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
RSpec.describe NounProjectApi do
|
4
|
-
it
|
4
|
+
it "has the correct default" do
|
5
5
|
expect(NounProjectApi.configuration.public_domain).to be false
|
6
6
|
end
|
7
7
|
|
8
|
-
it
|
8
|
+
it "sets the correct configuration" do
|
9
9
|
expect(NounProjectApi.configuration.public_domain).to be false
|
10
10
|
|
11
11
|
NounProjectApi.configure do |config|
|
data/spec/support/fakes.rb
CHANGED
@@ -308,5 +308,86 @@ module Fakes
|
|
308
308
|
"licenses_consumed": 1,
|
309
309
|
"result": "success"
|
310
310
|
}'''
|
311
|
+
|
312
|
+
COLLECTION_VALID = '''
|
313
|
+
{
|
314
|
+
"collection": {
|
315
|
+
"author": {
|
316
|
+
"location": "US",
|
317
|
+
"name": "irene hoffman",
|
318
|
+
"permalink": "/i",
|
319
|
+
"username": "i"
|
320
|
+
},
|
321
|
+
"author_id": "5562",
|
322
|
+
"date_created": "2014-06-27 00:22:22",
|
323
|
+
"date_updated": "2014-06-27 00:22:22",
|
324
|
+
"description": "",
|
325
|
+
"icon_count": "4",
|
326
|
+
"id": "321",
|
327
|
+
"is_collaborative": "",
|
328
|
+
"is_featured": "0",
|
329
|
+
"is_published": "1",
|
330
|
+
"is_store_item": "0",
|
331
|
+
"name": "genetics",
|
332
|
+
"permalink": "/i/collection/genetics",
|
333
|
+
"slug": "genetics",
|
334
|
+
"sponsor": {},
|
335
|
+
"sponsor_campaign_link": "",
|
336
|
+
"sponsor_id": "",
|
337
|
+
"tags": [
|
338
|
+
"genetics",
|
339
|
+
"chromosome",
|
340
|
+
"gene",
|
341
|
+
"heredity",
|
342
|
+
"genetic code",
|
343
|
+
"nucleic acid",
|
344
|
+
"RNA",
|
345
|
+
"DNA",
|
346
|
+
"deoxyribonucleic acid",
|
347
|
+
"reproduction",
|
348
|
+
"genome",
|
349
|
+
"human",
|
350
|
+
"chemistry",
|
351
|
+
"Allele",
|
352
|
+
"Amelogenin",
|
353
|
+
"bases",
|
354
|
+
"building blocks",
|
355
|
+
"cytosine",
|
356
|
+
"guanine",
|
357
|
+
"thymine",
|
358
|
+
"adenine",
|
359
|
+
"c",
|
360
|
+
"G",
|
361
|
+
"T",
|
362
|
+
"CGTA",
|
363
|
+
"A",
|
364
|
+
"biological",
|
365
|
+
"cell",
|
366
|
+
"CODIS",
|
367
|
+
"blueprint of life",
|
368
|
+
"life",
|
369
|
+
"material",
|
370
|
+
"nucleus",
|
371
|
+
"inherited",
|
372
|
+
"parent",
|
373
|
+
"unique",
|
374
|
+
"analysis",
|
375
|
+
"double helix",
|
376
|
+
"helix",
|
377
|
+
"forensic",
|
378
|
+
"loci",
|
379
|
+
"genotype",
|
380
|
+
"haplotype",
|
381
|
+
"junk DNA",
|
382
|
+
"match",
|
383
|
+
"nuclear",
|
384
|
+
"paternal",
|
385
|
+
"sequence",
|
386
|
+
"sequencing"
|
387
|
+
],
|
388
|
+
"template": "24"
|
389
|
+
}
|
390
|
+
}
|
391
|
+
'''
|
311
392
|
end
|
312
393
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: noun-project-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nadav Shatz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oauth
|
@@ -94,12 +94,18 @@ extra_rdoc_files: []
|
|
94
94
|
files:
|
95
95
|
- Rakefile
|
96
96
|
- lib/noun-project-api.rb
|
97
|
+
- lib/noun-project-api/base_item.rb
|
98
|
+
- lib/noun-project-api/collection.rb
|
99
|
+
- lib/noun-project-api/collection_retriever.rb
|
97
100
|
- lib/noun-project-api/connection.rb
|
98
101
|
- lib/noun-project-api/icon.rb
|
99
102
|
- lib/noun-project-api/icon_retriever.rb
|
100
103
|
- lib/noun-project-api/icons_retriever.rb
|
101
104
|
- lib/noun-project-api/reporter.rb
|
102
105
|
- lib/noun-project-api/retriever.rb
|
106
|
+
- spec/lib/noun-project-api/base_item_spec.rb
|
107
|
+
- spec/lib/noun-project-api/collection_retriever_spec.rb
|
108
|
+
- spec/lib/noun-project-api/collection_spec.rb
|
103
109
|
- spec/lib/noun-project-api/icon_retriever_spec.rb
|
104
110
|
- spec/lib/noun-project-api/icon_spec.rb
|
105
111
|
- spec/lib/noun-project-api/icons_retriever_spec.rb
|
@@ -128,11 +134,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
134
|
version: '0'
|
129
135
|
requirements: []
|
130
136
|
rubyforge_project:
|
131
|
-
rubygems_version: 2.
|
137
|
+
rubygems_version: 2.5.1
|
132
138
|
signing_key:
|
133
139
|
specification_version: 4
|
134
140
|
summary: An API wrapper for The Noun Project API's
|
135
141
|
test_files:
|
142
|
+
- spec/lib/noun-project-api/base_item_spec.rb
|
143
|
+
- spec/lib/noun-project-api/collection_retriever_spec.rb
|
144
|
+
- spec/lib/noun-project-api/collection_spec.rb
|
136
145
|
- spec/lib/noun-project-api/icon_retriever_spec.rb
|
137
146
|
- spec/lib/noun-project-api/icon_spec.rb
|
138
147
|
- spec/lib/noun-project-api/icons_retriever_spec.rb
|