Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion lib/mastodon/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,25 @@ module Mastodon
class Collection
include ::Enumerable

def initialize(items, klass)
def initialize(items, klass, headers = nil)
@collection = items.map { |attributes| klass.new(attributes) }
@link = {}
(headers ? headers.get("Link") : []).each do |link_values|
link_values.split(",").map(&:strip).each do |link_value|
uri_ref_part, *link_params = link_value.split(";").map(&:strip)
next unless uri_ref_part && uri_ref_part =~ /\A<(.*)>\z/
uri_ref = $1
rel_types_raw = link_params.find { |param| break $1 if param =~ /\Arel=(.*)\z/ }
if rel_types_raw =~ /\A"(.*)"\z/
rel_types = $1.split(/\s+/)
else
rel_types = [rel_types_raw].compact
end
rel_types.each do |rel_type|
@link[rel_type] = uri_ref
end
end
end
end

def each(start = 0)
Expand All @@ -23,5 +40,17 @@ def size
def last
@collection.last
end

def next_max_id
return unless @link["next"]
uri = Addressable::URI.parse(@link["next"])
uri.query_values["max_id"].to_i
end

def prev_since_id
return unless @link["prev"]
uri = Addressable::URI.parse(@link["prev"])
uri.query_values["since_id"].to_i
end
end
end
10 changes: 7 additions & 3 deletions lib/mastodon/rest/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ def initialize(client, request_method, path, options = {})
end

def perform
perform_with_headers[0]
end

def perform_with_headers
options_key = @request_method == :get ? :params : :form
response = http_client.headers(@headers).public_send(@request_method, @uri.to_s, options_key => @options)

STDERR.puts response.body if ENV['DEBUG'] == 'true'

fail_or_return(response.code, response.body.empty? ? '' : Oj.load(response.to_s, mode: :null))
fail_or_return(response.code, response.body.empty? ? '' : Oj.load(response.to_s, mode: :null), response.headers)
end

private

def fail_or_return(code, body)
def fail_or_return(code, body, headers)
raise Mastodon::Error::ERRORS[code].from_response(body) if Mastodon::Error::ERRORS.include?(code)
body
[body, headers]
end

def http_client
Expand Down
11 changes: 9 additions & 2 deletions lib/mastodon/rest/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ def perform_request(request_method, path, options = {})
Mastodon::REST::Request.new(self, request_method, path, options).perform
end

# @param request_method [Symbol]
# @param path [String]
# @param options [Hash]
def perform_request_with_headers(request_method, path, options = {})
Mastodon::REST::Request.new(self, request_method, path, options).perform_with_headers
end

# @param request_method [Symbol]
# @param path [String]
# @param options [Hash]
Expand All @@ -24,8 +31,8 @@ def perform_request_with_object(request_method, path, options, klass)
# @param options [Hash]
# @param klass [Class]
def perform_request_with_collection(request_method, path, options, klass)
response = perform_request(request_method, path, options)
Mastodon::Collection.new(response, klass)
response, headers = perform_request_with_headers(request_method, path, options)
Mastodon::Collection.new(response, klass, headers)
end

# Format an array of values into a query param
Expand Down