ruby_http_client 3.0.2 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 915aca3c1ef2db0465b051e7a30aa8af13918469
4
- data.tar.gz: 3a296aa133c66dd033b7be2b4483b0a3e085c242
3
+ metadata.gz: 3f9d4f32d8569bd5aa2e86656e3d6e68b5334442
4
+ data.tar.gz: 475cbb494afc68fe05165188196d7ae5f4e56113
5
5
  SHA512:
6
- metadata.gz: e8e6f3b4c8c1c32d56fd0ed8dc7f845d5bb6ddd232305b532642a6340fe6fce102ddfa11687af5ac1cb8541ecb1ef0d6954e7583143b5dbaf39571dba68d0c74
7
- data.tar.gz: 67b7f3b7d8db5bc929e566c0296e8f9297a9d8f921cdc916f0e755b5fe22eaaf9987d48b5618ccb6054d7f58c28e419328604db8fc5a65bd92575f3b163cd0e3
6
+ metadata.gz: 98ed2fbc4320ce9a9391db9f63d3c7bc02f27eb52cddb548af2a2b98dcb55ab015477911af84850aedcfc0a5b11992aaa50cf2b544df1f333461459e6b08e0ea
7
+ data.tar.gz: ce31fc3da4472576bfc3f2ca373c1a9719a3d3f34d8d3a3567737dea9152aeafa2ec04ac1eb39b467605f5d9b6e091ecb699e3b07b502fad0bc5963670f3c4d4
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ bin
1
2
  *.gem
2
3
  *.rbc
3
4
  /.config
@@ -28,7 +29,7 @@ build/
28
29
 
29
30
  # for a library or gem, you might want to ignore these files since the code is
30
31
  # intended to run in multiple environments; otherwise, check them in:
31
- # Gemfile.lock
32
+ Gemfile.lock
32
33
  # .ruby-version
33
34
  # .ruby-gemset
34
35
 
data/CHANGELOG.md CHANGED
@@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.
3
3
 
4
4
  This project adheres to [Semantic Versioning](http://semver.org/).
5
5
 
6
+ ## [3.1.0] - 2016-04-10
7
+ ### Added
8
+ - #5 Ability to set the Content-Type header
9
+ - Thanks to [Wataru Sato](https://github.com/awwa) for the pull request!
10
+
6
11
  ## [3.0.2] - 2016-04-10
7
12
  ### Update
8
13
  - #8 Internal refactor
data/README.md CHANGED
@@ -12,7 +12,7 @@ All updates to this library is documented in our [CHANGELOG](https://github.com/
12
12
 
13
13
  ## Prerequisites
14
14
 
15
- - Ruby version 2.2
15
+ - Ruby version 2.2+
16
16
 
17
17
  ## Install Package
18
18
 
@@ -27,7 +27,7 @@ gem install ruby_http_client
27
27
  ```ruby
28
28
  require 'ruby_http_client'
29
29
  global_headers = {'Authorization' => 'Basic XXXXXXX' }
30
- client = SendGrid::Client(host: 'base_url', request_headers: global_headers)
30
+ client = SendGrid::Client.new(host: 'base_url', request_headers: global_headers)
31
31
  client.your.api._(param).call.get
32
32
  puts response.status_code
33
33
  puts response.body
@@ -39,7 +39,7 @@ puts response.headers
39
39
  ```ruby
40
40
  require 'ruby_http_client'
41
41
  global_headers = {'Authorization' => 'Basic XXXXXXX' }
42
- client = SendGrid::Client(host: 'base_url', request_headers: global_headers)
42
+ client = SendGrid::Client.new(host: 'base_url', request_headers: global_headers)
43
43
  query_params = { 'hello' => 0, 'world' => 1 }
44
44
  request_headers = { 'X-Test' => 'test' }
45
45
  data = { 'some' => 1, 'awesome' => 2, 'data' => 3}
@@ -19,7 +19,7 @@ module SendGrid
19
19
 
20
20
  # A simple REST client.
21
21
  class Client
22
- attr_reader :host, :request_headers, :url_path
22
+ attr_reader :host, :request_headers, :url_path, :request, :http
23
23
  # * *Args* :
24
24
  # - +host+ -> Base URL for the api. (e.g. https://api.sendgrid.com)
25
25
  # - +request_headers+ -> A hash of the headers you want applied on
@@ -132,18 +132,21 @@ module SendGrid
132
132
  def build_request(name, args)
133
133
  build_args(args) if args
134
134
  uri = build_url(query_params: @query_params)
135
- http = Net::HTTP.new(uri.host, uri.port)
136
- http = add_ssl(http)
135
+ @http = add_ssl(Net::HTTP.new(uri.host, uri.port))
137
136
  net_http = Kernel.const_get('Net::HTTP::' + name.to_s.capitalize)
138
- request = net_http.new(uri.request_uri)
139
- request = build_request_headers(request)
140
- request.body = @request_body.to_json if @request_body
141
- if request.body
142
- request['Content-Type'] = 'application/json'
143
- elsif !request.body and (name.to_s == "post")
144
- request['Content-Type'] = ''
137
+ @request = build_request_headers(net_http.new(uri.request_uri))
138
+ if (@request_body &&
139
+ (!@request_headers.has_key?('Content-Type') ||
140
+ @request_headers['Content-Type'] == 'application/json')
141
+ )
142
+ @request.body = @request_body.to_json
143
+ @request['Content-Type'] = 'application/json'
144
+ elsif !@request_body and (name.to_s == "post")
145
+ @request['Content-Type'] = ''
146
+ else
147
+ @request.body = @request_body
145
148
  end
146
- make_request(http, request)
149
+ make_request(@http, @request)
147
150
  end
148
151
 
149
152
  # Make the API call and return the response. This is separated into
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'ruby_http_client'
7
- spec.version = '3.0.2'
7
+ spec.version = '3.1.0'
8
8
  spec.authors = ['Elmer Thomas']
9
9
  spec.email = 'dx@sendgrid.com'
10
10
  spec.summary = 'A simple REST client'
@@ -1,4 +1,4 @@
1
- require_relative '../lib/ruby_http_client'
1
+ require 'ruby_http_client'
2
2
  require 'minitest/autorun'
3
3
 
4
4
  class MockResponse
@@ -50,20 +50,20 @@ class TestClient < Minitest::Test
50
50
  def test_build_request_headers
51
51
  request = {}
52
52
  request = @client.build_request_headers(request)
53
- assert_equal(@client.request_headers, request)
53
+ assert_equal(request, @client.request_headers)
54
54
  end
55
55
 
56
56
  def test_add_version
57
57
  url = ''
58
58
  @client.add_version(url)
59
- assert_equal(url, "/#{@version}")
59
+ assert_equal("/#{@version}", url)
60
60
  end
61
61
 
62
62
  def test_build_query_params
63
63
  url = ''
64
64
  query_params = { 'limit' => 100, 'offset' => 0 }
65
65
  url = @client.build_query_params(url, query_params)
66
- assert_equal(url, '?limit=100&offset=0')
66
+ assert_equal('?limit=100&offset=0', url)
67
67
  end
68
68
 
69
69
  def test_build_url
@@ -71,28 +71,85 @@ class TestClient < Minitest::Test
71
71
  params = { 'limit' => 100, 'offset' => 0 }
72
72
  url = URI.parse(@host + '/' + @version +
73
73
  '/my/path/to/the/endpoint?limit=100&offset=0')
74
- assert_equal(url1.build_url(query_params: params), url)
74
+ assert_equal(url, url1.build_url(query_params: params))
75
75
 
76
76
  url1 = url1.one_more
77
77
  params = { 'limit' => 100, 'offset' => 0 }
78
78
  url = URI.parse(@host + '/' + @version +
79
79
  '/my/path/to/the/endpoint/one_more?limit=100&offset=0')
80
- assert_equal(url1.build_url(query_params: params), url)
80
+ assert_equal(url, url1.build_url(query_params: params))
81
81
 
82
82
  url2 = @client.my.path._('to').the.endpoint
83
83
  params = { 'limit' => 100, 'offset' => 0 }
84
84
  url = URI.parse(@host + '/' + @version +
85
85
  '/my/path/to/the/endpoint?limit=100&offset=0')
86
- assert_equal(url2.build_url(query_params: params), url)
86
+ assert_equal(url, url2.build_url(query_params: params))
87
87
  end
88
88
 
89
89
  def test_build_request
90
90
  name = 'get'
91
91
  args = nil
92
92
  response = @client.build_request(name, args)
93
- assert_equal(response.status_code, 200)
94
- assert_equal(response.body, 'message' => 'success')
95
- assert_equal(response.headers, 'headers' => 'test')
93
+ assert_equal(200, response.status_code)
94
+ assert_equal({'message' => 'success'}, response.body)
95
+ assert_equal({'headers' => 'test'}, response.headers)
96
+ end
97
+
98
+ def test_build_request_post_empty_content_type
99
+ headers = {
100
+ }
101
+ client = MockRequest.new(
102
+ host: 'https://localhost',
103
+ request_headers: headers,
104
+ version: 'v3'
105
+ )
106
+ args = [{'request_body' => {"hogekey" => "hogevalue"}}]
107
+ client.build_request('post', args)
108
+ assert_equal('application/json', client.request['Content-Type'])
109
+ assert_equal('{"hogekey":"hogevalue"}', client.request.body)
110
+ end
111
+
112
+ def test_build_request_get_application_json
113
+ headers = {
114
+ 'Content-Type' => 'application/json'
115
+ }
116
+ client = MockRequest.new(
117
+ host: 'https://localhost',
118
+ request_headers: headers,
119
+ version: 'v3'
120
+ )
121
+ client.build_request('get', nil)
122
+ assert_equal('application/json', client.request['Content-Type'])
123
+ assert_equal(nil, client.request.body)
124
+ end
125
+
126
+ def test_build_request_post_empty_body
127
+ headers = {
128
+ 'Content-Type' => 'application/json'
129
+ }
130
+ client = MockRequest.new(
131
+ host: 'https://localhost',
132
+ request_headers: headers,
133
+ version: 'v3'
134
+ )
135
+ client.build_request('post', nil)
136
+ assert_equal('', client.request['Content-Type'])
137
+ assert_equal(nil, client.request.body)
138
+ end
139
+
140
+ def test_build_request_post_multipart
141
+ headers = {
142
+ 'Content-Type' => 'multipart/form-data; boundary=xYzZY'
143
+ }
144
+ client = MockRequest.new(
145
+ host: 'https://localhost',
146
+ request_headers: headers,
147
+ )
148
+ name = 'post'
149
+ args = [{'request_body' => 'hogebody'}]
150
+ client.build_request(name, args)
151
+ assert_equal('multipart/form-data; boundary=xYzZY', client.request['Content-Type'])
152
+ assert_equal('hogebody', client.request.body)
96
153
  end
97
154
 
98
155
  def add_ssl
@@ -105,13 +162,13 @@ class TestClient < Minitest::Test
105
162
 
106
163
  def test__
107
164
  url1 = @client._('test')
108
- assert_equal(url1.url_path, ['test'])
165
+ assert_equal(['test'], url1.url_path)
109
166
  end
110
167
 
111
168
  def test_method_missing
112
169
  response = @client.get
113
- assert_equal(response.status_code, 200)
114
- assert_equal(response.body, 'message' => 'success')
115
- assert_equal(response.headers, 'headers' => 'test')
170
+ assert_equal(200, response.status_code)
171
+ assert_equal({'message' => 'success'}, response.body)
172
+ assert_equal({'headers' => 'test'}, response.headers)
116
173
  end
117
174
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_http_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elmer Thomas