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

Accounts, Customers, Merchants, 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.



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

#configObject

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.

Parameters:

  • path (String)

    the HTTP path

Returns:



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.

Parameters:

  • path (String)

    the HTTP path

  • query (Hash=) (defaults to: nil)

    optional query params to send

Returns:



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.

Parameters:

  • path (String)

    the HTTP path

  • body (Hash)

    the HTTP request body

Returns:



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

#timeoutObject



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

def timeout
  @http.read_timeout
end