class ClubhouseRuby::Request
Attributes
action[RW]
params[RW]
response_format[RW]
uri[RW]
Public Class Methods
new(clubhouse, action:, params: {})
click to toggle source
Prepares a fancy request object and ensures the inputs make as much sense as possible. It's still totally possible to just provide a path that doesn't match a real url though.
# File lib/clubhouse_ruby/request.rb, line 11 def initialize(clubhouse, action:, params: {}) raise ArgumentError unless validate_input(clubhouse, action, params) self.params = params || {} self.uri = construct_uri(clubhouse) self.action = action self.response_format = clubhouse.response_format end
Public Instance Methods
fetch()
click to toggle source
Executes the http(s) request and provides the response with some additional decoration in a Hash.
# File lib/clubhouse_ruby/request.rb, line 23 def fetch Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |https| req = Net::HTTP.const_get(action).new(uri) set_body(req) set_format_header(req) wrap_response(https.request(req)) end end
Private Instance Methods
construct_uri(clubhouse)
click to toggle source
# File lib/clubhouse_ruby/request.rb, line 45 def construct_uri(clubhouse) base_url = API_URL path = clubhouse.path.map(&:to_s).map { |p| p.gsub('_', '-') }.join('/') object_id = "/#{self.params.delete(:id)}" if self.params.key?(:id) token = clubhouse.token URI("#{base_url}#{path}#{object_id}?token=#{token}") end
set_body(req)
click to toggle source
# File lib/clubhouse_ruby/request.rb, line 59 def set_body(req) req.body = params.to_json if params end
set_format_header(req)
click to toggle source
# File lib/clubhouse_ruby/request.rb, line 53 def set_format_header(req) format_header = FORMATS[response_format][:headers][:header] format_content = FORMATS[response_format][:headers][:content] req[format_header] = format_content end
validate_input(clubhouse, action, params)
click to toggle source
# File lib/clubhouse_ruby/request.rb, line 36 def validate_input(clubhouse, action, params) clubhouse.is_a?(Clubhouse) && !clubhouse.path.nil? && !clubhouse.token.nil? && !clubhouse.response_format.nil? && ACTIONS.values.include?(action) && (params.is_a?(Hash) || params.nil?) end
wrap_response(res)
click to toggle source
# File lib/clubhouse_ruby/request.rb, line 63 def wrap_response(res) begin content = FORMATS[response_format][:parser].parse(res.body) if res.body rescue FORMATS[response_format][:parser]::ParserError content = res.body end { code: res.code, status: res.message, content: content } end