frenchy 0.5.2 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/frenchy.rb +17 -0
- data/lib/frenchy/client.rb +6 -2
- data/lib/frenchy/version.rb +1 -1
- data/spec/lib/frenchy_spec.rb +13 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b997d6d2702753a71cb04388c829b32cc68b53b1
|
4
|
+
data.tar.gz: 238f61662629cc797ffed1845a47598384539247
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9e395f9728e47492a1873a9b3813b7f673c05d0dbb4ec6df929808e557fd14bcd7947159c1159001ea7b8f52f162b297e72ee7d139025ee748ec7af30422524
|
7
|
+
data.tar.gz: cb5c37306cb4b97b1bc51fdf0e35a72762c6864768f3747e8734d118e5eabd82eb28faa49d2fa0b75200cd73a766586b8dc7afb9b744dd6b74e09e9fa21d06d0
|
data/lib/frenchy.rb
CHANGED
@@ -14,6 +14,8 @@ require "frenchy/version"
|
|
14
14
|
module Frenchy
|
15
15
|
class_eval do
|
16
16
|
@services = {}
|
17
|
+
@content_types = {}
|
18
|
+
@content_type_accept = ""
|
17
19
|
end
|
18
20
|
|
19
21
|
def self.register_service(name, options={})
|
@@ -23,4 +25,19 @@ module Frenchy
|
|
23
25
|
def self.find_service(name)
|
24
26
|
@services[name.to_s] || raise(Frenchy::Error, "No service '#{name}' registered")
|
25
27
|
end
|
28
|
+
|
29
|
+
def self.register_content_type(name, &block)
|
30
|
+
@content_types[name] = block
|
31
|
+
@content_type_accept = @content_types.keys.join(", ")
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.find_content_type_handler(name)
|
35
|
+
@content_types[name] || raise(Frenchy::Error, "No content type '#{name}' registered")
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.accept_header
|
39
|
+
@content_type_accept
|
40
|
+
end
|
26
41
|
end
|
42
|
+
|
43
|
+
Frenchy.register_content_type("application/json") {|x| JSON.parse(x) }
|
data/lib/frenchy/client.rb
CHANGED
@@ -51,7 +51,7 @@ module Frenchy
|
|
51
51
|
body = nil
|
52
52
|
headers = {
|
53
53
|
"User-Agent" => "Frenchy/#{Frenchy::VERSION}",
|
54
|
-
"Accept" =>
|
54
|
+
"Accept" => Frenchy.accept_header,
|
55
55
|
}.merge(@headers)
|
56
56
|
|
57
57
|
# Set the URI path
|
@@ -103,7 +103,7 @@ module Frenchy
|
|
103
103
|
when 200...399
|
104
104
|
# Positive responses are expected to return JSON
|
105
105
|
begin
|
106
|
-
|
106
|
+
decode_response(resp)
|
107
107
|
rescue => ex
|
108
108
|
raise Frenchy::InvalidResponse.new(ex, reqinfo, resp)
|
109
109
|
end
|
@@ -125,5 +125,9 @@ module Frenchy
|
|
125
125
|
def perform_request(http, req)
|
126
126
|
http.request(req)
|
127
127
|
end
|
128
|
+
|
129
|
+
def decode_response(resp)
|
130
|
+
Frenchy.find_content_type_handler(resp["Content-Type"]).call(resp.body)
|
131
|
+
end
|
128
132
|
end
|
129
133
|
end
|
data/lib/frenchy/version.rb
CHANGED
data/spec/lib/frenchy_spec.rb
CHANGED
@@ -15,4 +15,16 @@ describe Frenchy do
|
|
15
15
|
expect{Frenchy.find_service("nonexistent")}.to raise_error(Frenchy::Error)
|
16
16
|
end
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
|
+
describe ".register_content_type" do
|
20
|
+
it "adds the content type to the accept header" do
|
21
|
+
expect(Frenchy.accept_header).to eql("application/json")
|
22
|
+
Frenchy.register_content_type("application/other") do |x|
|
23
|
+
5
|
24
|
+
end
|
25
|
+
expect(Frenchy.accept_header).to eql("application/json, application/other")
|
26
|
+
expect(Frenchy.find_content_type_handler("application/other").call(nil)).to eql(5)
|
27
|
+
expect{Frenchy.find_content_type_handler("nonexistent")}.to raise_error(Frenchy::Error)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: frenchy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Coene
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|