Class: Staticdctl::RESTClient
- Inherits:
-
Object
- Object
- Staticdctl::RESTClient
- Defined in:
- lib/staticdctl/rest_client.rb
Overview
Simple REST client library with HMAC authentication.
Support for remote call on JSON REST API.
Example:
client = Staticdctl::RESTCLient.new(
url: "http://api.domain.tld/v1",
access_id: 1000,
secret_key: "youshallnotpass"
)
client.call(:get, "/resources") { |response| puts response }
Instance Method Summary collapse
-
#call(method, path, req_data = nil, &block) ⇒ Object
Call a remote REST API action.
-
#initialize(url, hmac = {}) ⇒ RESTClient
constructor
A new instance of RESTClient.
-
#send_files(path, files, &block) ⇒ Object
Send files using the HTTP multipart/form-data content-type.
Constructor Details
#initialize(url, hmac = {}) ⇒ RESTClient
Returns a new instance of RESTClient.
20 21 22 23 24 |
# File 'lib/staticdctl/rest_client.rb', line 20 def initialize(url, hmac={}) @url = url @access_id = hmac[:access_id] || "" @secret_key = hmac[:secret_key] || "" end |
Instance Method Details
#call(method, path, req_data = nil, &block) ⇒ Object
Call a remote REST API action.
Example:
client.call(:post, "/posts", {text: "hello_world"}) do |response|
puts response
end
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/staticdctl/rest_client.rb', line 32 def call(method, path, req_data=nil, &block) headers = { "Accept" => "application/json" } headers["Content-Type"] = "application/json" if req_data payload = req_data ? req_data.to_json : nil request = RestClient::Request.new( url: @url + path, method: method, headers: headers, payload: payload ) send_request(request, block) end |
#send_files(path, files, &block) ⇒ Object
Send files using the HTTP multipart/form-data content-type.
Example:
client.send_files("/attachments", {first: file1, second: file2})
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/staticdctl/rest_client.rb', line 51 def send_files(path, files, &block) headers = { "Accept" => "application/json", "Content-Type" => "multipart/form-data" } request = RestClient::Request.new( url: @url + path, method: :post, headers: headers, payload: files, timeout: -1 ) send_request(request, block) end |