frenchy 0.5.2 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aaf4ee2e3ddd7a66d40b81fc8f792e6f06dad047
4
- data.tar.gz: 25804251db9bf9cd05427f6f152e035bf3d43b08
3
+ metadata.gz: b997d6d2702753a71cb04388c829b32cc68b53b1
4
+ data.tar.gz: 238f61662629cc797ffed1845a47598384539247
5
5
  SHA512:
6
- metadata.gz: c7b03b5adda247d60140a9e8bcf44216fed3dbceb7f4e7a9f27e210f05fb2aad3c026b1b6cfea46af8248cc890d616115db4b1d5e6bf8bda605d43a0ef4e1419
7
- data.tar.gz: 7c7710e2b0eab498159b850e76d3856152dd8f129e39d132ae5fb0823c0d380053d3378e4ec0df39583ca1529bb578253d909ffe7ccce25cd6103db9baea7326
6
+ metadata.gz: b9e395f9728e47492a1873a9b3813b7f673c05d0dbb4ec6df929808e557fd14bcd7947159c1159001ea7b8f52f162b297e72ee7d139025ee748ec7af30422524
7
+ data.tar.gz: cb5c37306cb4b97b1bc51fdf0e35a72762c6864768f3747e8734d118e5eabd82eb28faa49d2fa0b75200cd73a766586b8dc7afb9b744dd6b74e09e9fa21d06d0
@@ -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) }
@@ -51,7 +51,7 @@ module Frenchy
51
51
  body = nil
52
52
  headers = {
53
53
  "User-Agent" => "Frenchy/#{Frenchy::VERSION}",
54
- "Accept" => "application/json",
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
- JSON.parse(resp.body)
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
@@ -1,3 +1,3 @@
1
1
  module Frenchy
2
- VERSION = "0.5.2"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -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
- end
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.5.2
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-04-18 00:00:00.000000000 Z
11
+ date: 2017-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json