class Button::Response
Response
is a simple proxy class for easy value unpacking from an API response. It is constructed with a hash and proxies method calls on the instance down to keys in the underling hash.
## Usage
response = Button::Response.new
({
prev: 'https://bloop.net/?cursor=1989', next: 'https://bloop.net/?cursor=1991'
}, :a => 1, :b => “two”)
puts response.data puts response.next_cursor puts response.prev_cursor
Public Class Methods
format_cursor(cursor_url)
click to toggle source
# File lib/button/response.rb, line 53 def format_cursor(cursor_url) return nil unless cursor_url parsed = URI(cursor_url) return nil unless parsed.query query = CGI.parse(parsed.query) cursor = query.fetch('cursor', []) cursor.empty? ? nil : cursor[0] end
new(meta, response_data)
click to toggle source
# File lib/button/response.rb, line 21 def initialize(meta, response_data) @meta = meta @response_data = response_data end
Public Instance Methods
data()
click to toggle source
# File lib/button/response.rb, line 40 def data @response_data end
next_cursor()
click to toggle source
# File lib/button/response.rb, line 44 def next_cursor Response.format_cursor(@meta.fetch(:next, nil)) end
prev_cursor()
click to toggle source
# File lib/button/response.rb, line 48 def prev_cursor Response.format_cursor(@meta.fetch(:prev, nil)) end
to_s()
click to toggle source
# File lib/button/response.rb, line 26 def to_s repr = '' if @response_data.is_a?(Hash) repr = @response_data.reduce([]) do |acc, (name, value)| acc + ["#{name}: #{value || 'nil'}"] end.join(', ') elsif @response_data.is_a?(Array) repr = "#{@response_data.size} elements" end "Button::Response(#{repr})" end