diff options
| author | mokha <mokha@cisco.com> | 2018-05-14 15:16:56 -0600 |
|---|---|---|
| committer | mokha <mokha@cisco.com> | 2018-05-14 15:16:56 -0600 |
| commit | a417b0f2a34582618c93740fd74ca1c620a39e54 (patch) | |
| tree | 04b4922bf963a483a860b81052fbbdc3b450629a /lib | |
| parent | 64f0138cff42932a4374c178d1c23d0e7de97fcc (diff) | |
attempt to map body using content type header.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/net/hippie.rb | 2 | ||||
| -rw-r--r-- | lib/net/hippie/client.rb | 7 | ||||
| -rw-r--r-- | lib/net/hippie/content_type_mapper.rb | 12 | ||||
| -rw-r--r-- | lib/net/hippie/json_mapper.rb | 10 |
4 files changed, 17 insertions, 14 deletions
diff --git a/lib/net/hippie.rb b/lib/net/hippie.rb index a02dbb4..6117512 100644 --- a/lib/net/hippie.rb +++ b/lib/net/hippie.rb @@ -4,7 +4,7 @@ require 'net/http' require 'openssl' require 'net/hippie/version' -require 'net/hippie/json_mapper' +require 'net/hippie/content_type_mapper' require 'net/hippie/client' require 'net/hippie/api' diff --git a/lib/net/hippie/client.rb b/lib/net/hippie/client.rb index b32c0f8..53f80d7 100644 --- a/lib/net/hippie/client.rb +++ b/lib/net/hippie/client.rb @@ -20,7 +20,7 @@ module Net @certificate = certificate @default_headers = headers @key = key - @mapper = JsonMapper.new + @mapper = ContentTypeMapper.new @passphrase = passphrase @verify_mode = verify_mode end @@ -70,8 +70,9 @@ module Net end def request_for(type, uri, headers: {}, body: {}) - type.new(uri, default_headers.merge(headers)).tap do |x| - x.body = mapper.map_from(body) unless body.empty? + final_headers = default_headers.merge(headers) + type.new(uri, final_headers).tap do |x| + x.body = mapper.map_from(final_headers, body) unless body.empty? end end diff --git a/lib/net/hippie/content_type_mapper.rb b/lib/net/hippie/content_type_mapper.rb new file mode 100644 index 0000000..132ee27 --- /dev/null +++ b/lib/net/hippie/content_type_mapper.rb @@ -0,0 +1,12 @@ +module Net + module Hippie + # Converts a ruby hash into a JSON string + class ContentTypeMapper + def map_from(headers, body) + content_type = headers['Content-Type'] || '' + return JSON.generate(body) if content_type.include?("json") + body + end + end + end +end diff --git a/lib/net/hippie/json_mapper.rb b/lib/net/hippie/json_mapper.rb deleted file mode 100644 index e04a545..0000000 --- a/lib/net/hippie/json_mapper.rb +++ /dev/null @@ -1,10 +0,0 @@ -module Net - module Hippie - # Converts a ruby hash into a JSON string - class JsonMapper - def map_from(hash) - JSON.generate(hash) - end - end - end -end |
