Class: Button::Resource

Inherits:
Object
  • Object
show all
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

Direct Known Subclasses

Orders

Constant Summary collapse

USER_AGENT =
"Button/#{Button::VERSION} ruby/#{RUBY_VERSION}".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, config) ⇒ Resource

Returns a new instance of Resource.



29
30
31
32
33
34
35
36
37
# File 'lib/button/resources/resource.rb', line 29

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

#configObject

Returns the value of attribute config.



105
106
107
# File 'lib/button/resources/resource.rb', line 105

def config
  @config
end

Instance Method Details

#api_delete(path) ⇒ Button::Response

Performs an HTTP DELETE at the provided path.

Parameters:

  • path (String)

    the HTTP path

Returns:



67
68
69
# File 'lib/button/resources/resource.rb', line 67

def api_delete(path)
  api_request(Net::HTTP::Delete.new(path))
end

#api_get(path) ⇒ Button::Response

Performs an HTTP GET at the provided path.

Parameters:

  • path (String)

    the HTTP path

Returns:



48
49
50
# File 'lib/button/resources/resource.rb', line 48

def api_get(path)
  api_request(Net::HTTP::Get.new(path))
end

#api_post(path, body) ⇒ Button::Response

Performs an HTTP POST at the provided path.

Parameters:

  • path (String)

    the HTTP path

  • body (Hash)

    the HTTP request body

Returns:



58
59
60
# File 'lib/button/resources/resource.rb', line 58

def api_post(path, body)
  api_request(Net::HTTP::Post.new(path), body)
end

#timeoutObject



39
40
41
# File 'lib/button/resources/resource.rb', line 39

def timeout
  @http.read_timeout
end