global_registry 1.0.8 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -0
- data/lib/global_registry/base.rb +75 -30
- data/lib/global_registry/version.rb +1 -1
- data/spec/base_spec.rb +26 -0
- data/spec/entity_spec.rb +47 -0
- data/spec/spec_helper.rb +16 -0
- metadata +12 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64ce57dbf8a816d806e978b1bc5bcd31c84ea387
|
4
|
+
data.tar.gz: 405831e2b00acd2096977138a5184569877b82d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d9eb988e414bf32f4e5a2da3f312a66ec0e639d493a625b5f419d7e922d18049125c17079c9d887c42acb2a2932cf279e3340520a328de18cb7de565779d758
|
7
|
+
data.tar.gz: 6ab0a32677502ddf3a49f03a31b6ddbac9fbe5a8da6b85b61fcd350eebcf6ddf8e7873cbca6b0d1fb951ff98007916d1d48246c0b754b7d1f8b8e2f06999d9c3
|
data/.travis.yml
ADDED
data/lib/global_registry/base.rb
CHANGED
@@ -4,63 +4,109 @@ require 'retryable'
|
|
4
4
|
|
5
5
|
module GlobalRegistry
|
6
6
|
class Base
|
7
|
+
def initialize(args = {})
|
8
|
+
@base_url = args[:base_url]
|
9
|
+
@access_token = args[:access_token]
|
10
|
+
@xff = args[:xff]
|
11
|
+
end
|
7
12
|
|
8
|
-
def self.find(id, params = {})
|
9
|
-
|
13
|
+
def self.find(id, params = {}, headers = {})
|
14
|
+
new.find(id, params, headers)
|
15
|
+
end
|
16
|
+
def find(id, params = {}, headers = {})
|
17
|
+
request(:get, params, path_with_id(id), headers)
|
10
18
|
end
|
11
19
|
|
12
|
-
def self.get(params = {})
|
13
|
-
|
20
|
+
def self.get(params = {}, headers = {})
|
21
|
+
new.get(params, headers)
|
22
|
+
end
|
23
|
+
def get(params = {}, headers = {})
|
24
|
+
request(:get, params, nil, headers)
|
14
25
|
end
|
15
26
|
|
16
|
-
def self.post(params = {})
|
17
|
-
|
27
|
+
def self.post(params = {}, headers = {})
|
28
|
+
new.post(params, headers)
|
29
|
+
end
|
30
|
+
def post(params = {}, headers = {})
|
31
|
+
request(:post, params, nil, headers)
|
18
32
|
end
|
19
33
|
|
20
|
-
def self.put(id, params = {})
|
21
|
-
|
34
|
+
def self.put(id, params = {}, headers = {})
|
35
|
+
new.put(id, params, headers)
|
36
|
+
end
|
37
|
+
def put(id, params = {}, headers = {})
|
38
|
+
request(:put, params, path_with_id(id), headers)
|
22
39
|
end
|
23
40
|
|
24
|
-
def self.delete(id)
|
25
|
-
|
41
|
+
def self.delete(id, headers = {})
|
42
|
+
new.delete(id, headers)
|
43
|
+
end
|
44
|
+
def delete(id, headers = {})
|
45
|
+
request(:delete, {}, path_with_id(id), headers)
|
26
46
|
end
|
27
47
|
|
28
|
-
def self.delete_or_ignore(id)
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
48
|
+
def self.delete_or_ignore(id, headers = {})
|
49
|
+
delete(id, headers)
|
50
|
+
rescue RestClient::Exception => e
|
51
|
+
raise unless e.response.code.to_i == 404
|
52
|
+
end
|
53
|
+
def delete_or_ignore(id, headers = {})
|
54
|
+
delete(id, headers)
|
55
|
+
rescue RestClient::Exception => e
|
56
|
+
raise unless e.response.code.to_i == 404
|
36
57
|
end
|
37
58
|
|
38
59
|
|
39
|
-
def self.request(method, params, path = nil)
|
40
|
-
|
60
|
+
def self.request(method, params, path = nil, headers = {})
|
61
|
+
new.request(method, params, path, headers)
|
62
|
+
end
|
63
|
+
|
64
|
+
def request(method, params, path = nil, headers = {})
|
65
|
+
raise 'You need to configure GlobalRegistry with your access_token.' unless access_token
|
41
66
|
|
42
67
|
path ||= default_path
|
43
|
-
url =
|
68
|
+
url = base_url
|
44
69
|
url += '/' unless url.last == '/'
|
45
70
|
url += path
|
46
71
|
|
47
72
|
case method
|
48
73
|
when :post
|
49
|
-
|
74
|
+
post_headers = default_headers.merge(content_type: :json, timeout: -1).merge(headers)
|
75
|
+
RestClient.post(url, params.to_json, post_headers) { |response, request, result, &block|
|
50
76
|
handle_response(response, request, result)
|
51
77
|
}
|
52
78
|
when :put
|
53
|
-
|
79
|
+
put_headers = default_headers.merge(content_type: :json, timeout: -1).merge(headers)
|
80
|
+
RestClient.put(url, params.to_json, put_headers) { |response, request, result, &block|
|
54
81
|
handle_response(response, request, result)
|
55
82
|
}
|
56
83
|
else
|
57
|
-
|
84
|
+
get_args = { method: method, url: url, timeout: -1,
|
85
|
+
headers: default_headers.merge(params: params).merge(headers)
|
86
|
+
}
|
87
|
+
RestClient::Request.execute(get_args) { |response, request, result, &block|
|
58
88
|
handle_response(response, request, result)
|
59
89
|
}
|
60
90
|
end
|
61
91
|
end
|
62
92
|
|
63
|
-
def
|
93
|
+
def default_path
|
94
|
+
self.class.to_s.split('::').last.underscore.pluralize
|
95
|
+
end
|
96
|
+
|
97
|
+
def path_with_id(id)
|
98
|
+
"#{default_path}/#{id}"
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def default_headers
|
104
|
+
headers = { authorization: "Bearer #{access_token}", accept: :json }
|
105
|
+
headers = headers.merge('X-Forwarded-For': @xff) if @xff.present?
|
106
|
+
headers
|
107
|
+
end
|
108
|
+
|
109
|
+
def handle_response(response, request, result)
|
64
110
|
case response.code
|
65
111
|
when 200..299
|
66
112
|
Oj.load(response)
|
@@ -77,14 +123,13 @@ module GlobalRegistry
|
|
77
123
|
end
|
78
124
|
end
|
79
125
|
|
80
|
-
def
|
81
|
-
|
126
|
+
def base_url
|
127
|
+
@base_url || GlobalRegistry.base_url
|
82
128
|
end
|
83
129
|
|
84
|
-
def
|
85
|
-
|
130
|
+
def access_token
|
131
|
+
@access_token || GlobalRegistry.access_token
|
86
132
|
end
|
87
|
-
|
88
133
|
end
|
89
134
|
end
|
90
135
|
|
data/spec/base_spec.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Base' do
|
4
|
+
describe '#get' do
|
5
|
+
before do
|
6
|
+
stub_request(:get, 'http://google.com/bases')
|
7
|
+
.with(headers: {authorization: 'Bearer asdf'})
|
8
|
+
.to_return(:body => {a: 'b'}.to_json)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'finds something' do
|
12
|
+
response = GlobalRegistry::Base.get
|
13
|
+
expect(response).to be_a Hash
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'can be configured' do
|
17
|
+
@custom_gr_url = stub_request(:get, 'http://cru.org/bases')
|
18
|
+
.with(headers: { authorization: 'Bearer qwer',
|
19
|
+
'X-Forwarded-For': '203.0.113.7' })
|
20
|
+
.to_return(:body => {a: 'b'}.to_json)
|
21
|
+
gr = GlobalRegistry::Base.new(access_token: 'qwer', base_url: 'cru.org', xff: '203.0.113.7')
|
22
|
+
expect(gr.get).to be_a Hash
|
23
|
+
expect(@custom_gr_url).to have_been_requested
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/spec/entity_spec.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Entity' do
|
4
|
+
describe '#get' do
|
5
|
+
before do
|
6
|
+
stub_request(:get, 'http://google.com/entities')
|
7
|
+
.to_return(:body => {a: 'b'}.to_json)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'finds something' do
|
11
|
+
response = GlobalRegistry::Entity.get
|
12
|
+
expect(response).to be_a Hash
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'can be configured' do
|
16
|
+
@custom_gr_url = stub_request(:get, 'http://cru.org/entities')
|
17
|
+
.to_return(:body => {a: 'b'}.to_json)
|
18
|
+
gr = GlobalRegistry::Entity.new(base_url: 'cru.org')
|
19
|
+
expect(gr.get).to be_a Hash
|
20
|
+
expect(@custom_gr_url).to have_been_requested
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#put' do
|
25
|
+
before do
|
26
|
+
stub_request(:get, 'http://google.com/entities')
|
27
|
+
.to_return(:body => {a: 'b'}.to_json)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'sends data' do
|
31
|
+
response = GlobalRegistry::Entity.get
|
32
|
+
expect(response).to be_a Hash
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#find' do
|
37
|
+
before do
|
38
|
+
stub_request(:get, 'http://google.com/entities/asdf')
|
39
|
+
.to_return(:body => {a: 'b'}.to_json)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'sends data' do
|
43
|
+
response = GlobalRegistry::Entity.find('asdf')
|
44
|
+
expect(response).to be_a Hash
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
Bundler.setup
|
3
|
+
|
4
|
+
require 'global_registry'
|
5
|
+
require 'active_support/core_ext/string'
|
6
|
+
require 'webmock/rspec'
|
7
|
+
|
8
|
+
RSpec.configure do |_config|
|
9
|
+
# some config here
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
GlobalRegistry.configure do |config|
|
14
|
+
config.access_token = 'asdf'
|
15
|
+
config.base_url = 'google.com/'
|
16
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: global_registry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Starcher
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -88,6 +88,7 @@ extensions: []
|
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
91
92
|
- Gemfile
|
92
93
|
- LICENSE
|
93
94
|
- README.md
|
@@ -104,6 +105,9 @@ files:
|
|
104
105
|
- lib/global_registry/relationship_type.rb
|
105
106
|
- lib/global_registry/system.rb
|
106
107
|
- lib/global_registry/version.rb
|
108
|
+
- spec/base_spec.rb
|
109
|
+
- spec/entity_spec.rb
|
110
|
+
- spec/spec_helper.rb
|
107
111
|
homepage: ''
|
108
112
|
licenses: []
|
109
113
|
metadata: {}
|
@@ -123,8 +127,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
127
|
version: '0'
|
124
128
|
requirements: []
|
125
129
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.4.5
|
130
|
+
rubygems_version: 2.4.5.1
|
127
131
|
signing_key:
|
128
132
|
specification_version: 4
|
129
133
|
summary: Push and pull data from the Global Registry
|
130
|
-
test_files:
|
134
|
+
test_files:
|
135
|
+
- spec/base_spec.rb
|
136
|
+
- spec/entity_spec.rb
|
137
|
+
- spec/spec_helper.rb
|
138
|
+
has_rdoc:
|