noun-project-api 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/noun-project-api.rb +2 -1
- data/lib/noun-project-api/icon.rb +30 -10
- data/lib/noun-project-api/icon_retriever.rb +18 -0
- data/lib/noun-project-api/{icons.rb → icons_retriever.rb} +3 -3
- data/spec/lib/noun-project-api/icon_retriever_spec.rb +85 -0
- data/spec/lib/noun-project-api/icon_spec.rb +42 -59
- data/spec/lib/noun-project-api/{icons_spec.rb → icons_retriever_spec.rb} +37 -18
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 810e93f2a6007af38536ac99d6d297b8a84419df
|
4
|
+
data.tar.gz: f672c9976b6425ca47ac49046b86ed52b43dcbd7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f825fc58d3e1a3a217433488f0c3e91fab753eb05c7241a410690a50af2968b8f0448d5c787d630fb9e86e76155d461c81a336727fbc9cb54dd14af895231a0b
|
7
|
+
data.tar.gz: 80c91604f12dc4df532f30347efa178abefe40b74962593e67773cf89bb2a11e138ac2a096e1930c8e0e1f1db37e6b1235abf91901e9c7de7ebc0ec151efe299
|
data/lib/noun-project-api.rb
CHANGED
@@ -1,18 +1,38 @@
|
|
1
|
-
require 'noun-project-api/retriever'
|
2
|
-
|
3
1
|
module NounProjectApi
|
4
|
-
class Icon
|
5
|
-
|
2
|
+
class Icon
|
3
|
+
PREVIEW_SIZE_200 = 200
|
4
|
+
PREVIEW_SIZE_42 = 42
|
5
|
+
PREVIEW_SIZE_84 = 84
|
6
|
+
|
7
|
+
PUBLIC_DOMAIN_LICENSE = "public-domain"
|
6
8
|
|
7
|
-
|
8
|
-
raise ArgumentError.new('Missing id/slug') unless id
|
9
|
+
attr_accessor :original_hash
|
9
10
|
|
10
|
-
|
11
|
-
|
11
|
+
def initialize(origin)
|
12
|
+
origin = JSON.parse(origin) if origin.is_a? String
|
13
|
+
origin = origin.delete("icon") if origin.key? "icon"
|
12
14
|
|
13
|
-
|
15
|
+
@original_hash = origin
|
14
16
|
end
|
15
17
|
|
16
|
-
|
18
|
+
def id
|
19
|
+
original_hash["id"].to_i
|
20
|
+
end
|
21
|
+
|
22
|
+
def public_domain?
|
23
|
+
original_hash["license_description"] == PUBLIC_DOMAIN_LICENSE
|
24
|
+
end
|
25
|
+
|
26
|
+
def svg_url
|
27
|
+
original_hash["icon_url"]
|
28
|
+
end
|
29
|
+
|
30
|
+
def preview_url(size = PREVIEW_SIZE_200)
|
31
|
+
if size == PREVIEW_SIZE_200
|
32
|
+
original_hash["preview_url"]
|
33
|
+
else
|
34
|
+
original_hash["preview_url_#{size}"]
|
35
|
+
end
|
36
|
+
end
|
17
37
|
end
|
18
38
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'noun-project-api/retriever'
|
2
|
+
|
3
|
+
module NounProjectApi
|
4
|
+
class IconRetriever < Retriever
|
5
|
+
API_PATH = "/icon/"
|
6
|
+
|
7
|
+
def find(id)
|
8
|
+
raise ArgumentError.new('Missing id/slug') unless id
|
9
|
+
|
10
|
+
result = self.access_token.get("#{API_BASE}#{API_PATH}#{id}")
|
11
|
+
raise ArgumentError.new('Bad request') unless result.code == '200'
|
12
|
+
|
13
|
+
Icon.new(result.body)
|
14
|
+
end
|
15
|
+
|
16
|
+
alias_method :find_by_slug, :find
|
17
|
+
end
|
18
|
+
end
|
@@ -2,7 +2,7 @@ require 'noun-project-api/retriever'
|
|
2
2
|
require 'open-uri'
|
3
3
|
|
4
4
|
module NounProjectApi
|
5
|
-
class
|
5
|
+
class IconsRetriever < Retriever
|
6
6
|
API_PATH = "/icons/"
|
7
7
|
|
8
8
|
def find(term, limit = nil, offset = nil, page = nil)
|
@@ -20,7 +20,7 @@ module NounProjectApi
|
|
20
20
|
raise ArgumentError.new('Bad request') unless ['200', '404'].include? result.code
|
21
21
|
|
22
22
|
if result.code == '200'
|
23
|
-
JSON.parse(result.body)["icons"]
|
23
|
+
JSON.parse(result.body)["icons"].map { |icon| Icon.new(icon) }
|
24
24
|
else
|
25
25
|
[]
|
26
26
|
end
|
@@ -38,7 +38,7 @@ module NounProjectApi
|
|
38
38
|
result = self.access_token.get("#{API_BASE}#{API_PATH}recent_uploads#{search}")
|
39
39
|
raise ArgumentError.new('Bad request') unless result.code == '200'
|
40
40
|
|
41
|
-
JSON.parse(result.body)["recent_uploads"]
|
41
|
+
JSON.parse(result.body)["recent_uploads"].map { |icon| Icon.new(icon) }
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
RSpec.describe NounProjectApi::IconRetriever do
|
5
|
+
before :each do
|
6
|
+
@icon = NounProjectApi::IconRetriever.new(Faker::Internet.password(16), Faker::Internet.password(16))
|
7
|
+
@valid_hash = JSON.parse(Fakes::Results::ICON_VALID)
|
8
|
+
@valid_response = OpenStruct.new(
|
9
|
+
body: Fakes::Results::ICON_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 { @icon.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(@icon.access_token).to receive(
|
26
|
+
:get
|
27
|
+
).with(
|
28
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::IconRetriever::API_PATH}#{id}"
|
29
|
+
).and_return(
|
30
|
+
@valid_response
|
31
|
+
)
|
32
|
+
|
33
|
+
result = @icon.find(id)
|
34
|
+
expect(result).to be_a(NounProjectApi::Icon)
|
35
|
+
expect(result.original_hash).to eq(@valid_hash["icon"])
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'raises an error with a missing id' do
|
39
|
+
id = 1
|
40
|
+
expect(@icon.access_token).to receive(
|
41
|
+
:get
|
42
|
+
).with(
|
43
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::IconRetriever::API_PATH}#{id}"
|
44
|
+
).and_return(
|
45
|
+
@missing_response
|
46
|
+
)
|
47
|
+
|
48
|
+
expect { @icon.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 { @icon.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(@icon.access_token).to receive(
|
60
|
+
:get
|
61
|
+
).with(
|
62
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::IconRetriever::API_PATH}#{slug}"
|
63
|
+
).and_return(
|
64
|
+
@valid_response
|
65
|
+
)
|
66
|
+
|
67
|
+
result = @icon.find(slug)
|
68
|
+
expect(result).to be_a(NounProjectApi::Icon)
|
69
|
+
expect(result.original_hash).to eq(@valid_hash["icon"])
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'raises an error with a missing slug' do
|
73
|
+
slug = 'missing_slug'
|
74
|
+
expect(@icon.access_token).to receive(
|
75
|
+
:get
|
76
|
+
).with(
|
77
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::IconRetriever::API_PATH}#{slug}"
|
78
|
+
).and_return(
|
79
|
+
@missing_response
|
80
|
+
)
|
81
|
+
|
82
|
+
expect { @icon.find_by_slug(slug) }.to raise_error(ArgumentError)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -1,81 +1,64 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'ostruct'
|
3
3
|
|
4
|
-
RSpec.describe NounProjectApi::
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
@valid_response = OpenStruct.new(
|
9
|
-
body: Fakes::Results::ICON_VALID,
|
10
|
-
code: '200'
|
11
|
-
)
|
4
|
+
RSpec.describe NounProjectApi::IconRetriever do
|
5
|
+
it 'raises an error on empty initialization input' do
|
6
|
+
expect { NounProjectApi::Icon.new }.to raise_error(ArgumentError)
|
7
|
+
end
|
12
8
|
|
13
|
-
|
14
|
-
|
15
|
-
)
|
9
|
+
it 'accepts JSON string input' do
|
10
|
+
data = JSON.parse(Fakes::Results::ICON_VALID)
|
11
|
+
expect { NounProjectApi::Icon.new(JSON.dump(data["icon"])) }.to_not raise_error
|
16
12
|
end
|
17
13
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
14
|
+
it 'accepts JSON string input with icon encapsulation' do
|
15
|
+
expect { NounProjectApi::Icon.new(Fakes::Results::ICON_VALID) }.to_not raise_error
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'accepts hash input' do
|
19
|
+
data = JSON.parse(Fakes::Results::ICON_VALID)
|
20
|
+
expect { NounProjectApi::Icon.new(data) }.to_not raise_error
|
21
|
+
end
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
"#{NounProjectApi::API_BASE}#{NounProjectApi::Icon::API_PATH}#{id}"
|
29
|
-
).and_return(
|
30
|
-
@valid_response
|
31
|
-
)
|
23
|
+
context "exposed attributes" do
|
24
|
+
before :each do
|
25
|
+
@json = JSON.parse(Fakes::Results::ICON_VALID)
|
26
|
+
@valid_icon = NounProjectApi::Icon.new(Fakes::Results::ICON_VALID)
|
27
|
+
end
|
32
28
|
|
33
|
-
|
29
|
+
it 'public domain' do
|
30
|
+
expect(@valid_icon.public_domain?).to eq(@json["icon"]["license_description"] == "public-domain")
|
34
31
|
end
|
35
32
|
|
36
|
-
it '
|
37
|
-
|
38
|
-
|
39
|
-
:get
|
40
|
-
).with(
|
41
|
-
"#{NounProjectApi::API_BASE}#{NounProjectApi::Icon::API_PATH}#{id}"
|
42
|
-
).and_return(
|
43
|
-
@missing_response
|
44
|
-
)
|
33
|
+
it 'SVG url' do
|
34
|
+
expect(@valid_icon.svg_url).to eq(@json["icon"]["icon_url"])
|
35
|
+
end
|
45
36
|
|
46
|
-
|
37
|
+
it 'nil on missing SVG url' do
|
38
|
+
@json["icon"].delete("icon_url")
|
39
|
+
icon = NounProjectApi::Icon.new(@json)
|
40
|
+
expect(icon.svg_url).to be_nil
|
47
41
|
end
|
48
|
-
end
|
49
42
|
|
50
|
-
|
51
|
-
|
52
|
-
expect { @icon.find_by_slug(nil) }.to raise_error(ArgumentError)
|
43
|
+
it 'preview url defaults at 200 size' do
|
44
|
+
expect(@valid_icon.preview_url).to eq(@json["icon"]["preview_url"])
|
53
45
|
end
|
54
46
|
|
55
|
-
it '
|
56
|
-
|
57
|
-
|
58
|
-
:get
|
59
|
-
).with(
|
60
|
-
"#{NounProjectApi::API_BASE}#{NounProjectApi::Icon::API_PATH}#{slug}"
|
61
|
-
).and_return(
|
62
|
-
@valid_response
|
63
|
-
)
|
47
|
+
it 'preview url for 200 size' do
|
48
|
+
expect(@valid_icon.preview_url(NounProjectApi::Icon::PREVIEW_SIZE_200)).to eq(@json["icon"]["preview_url"])
|
49
|
+
end
|
64
50
|
|
65
|
-
|
51
|
+
it 'preview url for 84 size' do
|
52
|
+
expect(@valid_icon.preview_url(NounProjectApi::Icon::PREVIEW_SIZE_84)).to eq(@json["icon"]["preview_url_84"])
|
66
53
|
end
|
67
54
|
|
68
|
-
it '
|
69
|
-
|
70
|
-
|
71
|
-
:get
|
72
|
-
).with(
|
73
|
-
"#{NounProjectApi::API_BASE}#{NounProjectApi::Icon::API_PATH}#{slug}"
|
74
|
-
).and_return(
|
75
|
-
@missing_response
|
76
|
-
)
|
55
|
+
it 'preview url for 42 size' do
|
56
|
+
expect(@valid_icon.preview_url(NounProjectApi::Icon::PREVIEW_SIZE_42)).to eq(@json["icon"]["preview_url_42"])
|
57
|
+
end
|
77
58
|
|
78
|
-
|
59
|
+
it 'id as number' do
|
60
|
+
expect(@valid_icon.id).to be_a(Fixnum)
|
61
|
+
expect(@valid_icon.id).to eq(@json["icon"]["id"].to_i)
|
79
62
|
end
|
80
63
|
end
|
81
64
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
RSpec.describe NounProjectApi::
|
3
|
+
RSpec.describe NounProjectApi::IconsRetriever do
|
4
4
|
before :each do
|
5
|
-
@icons = NounProjectApi::
|
5
|
+
@icons = NounProjectApi::IconsRetriever.new(Faker::Internet.password(16), Faker::Internet.password(16))
|
6
6
|
end
|
7
7
|
|
8
8
|
context "recent uploads" do
|
@@ -16,32 +16,39 @@ RSpec.describe NounProjectApi::Icons do
|
|
16
16
|
expect(@icons.access_token).to receive(
|
17
17
|
:get
|
18
18
|
).with(
|
19
|
-
"#{NounProjectApi::API_BASE}#{NounProjectApi::
|
19
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::IconsRetriever::API_PATH}recent_uploads"
|
20
20
|
).and_return(
|
21
21
|
valid_response
|
22
22
|
)
|
23
23
|
|
24
|
-
|
24
|
+
results = @icons.recent_uploads
|
25
|
+
expect(results.size).to eq(valid_hash["recent_uploads"].size)
|
26
|
+
results.each do |icon|
|
27
|
+
expect(icon).to be_a(NounProjectApi::Icon)
|
28
|
+
end
|
25
29
|
end
|
26
30
|
|
27
31
|
it "returns the recent uploads and passes limit when passed" do
|
28
|
-
valid_hash = JSON.parse(Fakes::Results::ICONS_RECENT_VALID)
|
29
32
|
valid_response = OpenStruct.new(
|
30
33
|
body: Fakes::Results::ICONS_RECENT_VALID,
|
31
34
|
code: '200'
|
32
35
|
)
|
33
36
|
|
34
|
-
limit =
|
37
|
+
limit = 3
|
35
38
|
|
36
39
|
expect(@icons.access_token).to receive(
|
37
40
|
:get
|
38
41
|
).with(
|
39
|
-
"#{NounProjectApi::API_BASE}#{NounProjectApi::
|
42
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::IconsRetriever::API_PATH}recent_uploads?limit=#{limit}&"
|
40
43
|
).and_return(
|
41
44
|
valid_response
|
42
45
|
)
|
43
46
|
|
44
|
-
|
47
|
+
results = @icons.recent_uploads(limit)
|
48
|
+
expect(results.size).to eq(limit)
|
49
|
+
results.each do |icon|
|
50
|
+
expect(icon).to be_a(NounProjectApi::Icon)
|
51
|
+
end
|
45
52
|
end
|
46
53
|
end
|
47
54
|
|
@@ -61,12 +68,16 @@ RSpec.describe NounProjectApi::Icons do
|
|
61
68
|
expect(@icons.access_token).to receive(
|
62
69
|
:get
|
63
70
|
).with(
|
64
|
-
"#{NounProjectApi::API_BASE}#{NounProjectApi::
|
71
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::IconsRetriever::API_PATH}#{URI::encode(term)}"
|
65
72
|
).and_return(
|
66
73
|
valid_response
|
67
74
|
)
|
68
75
|
|
69
|
-
|
76
|
+
results = @icons.find(term)
|
77
|
+
expect(results.size).to eq(valid_hash["icons"].size)
|
78
|
+
results.each do |icon|
|
79
|
+
expect(icon).to be_a(NounProjectApi::Icon)
|
80
|
+
end
|
70
81
|
end
|
71
82
|
|
72
83
|
it 'returns a proper result with a correct phrase' do
|
@@ -80,32 +91,39 @@ RSpec.describe NounProjectApi::Icons do
|
|
80
91
|
expect(@icons.access_token).to receive(
|
81
92
|
:get
|
82
93
|
).with(
|
83
|
-
"#{NounProjectApi::API_BASE}#{NounProjectApi::
|
94
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::IconsRetriever::API_PATH}#{URI::encode(term)}"
|
84
95
|
).and_return(
|
85
96
|
valid_response
|
86
97
|
)
|
87
98
|
|
88
|
-
|
99
|
+
results = @icons.find(term)
|
100
|
+
expect(results.size).to eq(valid_hash["icons"].size)
|
101
|
+
results.each do |icon|
|
102
|
+
expect(icon).to be_a(NounProjectApi::Icon)
|
103
|
+
end
|
89
104
|
end
|
90
105
|
|
91
106
|
it 'returns a proper result with a correct phrase and passes along the args' do
|
92
|
-
valid_hash = JSON.parse(Fakes::Results::ICONS_VALID)
|
93
107
|
valid_response = OpenStruct.new(
|
94
108
|
body: Fakes::Results::ICONS_VALID,
|
95
109
|
code: '200'
|
96
110
|
)
|
97
111
|
|
98
112
|
term = 'some search'
|
99
|
-
limit =
|
113
|
+
limit = 4
|
100
114
|
expect(@icons.access_token).to receive(
|
101
115
|
:get
|
102
116
|
).with(
|
103
|
-
"#{NounProjectApi::API_BASE}#{NounProjectApi::
|
117
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::IconsRetriever::API_PATH}#{URI::encode(term)}?limit=#{limit}&"
|
104
118
|
).and_return(
|
105
119
|
valid_response
|
106
120
|
)
|
107
121
|
|
108
|
-
|
122
|
+
results = @icons.find(term, limit)
|
123
|
+
expect(results.size).to eq(limit)
|
124
|
+
results.each do |icon|
|
125
|
+
expect(icon).to be_a(NounProjectApi::Icon)
|
126
|
+
end
|
109
127
|
end
|
110
128
|
|
111
129
|
it 'returns an empty array for no result' do
|
@@ -117,12 +135,13 @@ RSpec.describe NounProjectApi::Icons do
|
|
117
135
|
expect(@icons.access_token).to receive(
|
118
136
|
:get
|
119
137
|
).with(
|
120
|
-
"#{NounProjectApi::API_BASE}#{NounProjectApi::
|
138
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::IconsRetriever::API_PATH}#{URI::encode(term)}"
|
121
139
|
).and_return(
|
122
140
|
missing_response
|
123
141
|
)
|
124
142
|
|
125
|
-
|
143
|
+
results = @icons.find(term)
|
144
|
+
expect(results.size).to eq(0)
|
126
145
|
end
|
127
146
|
end
|
128
147
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: noun-project-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nadav Shatz
|
@@ -81,10 +81,12 @@ files:
|
|
81
81
|
- Rakefile
|
82
82
|
- lib/noun-project-api.rb
|
83
83
|
- lib/noun-project-api/icon.rb
|
84
|
-
- lib/noun-project-api/
|
84
|
+
- lib/noun-project-api/icon_retriever.rb
|
85
|
+
- lib/noun-project-api/icons_retriever.rb
|
85
86
|
- lib/noun-project-api/retriever.rb
|
87
|
+
- spec/lib/noun-project-api/icon_retriever_spec.rb
|
86
88
|
- spec/lib/noun-project-api/icon_spec.rb
|
87
|
-
- spec/lib/noun-project-api/
|
89
|
+
- spec/lib/noun-project-api/icons_retriever_spec.rb
|
88
90
|
- spec/lib/noun-project-api/retriever_spec.rb
|
89
91
|
- spec/spec_helper.rb
|
90
92
|
- spec/support/fakes.rb
|
@@ -113,8 +115,9 @@ signing_key:
|
|
113
115
|
specification_version: 4
|
114
116
|
summary: An API wrapper for The Noun Project API's
|
115
117
|
test_files:
|
118
|
+
- spec/lib/noun-project-api/icon_retriever_spec.rb
|
116
119
|
- spec/lib/noun-project-api/icon_spec.rb
|
117
|
-
- spec/lib/noun-project-api/
|
120
|
+
- spec/lib/noun-project-api/icons_retriever_spec.rb
|
118
121
|
- spec/lib/noun-project-api/retriever_spec.rb
|
119
122
|
- spec/spec_helper.rb
|
120
123
|
- spec/support/fakes.rb
|