Class: Button::Resource
- Inherits:
-
Object
- Object
- Button::Resource
- Defined in:
- lib/button/resources/resource.rb
Overview
Resource is a handy base class for making API requests. It includes serveral handy methods for making such requests:
api_get(path) api_post(path, body) api_delete(path)
## Usage
class Blorp < Button::Resource
def get(blorp_id)
api_get("/api/v1/blorps/#{blorp_id}")
end
end
Constant Summary collapse
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
-
#api_delete(path) ⇒ Button::Response
Performs an HTTP DELETE at the provided path.
-
#api_get(path, query = nil) ⇒ Button::Response
Performs an HTTP GET at the provided path.
-
#api_post(path, body) ⇒ Button::Response
Performs an HTTP POST at the provided path.
-
#initialize(api_key, config) ⇒ Resource
constructor
A new instance of Resource.
- #timeout ⇒ Object
Constructor Details
#initialize(api_key, config) ⇒ Resource
Returns a new instance of Resource.
30 31 32 33 34 35 36 37 38 |
# File 'lib/button/resources/resource.rb', line 30 def initialize(api_key, config) @api_key = api_key @config = config @http = Net::HTTP.new(config[:hostname], config[:port]) @http.use_ssl = config[:secure] return if config[:timeout].nil? @http.read_timeout = config[:timeout] end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
111 112 113 |
# File 'lib/button/resources/resource.rb', line 111 def config @config end |
Instance Method Details
#api_delete(path) ⇒ Button::Response
Performs an HTTP DELETE at the provided path.
71 72 73 |
# File 'lib/button/resources/resource.rb', line 71 def api_delete(path) api_request(Net::HTTP::Delete.new(path)) end |
#api_get(path, query = nil) ⇒ Button::Response
Performs an HTTP GET at the provided path.
50 51 52 53 54 |
# File 'lib/button/resources/resource.rb', line 50 def api_get(path, query = nil) uri = URI(path) uri.query = URI.encode_www_form(query) if query api_request(Net::HTTP::Get.new(uri.to_s)) end |
#api_post(path, body) ⇒ Button::Response
Performs an HTTP POST at the provided path.
62 63 64 |
# File 'lib/button/resources/resource.rb', line 62 def api_post(path, body) api_request(Net::HTTP::Post.new(path), body) end |
#timeout ⇒ Object
40 41 42 |
# File 'lib/button/resources/resource.rb', line 40 def timeout @http.read_timeout end |