Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bin
*.gem
*.rbc
/.config
Expand Down Expand Up @@ -28,7 +29,7 @@ build/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
Gemfile.lock
# .ruby-version
# .ruby-gemset

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ gem install ruby_http_client
```ruby
require 'ruby_http_client'
global_headers = {'Authorization' => 'Basic XXXXXXX' }
client = SendGrid::Client(host: 'base_url', request_headers: global_headers)
client = SendGrid::Client.new(host: 'base_url', request_headers: global_headers)
client.your.api._(param).call.get
puts response.status_code
puts response.body
Expand All @@ -39,7 +39,7 @@ puts response.headers
```ruby
require 'ruby_http_client'
global_headers = {'Authorization' => 'Basic XXXXXXX' }
client = SendGrid::Client(host: 'base_url', request_headers: global_headers)
client = SendGrid::Client.new(host: 'base_url', request_headers: global_headers)
query_params = { 'hello' => 0, 'world' => 1 }
request_headers = { 'X-Test' => 'test' }
data = { 'some' => 1, 'awesome' => 2, 'data' => 3}
Expand Down
25 changes: 14 additions & 11 deletions lib/ruby_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(response)

# A simple REST client.
class Client
attr_reader :host, :request_headers, :url_path
attr_reader :host, :request_headers, :url_path, :request, :http
# * *Args* :
# - +host+ -> Base URL for the api. (e.g. https://api.sendgrid.com)
# - +request_headers+ -> A hash of the headers you want applied on
Expand Down Expand Up @@ -142,18 +142,21 @@ def build_url(query_params: nil)
def build_request(name, args)
build_args(args) if args
uri = build_url(query_params: @query_params)
http = Net::HTTP.new(uri.host, uri.port)
http = add_ssl(http)
@http = add_ssl(Net::HTTP.new(uri.host, uri.port))
net_http = Kernel.const_get('Net::HTTP::' + name.to_s.capitalize)
request = net_http.new(uri.request_uri)
request = build_request_headers(request)
request.body = @request_body.to_json if @request_body
if request.body
request['Content-Type'] = 'application/json'
elsif !request.body and (name.to_s == "post")
request['Content-Type'] = ''
@request = build_request_headers(net_http.new(uri.request_uri))
if (@request_body &&
(!@request_headers.has_key?('Content-Type') ||
@request_headers['Content-Type'] == 'application/json')
)
@request.body = @request_body.to_json
@request['Content-Type'] = 'application/json'
elsif !@request_body and (name.to_s == "post")
@request['Content-Type'] = ''
else
@request.body = @request_body
end
make_request(http, request)
make_request(@http, @request)
end

# Make the API call and return the response. This is separated into
Expand Down
85 changes: 71 additions & 14 deletions test/test_ruby_http_client.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative '../lib/ruby_http_client'
require 'ruby_http_client'
require 'minitest/autorun'

class MockResponse
Expand Down Expand Up @@ -50,49 +50,106 @@ def test_update_headers
def test_build_request_headers
request = {}
request = @client.build_request_headers(request)
assert_equal(@client.request_headers, request)
assert_equal(request, @client.request_headers)
end

def test_add_version
url = ''
@client.add_version(url)
assert_equal(url, "/#{@version}")
assert_equal("/#{@version}", url)
end

def test_build_query_params
url = ''
query_params = { 'limit' => 100, 'offset' => 0 }
url = @client.build_query_params(url, query_params)
assert_equal(url, '?limit=100&offset=0')
assert_equal('?limit=100&offset=0', url)
end

def test_build_url
url1 = @client.my.path.to.the.endpoint
params = { 'limit' => 100, 'offset' => 0 }
url = URI.parse(@host + '/' + @version +
'/my/path/to/the/endpoint?limit=100&offset=0')
assert_equal(url1.build_url(query_params: params), url)
assert_equal(url, url1.build_url(query_params: params))

url1 = url1.one_more
params = { 'limit' => 100, 'offset' => 0 }
url = URI.parse(@host + '/' + @version +
'/my/path/to/the/endpoint/one_more?limit=100&offset=0')
assert_equal(url1.build_url(query_params: params), url)
assert_equal(url, url1.build_url(query_params: params))

url2 = @client.my.path._('to').the.endpoint
params = { 'limit' => 100, 'offset' => 0 }
url = URI.parse(@host + '/' + @version +
'/my/path/to/the/endpoint?limit=100&offset=0')
assert_equal(url2.build_url(query_params: params), url)
assert_equal(url, url2.build_url(query_params: params))
end

def test_build_request
name = 'get'
args = nil
response = @client.build_request(name, args)
assert_equal(response.status_code, 200)
assert_equal(response.body, 'message' => 'success')
assert_equal(response.headers, 'headers' => 'test')
assert_equal(200, response.status_code)
assert_equal({'message' => 'success'}, response.body)
assert_equal({'headers' => 'test'}, response.headers)
end

def test_build_request_post_empty_content_type
headers = {
}
client = MockRequest.new(
host: 'https://localhost',
request_headers: headers,
version: 'v3'
)
args = [{'request_body' => {"hogekey" => "hogevalue"}}]
client.build_request('post', args)
assert_equal('application/json', client.request['Content-Type'])
assert_equal('{"hogekey":"hogevalue"}', client.request.body)
end

def test_build_request_get_application_json
headers = {
'Content-Type' => 'application/json'
}
client = MockRequest.new(
host: 'https://localhost',
request_headers: headers,
version: 'v3'
)
client.build_request('get', nil)
assert_equal('application/json', client.request['Content-Type'])
assert_equal(nil, client.request.body)
end

def test_build_request_post_empty_body
headers = {
'Content-Type' => 'application/json'
}
client = MockRequest.new(
host: 'https://localhost',
request_headers: headers,
version: 'v3'
)
client.build_request('post', nil)
assert_equal('', client.request['Content-Type'])
assert_equal(nil, client.request.body)
end

def test_build_request_post_multipart
headers = {
'Content-Type' => 'multipart/form-data; boundary=xYzZY'
}
client = MockRequest.new(
host: 'https://localhost',
request_headers: headers,
)
name = 'post'
args = [{'request_body' => 'hogebody'}]
client.build_request(name, args)
assert_equal('multipart/form-data; boundary=xYzZY', client.request['Content-Type'])
assert_equal('hogebody', client.request.body)
end

def add_ssl
Expand All @@ -105,13 +162,13 @@ def add_ssl

def test__
url1 = @client._('test')
assert_equal(url1.url_path, ['test'])
assert_equal(['test'], url1.url_path)
end

def test_method_missing
response = @client.get
assert_equal(response.status_code, 200)
assert_equal(response.body, 'message' => 'success')
assert_equal(response.headers, 'headers' => 'test')
assert_equal(200, response.status_code)
assert_equal({'message' => 'success'}, response.body)
assert_equal({'headers' => 'test'}, response.headers)
end
end