Skip to content

Commit f45fcc4

Browse files
authored
Merge branch 'master' into issue#79
2 parents c719a0b + efc7af6 commit f45fcc4

File tree

7 files changed

+176
-36
lines changed

7 files changed

+176
-36
lines changed

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2016-2017 SendGrid, Inc.
3+
Copyright (c) 2016-2018 SendGrid, Inc.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

PULL_REQUEST_TEMPLATE.md

Lines changed: 0 additions & 25 deletions
This file was deleted.

USAGE.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Usage
2+
3+
Usage examples for SendGrid ruby-http-client
4+
5+
## Initialization
6+
7+
```ruby
8+
require_relative '../lib/ruby_http_client'
9+
10+
# This uses the SendGrid API as an example
11+
headers = JSON.parse('
12+
{
13+
"Authorization": "Bearer ' + ENV['SENDGRID_API_KEY'] + '"
14+
}
15+
')
16+
host = 'https://api.sendgrid.com'
17+
client = SendGrid::Client.new(host: host, request_headers: headers)
18+
```
19+
20+
## Table of Contents
21+
22+
- [GET](#get)
23+
- [DELETE](#delete)
24+
- [POST](#post)
25+
- [PUT](#put)
26+
- [PATCH](#patch)
27+
28+
## GET
29+
30+
#### GET Collection
31+
32+
```ruby
33+
query_params = { 'limit' => 100, 'offset' => 0 }
34+
response = client.version('v3').api_keys.get(query_params: query_params)
35+
puts response.status_code
36+
puts response.body
37+
puts response.headers
38+
```
39+
40+
#### GET Single
41+
42+
```ruby
43+
response = client.version('v3').api_keys._(api_key_id).get
44+
puts response.status_code
45+
puts response.body
46+
puts response.headers
47+
```
48+
49+
## DELETE
50+
51+
```ruby
52+
response = client.api_keys._(api_key_id).delete
53+
puts response.status_code
54+
puts response.headers
55+
```
56+
57+
## POST
58+
59+
```ruby
60+
request_body = JSON.parse('
61+
{
62+
"name": "My API Key Ruby Test",
63+
"scopes": [
64+
"mail.send",
65+
"alerts.create",
66+
"alerts.read"
67+
]
68+
}
69+
')
70+
response = client.version('v3').api_keys.post(request_body: request_body)
71+
puts response.status_code
72+
puts response.body
73+
puts response.headers
74+
api_key_id = JSON.parse(response.body)['api_key_id']
75+
```
76+
77+
## PUT
78+
79+
```ruby
80+
request_body = JSON.parse('
81+
{
82+
"name": "A New Hope",
83+
"scopes": [
84+
"user.profile.read",
85+
"user.profile.update"
86+
]
87+
}
88+
')
89+
90+
response = client.api_keys._(api_key_id).put(request_body: request_body)
91+
puts response.status_code
92+
puts response.body
93+
puts response.headers
94+
```
95+
96+
## PATCH
97+
98+
```ruby
99+
request_body = JSON.parse('
100+
{
101+
"name": "A New Hope"
102+
}
103+
')
104+
response = client.api_keys._(api_key_id).patch(request_body: request_body)
105+
puts response.status_code
106+
puts response.body
107+
puts response.headers
108+
```

examples/example.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515
# request_headers: headers,
1616
# http_options: {open_timeout: 15, read_timeout: 30})
1717

18+
# If you want to make request via proxy, you can set your proxy server in two ways.
19+
#
20+
# (1) Pass proxy_options hash
21+
#
22+
# client = SendGrid::Client.new(host: host,
23+
# request_headers: headers,
24+
# proxy_options: { host: '127.0.0.1', port: 8080 })
25+
#
26+
# (2) Set 'http_proxy' environment variable
27+
#
28+
# ENV['http_proxy'] = 'user:[email protected]:8080'
29+
# client = SendGrid::Client.new(host: host, request_headers: headers)
30+
1831
# GET Collection
1932
query_params = { 'limit' => 100, 'offset' => 0 }
2033
response = client.version('v3').api_keys.get(query_params: query_params)

lib/ruby_http_client.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ class Client
3535
# Or just pass the version as part of the URL
3636
# (e.g. client._("/v3"))
3737
# - +url_path+ -> A list of the url path segments
38+
# - +proxy_options+ -> A hash of proxy settings.
39+
# (e.g. { host: '127.0.0.1', port: 8080 })
3840
#
39-
def initialize(host: nil, request_headers: nil, version: nil, url_path: nil, http_options: {})
41+
def initialize(host: nil, request_headers: nil, version: nil, url_path: nil, http_options: {}, proxy_options: {})
4042
@host = host
4143
@request_headers = request_headers || {}
4244
@version = version
@@ -45,6 +47,7 @@ def initialize(host: nil, request_headers: nil, version: nil, url_path: nil, htt
4547
@query_params = nil
4648
@request_body = nil
4749
@http_options = http_options
50+
@proxy_options = proxy_options
4851
end
4952

5053
# Update the headers for the request
@@ -139,7 +142,7 @@ def build_url(query_params: nil)
139142
def build_request(name, args)
140143
build_args(args) if args
141144
uri = build_url(query_params: @query_params)
142-
@http = add_ssl(Net::HTTP.new(uri.host, uri.port))
145+
@http = build_http(uri.host, uri.port)
143146
net_http = Kernel.const_get('Net::HTTP::' + name.to_s.capitalize)
144147
@request = build_request_headers(net_http.new(uri.request_uri))
145148
if @request_body &&
@@ -173,6 +176,16 @@ def make_request(http, request)
173176
Response.new(response)
174177
end
175178

179+
# Build HTTP request object
180+
#
181+
# * *Returns* :
182+
# - Request object
183+
def build_http(host, port)
184+
params = [host, port]
185+
params = params + @proxy_options.values_at(:host, :port, :user, :pass) unless @proxy_options.empty?
186+
add_ssl(Net::HTTP.new(*params))
187+
end
188+
176189
# Allow for https calls
177190
#
178191
# * *Args* :

test/test_ruby_http_client.rb

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,46 @@ def test_http_options
185185
assert_equal(['test'], url1.url_path)
186186
end
187187

188-
def test_docker_exists
189-
assert(File.file?('./Dockerfile') || File.file?('./docker/Dockerfile'))
188+
def test_proxy_options
189+
proxy_options = {
190+
host: '127.0.0.1', port: 8080, user: 'anonymous', pass: 'secret'
191+
}
192+
client = MockRequest.new(
193+
host: 'https://api.sendgrid.com',
194+
request_headers: { 'Authorization' => 'Bearer xxx' },
195+
proxy_options: proxy_options
196+
).version('v3').api_keys
197+
198+
assert(client.proxy_address, '127.0.0.1')
199+
assert(client.proxy_pass, 'secret')
200+
assert(client.proxy_port, 8080)
201+
assert(client.proxy_user, 'anonymous')
190202
end
191203

192-
def test_docker_compose_exists
193-
assert(File.file?('./docker-compose.yml') || File.file?('./docker/docker-compose.yml'))
204+
def test_proxy_from_http_proxy_environment_variable
205+
ENV['http_proxy'] = 'anonymous:[email protected]:8080'
206+
207+
client = MockRequest.new(
208+
host: 'https://api.sendgrid.com',
209+
request_headers: { 'Authorization' => 'Bearer xxx' }
210+
).version('v3').api_keys
211+
212+
assert(client.proxy_address, '127.0.0.1')
213+
assert(client.proxy_pass, 'secret')
214+
assert(client.proxy_port, 8080)
215+
assert(client.proxy_user, 'anonymous')
216+
ensure
217+
ENV.delete('http_proxy')
194218
end
195219

220+
# def test_docker_exists
221+
# assert(File.file?('./Dockerfile') || File.file?('./docker/Dockerfile'))
222+
# end
223+
224+
# def test_docker_compose_exists
225+
# assert(File.file?('./docker-compose.yml') || File.file?('./docker/docker-compose.yml'))
226+
# end
227+
196228
def test_env_sample_exists
197229
assert(File.file?('./.env_sample'))
198230
end
@@ -241,10 +273,6 @@ def test_troubleshooting_exists
241273
assert(File.file?('./TROUBLESHOOTING.md'))
242274
end
243275

244-
def test_usage_exists
245-
assert(File.file?('./USAGE.md'))
246-
end
247-
248276
def test_use_cases_exists
249277
assert(File.file?('./USE_CASES.md'))
250278
end

use_cases/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This directory provides examples for specific use cases. Please [open an issue](https://github.com/sendgrid/ruby-http-client/issues) or make a pull request for any use cases you would like us to document here. Thank you!
2+
3+
# Table of Contents

0 commit comments

Comments
 (0)