decathlon-https 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/decathlon/https.rb +24 -8
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ed30bcfe395a329f0f2217675bd09513bbd0bb378efe99f6f1582ac0a44f0ca8
4
- data.tar.gz: a3e8870b643bc8dad06159eaa0f5384a87d6597e5fdae671b30398db51e0f70a
3
+ metadata.gz: 75ced51ca25387dd63aab7ead0962a4a5e76021e6eedc86600c77fdb324cd35d
4
+ data.tar.gz: 1d634bc7f46c7c3451e4a8a6044f0c3aeb59845c43db66aed68f83ab5df96594
5
5
  SHA512:
6
- metadata.gz: 9e73cfaf5d4a95d4615b31a47dcdbc00ba358eea039c61f26a68863efc1b4eebaf77786c1f10907290603bbd6a9cb5b9ca59961198471a3e17c692c182e85a04
7
- data.tar.gz: 478b2e15b2783c79e402636cb5bf8d50f4d5154332d052b490adb050921222f2ab4b91fdf83ac8877edb42a2111d431b98fca3d538273dcedd27fa5d71806680
6
+ metadata.gz: de58b4bddd8439180b7020385379d7187e9873a931f09f5d5ff9eff21ab0973847cf0ab0b0cb0df43cea68a18698740da737bf9e7d3a6adba7ce8a8bb98576c7
7
+ data.tar.gz: a278eed529f22f7c95f30a373f0ca692158cb31e5dbfe4d5af60af42d63d8187dcf841c3f35b66cbd4005758543cbd77d845009c4b98b163aefceb729611732a
@@ -9,17 +9,26 @@ module Decathlon
9
9
  #
10
10
  module HTTPS
11
11
 
12
- VERSION = '0.1.2'
12
+ VERSION = '0.1.3'
13
13
 
14
14
  def self.json_api_request(base_url: '', params: {}, verb: :get, headers: {}, return_http_status_code: false)
15
15
 
16
- # Parse URI and build a new HTTPS client
16
+ # Parse URI and build a new HTTP(S) client
17
17
  #
18
18
  uri = URI.parse(base_url)
19
- raise 'InsecureRequestURL' unless uri.scheme == 'https'
20
19
  http = ::Net::HTTP.new(uri.host, uri.port)
21
- http.use_ssl = true
22
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
20
+
21
+ # Sometimes, there might be reasons to call a non-https API
22
+ #
23
+ if uri.scheme == 'https'
24
+ http.use_ssl = true
25
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
26
+
27
+ # Prevent calls to non HTTP based schemes
28
+ #
29
+ elsif uri.scheme != 'http'
30
+ raise 'InsecureRequestURL'
31
+ end
23
32
 
24
33
  # Don't include empty params
25
34
  #
@@ -61,13 +70,20 @@ module Decathlon
61
70
  #
62
71
  response = http.request(request)
63
72
  code = response.code.to_i
64
- body = response.body.to_s
73
+
74
+ # Parse the response body based on response content header
75
+ #
76
+ if response['content-type'].match(/application\/json/)
77
+ JSON.parse(response.body.to_s, symbolize_names: true)
78
+ else
79
+ body = response.body.to_s
80
+ end
65
81
 
66
82
  # Return parsed body content, and status code if requested
67
83
  #
68
- return { body: JSON.parse(body, symbolize_names: true), code: code } if return_http_status_code == true
84
+ return { body: body, code: code } if return_http_status_code == true
69
85
  return {} if code == 204 || (body.empty? && code < 400)
70
- return JSON.parse(body, symbolize_names: true)
86
+ return body
71
87
 
72
88
  end
73
89
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decathlon-https
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Hayter
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  requirements: []
77
- rubygems_version: 3.0.4
77
+ rubygems_version: 3.0.6
78
78
  signing_key:
79
79
  specification_version: 4
80
80
  summary: Simple abstraction library for making HTTPS JSON calls