lib/ayadn/api.rb in ayadn-3.0 vs lib/ayadn/api.rb in ayadn-4.0
- old
+ new
@@ -1,17 +1,16 @@
# encoding: utf-8
module Ayadn
class API
- def initialize
- @workers = Workers.new
- @status = Status.new
- end
-
def get_unified(options)
+ # "paginate" fetches last post ID we've seen if the user asks for scrolling or asks to see new posts only
options = paginate options, 'unified'
- get_parsed_response(Endpoints.new.unified(options))
+ # Create the API endpoint URL
+ endpoint = Endpoints.new.unified(options)
+ # Fetch the response from the ADN servers
+ get_parsed_response(endpoint)
end
def get_checkins(options)
options = paginate options, 'explore:checkins'
get_parsed_response(Endpoints.new.checkins(options))
@@ -109,28 +108,31 @@
def get_followers(username)
build_list(username, :followers)
end
+ # "nil" as a first argument? the other method should be refactored
def get_muted
build_list(nil, :muted)
end
def get_blocked
build_list(nil, :blocked)
end
def get_raw_list(username, target)
options = {:count => 200, :before_id => nil}
- big = []
+ bucket = []
+ # Fetch new items until the API says no more
+ # This is chronologically reversed: start with current id, get 200 posts, get the post id we're at, then 200 again, etc
loop do
resp = get_parsed_response(get_list_url(username, target, options))
- big << resp
+ bucket << resp
break if resp['meta']['min_id'] == nil || resp['meta']['more'] == false
options = {:count => 200, :before_id => resp['meta']['min_id']}
end
- big
+ bucket
end
def get_user(username)
get_parsed_response(Endpoints.new.user(username))
end
@@ -148,12 +150,13 @@
unless options[:all]
resp = get_parsed_response(Endpoints.new.files_list(options))
resp['data'].each { |p| array_of_hashes << p }
else
options = {:count => 200, :before_id => nil}
+ endpoints = Endpoints.new
loop do
- resp = get_parsed_response(Endpoints.new.files_list(options))
+ resp = get_parsed_response(endpoints.files_list(options))
resp['data'].each { |p| array_of_hashes << p }
break unless resp['meta']['more']
options = {:count => 200, :before_id => resp['meta']['min_id']}
end
end
@@ -218,19 +221,10 @@
end
def get_channels
options = {:count => 200, :recent_message => 1, :annotations => 1, :before_id => nil}
get_parsed_response(Endpoints.new.channels(options))
- # big = []
- # loop do
- # resp = get_parsed_response(Endpoints.new.channels(options))
- # #check_response_meta_code(resp)
- # big << resp
- # break if resp['meta']['more'] == false
- # options = {:count => 200, :before_id => resp['meta']['min_id']}
- # end
- # big
end
def get_channel channel_id, options = {}
options = {:recent_message => 1, :annotations => 1, :before_id => nil}
get_parsed_response(Endpoints.new.channel(channel_id, options))
@@ -261,26 +255,37 @@
url = Endpoints.new.update_marker
CNX.post(url, obj)
end
def self.build_query(arg)
+ # Number of posts/messages to fetch.
+ # Either from CLI and integer
+ # or from the settings
if arg[:count].to_s.is_integer?
count = arg[:count]
else
- count = Settings.options[:counts][:default]
+ count = Settings.options.counts.default
end
- directed = arg[:directed] || Settings.options[:timeline][:directed]
+ # Do we want the "directed posts"?
+ # Either from CLI (optional)
+ # or from the settings
+ directed = arg[:directed] || Settings.options.timeline.directed
+ # because I was not always consistent in the legacy code base, let's be cautious
if directed == true || directed == 1
directed = 1
else
directed = 0
end
+ # We *never* want the HTML in the response, we are a CLI client
html = 0
+ # If the user asks to see posts starting with a specific post ID
if arg[:since_id]
"&count=#{count}&include_html=#{html}&include_directed_posts=#{directed}&include_deleted=0&include_annotations=1&since_id=#{arg[:since_id]}"
+ # Or asks to see their recent messages
elsif arg[:recent_message]
"&count=#{count}&include_html=#{html}&include_directed_posts=#{directed}&include_deleted=0&include_annotations=1&include_recent_message=#{arg[:recent_message]}"
+ # Else we create a normal request URL
else
"&count=#{count}&include_html=#{html}&include_directed_posts=#{directed}&include_deleted=0&include_annotations=1"
end
end
@@ -292,28 +297,32 @@
end
return options
end
def get_parsed_response(url)
+ # Bool for retry if failure
working = true
begin
+ # Get the response from the API and decode the JSON
resp = JSON.parse(CNX.get_response_from(url))
return resp
rescue JSON::ParserError => e
- if working == true
+ # Retry once after 10 seconds if the response wasn't valid
+ status = Status.new
+ if working
working = false
- @status.server_error(true)
+ status.server_error(true)
begin
sleep 10
rescue Interrupt
- @status.canceled
+ status.canceled
exit
end
- puts "\e[H\e[2J"
+ View.new.clear_screen
retry
else
- @status.server_error(false)
+ status.server_error(false)
Errors.global_error({error: e, caller: caller, data: [resp]})
end
end
end
@@ -324,18 +333,20 @@
resp
end
end
def build_list(username, target)
+ # Fetch data for each user (and verify the user isn't deleted)
options = {:count => 200, :before_id => nil}
big_hash = {}
+ workers= Workers.new
loop do
resp = get_parsed_response(get_list_url(username, target, options))
if resp['meta']['code'] == 404
- @status.user_404(username)
+ Status.new.user_404(username)
exit
end
- users = @workers.extract_users(resp)
+ users = workers.extract_users(resp)
big_hash.merge!(users)
break if resp['meta']['min_id'] == nil
options = {:count => 200, :before_id => resp['meta']['min_id']}
end
big_hash