noun-project-api 0.1.1 → 0.2.0
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 +6 -0
- data/lib/noun-project-api/connection.rb +14 -0
- data/lib/noun-project-api/icon.rb +7 -6
- data/lib/noun-project-api/icon_retriever.rb +6 -4
- data/lib/noun-project-api/icons_retriever.rb +22 -14
- data/lib/noun-project-api/reporter.rb +16 -0
- data/lib/noun-project-api/retriever.rb +2 -10
- data/spec/lib/noun-project-api/reporter_spec.rb +93 -0
- data/spec/support/fakes.rb +12 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0dcbb2c72c7cbc5df96879c43521e83d3016f70c
|
4
|
+
data.tar.gz: 7f6855dcc4b3f50824c83354401b7b3895a50cc5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60016d2253fe5e65ce2aec847d0c26d76e348baecf6f2b7c53e33eaca44bb97a2824d3d0abe495be68755bffc1ff394b2b1104dc132c19676d8f36d79243218a
|
7
|
+
data.tar.gz: 1951a91ccdf5b4e83be25c3612a84beb987a7af3d2de524d916eef2d142724cd76ea0262c35fd6de484c8380c403291051ac3b243d6f0f079a12e558f3bfb909
|
data/lib/noun-project-api.rb
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
require 'oauth'
|
2
2
|
require 'json'
|
3
|
+
require 'noun-project-api/connection'
|
3
4
|
require 'noun-project-api/icon_retriever'
|
5
|
+
require 'noun-project-api/reporter'
|
4
6
|
require 'noun-project-api/icons_retriever'
|
5
7
|
require 'noun-project-api/icon'
|
6
8
|
|
9
|
+
# Top level name space for the entire Gem.
|
7
10
|
module NounProjectApi
|
11
|
+
API_BASE = 'http://api.thenounproject.com'
|
12
|
+
|
8
13
|
def self.configuration
|
9
14
|
@configuration ||= Configuration.new
|
10
15
|
end
|
@@ -14,6 +19,7 @@ module NounProjectApi
|
|
14
19
|
yield(configuration) if block_given?
|
15
20
|
end
|
16
21
|
|
22
|
+
# Main configuration class.
|
17
23
|
class Configuration
|
18
24
|
attr_accessor :public_domain
|
19
25
|
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module NounProjectApi
|
2
|
+
# Basic connection methods and setup.
|
3
|
+
module Connection
|
4
|
+
attr_accessor :token, :secret, :access_token
|
5
|
+
|
6
|
+
def initialize(token, secret)
|
7
|
+
@token = token
|
8
|
+
@secret = secret
|
9
|
+
fail(ArgumentError, 'Missing token or secret') unless @token && @secret
|
10
|
+
|
11
|
+
@access_token = OAuth::AccessToken.new(OAuth::Consumer.new(token, secret))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -1,35 +1,36 @@
|
|
1
1
|
module NounProjectApi
|
2
|
+
# A single Icon as an abstracted ruby object.
|
2
3
|
class Icon
|
3
4
|
PREVIEW_SIZE_200 = 200
|
4
5
|
PREVIEW_SIZE_42 = 42
|
5
6
|
PREVIEW_SIZE_84 = 84
|
6
7
|
|
7
|
-
PUBLIC_DOMAIN_LICENSE =
|
8
|
+
PUBLIC_DOMAIN_LICENSE = 'public-domain'
|
8
9
|
|
9
10
|
attr_accessor :original_hash
|
10
11
|
|
11
12
|
def initialize(origin)
|
12
13
|
origin = JSON.parse(origin) if origin.is_a? String
|
13
|
-
origin = origin.delete(
|
14
|
+
origin = origin.delete('icon') if origin.key? 'icon'
|
14
15
|
|
15
16
|
@original_hash = origin
|
16
17
|
end
|
17
18
|
|
18
19
|
def id
|
19
|
-
original_hash[
|
20
|
+
original_hash['id'].to_i
|
20
21
|
end
|
21
22
|
|
22
23
|
def public_domain?
|
23
|
-
original_hash[
|
24
|
+
original_hash['license_description'] == PUBLIC_DOMAIN_LICENSE
|
24
25
|
end
|
25
26
|
|
26
27
|
def svg_url
|
27
|
-
original_hash[
|
28
|
+
original_hash['icon_url']
|
28
29
|
end
|
29
30
|
|
30
31
|
def preview_url(size = PREVIEW_SIZE_200)
|
31
32
|
if size == PREVIEW_SIZE_200
|
32
|
-
original_hash[
|
33
|
+
original_hash['preview_url']
|
33
34
|
else
|
34
35
|
original_hash["preview_url_#{size}"]
|
35
36
|
end
|
@@ -1,14 +1,16 @@
|
|
1
1
|
require 'noun-project-api/retriever'
|
2
2
|
|
3
3
|
module NounProjectApi
|
4
|
+
# Retrieve an icon.
|
4
5
|
class IconRetriever < Retriever
|
5
|
-
API_PATH =
|
6
|
+
API_PATH = '/icon/'
|
6
7
|
|
8
|
+
# Find an icon based on it's id.
|
7
9
|
def find(id)
|
8
|
-
|
10
|
+
fail(ArgumentError, 'Missing id/slug') unless id
|
9
11
|
|
10
|
-
result =
|
11
|
-
|
12
|
+
result = access_token.get("#{API_BASE}#{API_PATH}#{id}")
|
13
|
+
fail(ArgumentError, 'Bad request') unless result.code == '200'
|
12
14
|
|
13
15
|
Icon.new(result.body)
|
14
16
|
end
|
@@ -1,32 +1,40 @@
|
|
1
1
|
require 'noun-project-api/retriever'
|
2
2
|
|
3
3
|
module NounProjectApi
|
4
|
+
# Retrieve icons.
|
4
5
|
class IconsRetriever < Retriever
|
5
|
-
API_PATH =
|
6
|
+
API_PATH = '/icons/'
|
6
7
|
|
8
|
+
# Finds multiple icons based on the term
|
9
|
+
# * term - search term
|
10
|
+
# * limit - limit the amount of results
|
11
|
+
# * offset - offset the results
|
12
|
+
# * page - page number
|
7
13
|
def find(term, limit = nil, offset = nil, page = nil)
|
8
|
-
|
14
|
+
fail(ArgumentError, 'Missing search term') unless term
|
9
15
|
|
10
|
-
search = OAuth::Helper
|
16
|
+
search = OAuth::Helper.escape(term)
|
11
17
|
search += "?limit_to_public_domain=#{NounProjectApi.configuration.public_domain ? 1 : 0}"
|
12
18
|
|
13
|
-
args = {
|
14
|
-
if args.size > 0
|
15
|
-
args.each { |k, v| search += "&#{k}=#{v}" }
|
16
|
-
end
|
19
|
+
args = { 'limit' => limit, 'offset' => offset, 'page' => page }.reject { |_, v| v.nil? }
|
20
|
+
args.each { |k, v| search += "&#{k}=#{v}" } if args.size > 0
|
17
21
|
|
18
|
-
result =
|
19
|
-
|
22
|
+
result = access_token.get("#{API_BASE}#{API_PATH}#{search}")
|
23
|
+
fail(ArgumentError, 'Bad request') unless %w(200 404).include? result.code
|
20
24
|
|
21
25
|
if result.code == '200'
|
22
|
-
JSON.parse(result.body)[
|
26
|
+
JSON.parse(result.body)['icons'].map { |icon| Icon.new(icon) }
|
23
27
|
else
|
24
28
|
[]
|
25
29
|
end
|
26
30
|
end
|
27
31
|
|
32
|
+
# List recent uploads
|
33
|
+
# * limit - limit the amount of results
|
34
|
+
# * offset - offset the results
|
35
|
+
# * page - page number
|
28
36
|
def recent_uploads(limit = nil, offset = nil, page = nil)
|
29
|
-
args = {
|
37
|
+
args = { 'limit' => limit, 'offset' => offset, 'page' => page }.reject { |k, v| v.nil? }
|
30
38
|
if args.size > 0
|
31
39
|
search = '?'
|
32
40
|
args.each { |k, v| search += "#{k}=#{v}&" }
|
@@ -34,10 +42,10 @@ module NounProjectApi
|
|
34
42
|
search = ''
|
35
43
|
end
|
36
44
|
|
37
|
-
result =
|
38
|
-
|
45
|
+
result = access_token.get("#{API_BASE}#{API_PATH}recent_uploads#{search}")
|
46
|
+
fail(ArgumentError, 'Bad request') unless result.code == '200'
|
39
47
|
|
40
|
-
JSON.parse(result.body)[
|
48
|
+
JSON.parse(result.body)['recent_uploads'].map { |icon| Icon.new(icon) }
|
41
49
|
end
|
42
50
|
end
|
43
51
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module NounProjectApi
|
2
|
+
# Main class to hold reporting actions back to the Noun Project.
|
3
|
+
class Reporter
|
4
|
+
include Connection
|
5
|
+
|
6
|
+
API_PATH = '/notify/publish'
|
7
|
+
|
8
|
+
def report_used(ids)
|
9
|
+
ids = [ids] if ids.is_a?(String) || ids.is_a?(Fixnum)
|
10
|
+
fail(ArgumentError, 'Missing ids') if ids.nil? || ids.empty?
|
11
|
+
|
12
|
+
result = access_token.post("#{API_BASE}#{API_PATH}", icons: ids.join(','))
|
13
|
+
result.code == '200'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -1,14 +1,6 @@
|
|
1
1
|
module NounProjectApi
|
2
|
-
|
3
|
-
|
2
|
+
# A base class for different retriever classes.
|
4
3
|
class Retriever
|
5
|
-
|
6
|
-
def initialize(token, secret)
|
7
|
-
@token = token
|
8
|
-
@secret = secret
|
9
|
-
raise ArgumentError.new('Missing token or secret') unless @token && @secret
|
10
|
-
|
11
|
-
@access_token = OAuth::AccessToken.new(OAuth::Consumer.new(token, secret))
|
12
|
-
end
|
4
|
+
include Connection
|
13
5
|
end
|
14
6
|
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe NounProjectApi::Reporter do
|
4
|
+
it 'raises an error when initialized without token' do
|
5
|
+
expect { NounProjectApi::Reporter.new(nil, Faker::Internet.password(16)) }.to raise_error(ArgumentError)
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'raises an error when initialized without secret' do
|
9
|
+
expect { NounProjectApi::Reporter.new(Faker::Internet.password(16), nil) }.to raise_error(ArgumentError)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'initializes the values properly' do
|
13
|
+
token = Faker::Internet.password(16)
|
14
|
+
secret = Faker::Internet.password(16)
|
15
|
+
reporter = NounProjectApi::Reporter.new(token, secret)
|
16
|
+
|
17
|
+
expect(reporter.token).to eq(token)
|
18
|
+
expect(reporter.secret).to eq(secret)
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'reports ids usage' do
|
22
|
+
before :each do
|
23
|
+
token = Faker::Internet.password(16)
|
24
|
+
secret = Faker::Internet.password(16)
|
25
|
+
@reporter = NounProjectApi::Reporter.new(token, secret)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'reports a singular id' do
|
29
|
+
valid_response = OpenStruct.new(
|
30
|
+
body: Fakes::Results::REPORTED_ONE,
|
31
|
+
code: '200'
|
32
|
+
)
|
33
|
+
|
34
|
+
id = 122
|
35
|
+
|
36
|
+
expect(@reporter.access_token).to receive(
|
37
|
+
:post
|
38
|
+
).with(
|
39
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::Reporter::API_PATH}",
|
40
|
+
icons: id.to_s
|
41
|
+
).and_return(
|
42
|
+
valid_response
|
43
|
+
)
|
44
|
+
|
45
|
+
result = @reporter.report_used(id)
|
46
|
+
expect(result).to be true
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'reports a singular id for a string' do
|
50
|
+
valid_response = OpenStruct.new(
|
51
|
+
body: Fakes::Results::REPORTED_ONE,
|
52
|
+
code: '200'
|
53
|
+
)
|
54
|
+
|
55
|
+
id = '122'
|
56
|
+
|
57
|
+
expect(@reporter.access_token).to receive(
|
58
|
+
:post
|
59
|
+
).with(
|
60
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::Reporter::API_PATH}",
|
61
|
+
icons: id
|
62
|
+
).and_return(
|
63
|
+
valid_response
|
64
|
+
)
|
65
|
+
|
66
|
+
result = @reporter.report_used(id)
|
67
|
+
expect(result).to be true
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
it 'reports multiple ids' do
|
73
|
+
valid_response = OpenStruct.new(
|
74
|
+
body: Fakes::Results::REPORTED_ONE,
|
75
|
+
code: '200'
|
76
|
+
)
|
77
|
+
|
78
|
+
ids = [122, 4541, 342_11, 4352]
|
79
|
+
|
80
|
+
expect(@reporter.access_token).to receive(
|
81
|
+
:post
|
82
|
+
).with(
|
83
|
+
"#{NounProjectApi::API_BASE}#{NounProjectApi::Reporter::API_PATH}",
|
84
|
+
icons: ids.join(',')
|
85
|
+
).and_return(
|
86
|
+
valid_response
|
87
|
+
)
|
88
|
+
|
89
|
+
result = @reporter.report_used(ids)
|
90
|
+
expect(result).to be true
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
data/spec/support/fakes.rb
CHANGED
@@ -296,5 +296,17 @@ module Fakes
|
|
296
296
|
}
|
297
297
|
]
|
298
298
|
}'''
|
299
|
+
|
300
|
+
REPORTED_ONE = '''
|
301
|
+
{
|
302
|
+
"licenses_consumed": 1,
|
303
|
+
"result": "success"
|
304
|
+
}'''
|
305
|
+
|
306
|
+
REPORTED_THREE = '''
|
307
|
+
{
|
308
|
+
"licenses_consumed": 1,
|
309
|
+
"result": "success"
|
310
|
+
}'''
|
299
311
|
end
|
300
312
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nadav Shatz
|
@@ -94,13 +94,16 @@ extra_rdoc_files: []
|
|
94
94
|
files:
|
95
95
|
- Rakefile
|
96
96
|
- lib/noun-project-api.rb
|
97
|
+
- lib/noun-project-api/connection.rb
|
97
98
|
- lib/noun-project-api/icon.rb
|
98
99
|
- lib/noun-project-api/icon_retriever.rb
|
99
100
|
- lib/noun-project-api/icons_retriever.rb
|
101
|
+
- lib/noun-project-api/reporter.rb
|
100
102
|
- lib/noun-project-api/retriever.rb
|
101
103
|
- spec/lib/noun-project-api/icon_retriever_spec.rb
|
102
104
|
- spec/lib/noun-project-api/icon_spec.rb
|
103
105
|
- spec/lib/noun-project-api/icons_retriever_spec.rb
|
106
|
+
- spec/lib/noun-project-api/reporter_spec.rb
|
104
107
|
- spec/lib/noun-project-api/retriever_spec.rb
|
105
108
|
- spec/lib/nount_project_api_spec.rb
|
106
109
|
- spec/spec_helper.rb
|
@@ -125,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
128
|
version: '0'
|
126
129
|
requirements: []
|
127
130
|
rubyforge_project:
|
128
|
-
rubygems_version: 2.
|
131
|
+
rubygems_version: 2.4.5
|
129
132
|
signing_key:
|
130
133
|
specification_version: 4
|
131
134
|
summary: An API wrapper for The Noun Project API's
|
@@ -133,6 +136,7 @@ test_files:
|
|
133
136
|
- spec/lib/noun-project-api/icon_retriever_spec.rb
|
134
137
|
- spec/lib/noun-project-api/icon_spec.rb
|
135
138
|
- spec/lib/noun-project-api/icons_retriever_spec.rb
|
139
|
+
- spec/lib/noun-project-api/reporter_spec.rb
|
136
140
|
- spec/lib/noun-project-api/retriever_spec.rb
|
137
141
|
- spec/lib/nount_project_api_spec.rb
|
138
142
|
- spec/spec_helper.rb
|