class Ayadn::API

Public Class Methods

build_query(arg) click to toggle source
# File lib/ayadn/api.rb, line 259
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
  end
  # 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

Public Instance Methods

block(username) click to toggle source
# File lib/ayadn/api.rb, line 182
def block(username)
  JSON.parse(CNX.post(Endpoints.new.block(username)))
end
check_response_meta_code(res) click to toggle source
# File lib/ayadn/api.rb, line 242
def check_response_meta_code(res)
  if res['meta']['code'] == 200
    res
  else
    Errors.global_error({error: nil, caller: caller, data: [res['meta']]})
  end
end
delete_message(channel_id, message_id) click to toggle source
# File lib/ayadn/api.rb, line 194
def delete_message(channel_id, message_id)
  JSON.parse(CNX.delete(Endpoints.new.delete_message(channel_id, message_id)))
end
delete_post(post_id) click to toggle source
# File lib/ayadn/api.rb, line 190
def delete_post(post_id)
  JSON.parse(CNX.delete(Endpoints.new.delete_post(post_id)))
end
follow(username) click to toggle source
# File lib/ayadn/api.rb, line 174
def follow(username)
  JSON.parse(CNX.post(Endpoints.new.follow(username)))
end
get_blocked() click to toggle source
# File lib/ayadn/api.rb, line 118
def get_blocked
  build_list(nil, :blocked)
end
get_channel(channel_id, options = {}) click to toggle source
# File lib/ayadn/api.rb, line 228
def get_channel channel_id, options = {}
  options = {:recent_message => 1, :annotations => 1, :before_id => nil}
  get_parsed_response(Endpoints.new.channel(channel_id, options))
end
get_channels() click to toggle source
# File lib/ayadn/api.rb, line 223
def get_channels
  options = {:count => 200, :recent_message => 1, :annotations => 1, :before_id => nil}
  get_parsed_response(Endpoints.new.channels(options))
end
get_checkins(options) click to toggle source
# File lib/ayadn/api.rb, line 14
def get_checkins(options)
  options = paginate options, 'explore:checkins'
  get_parsed_response(Endpoints.new.checkins(options))
end
get_config() click to toggle source
# File lib/ayadn/api.rb, line 238
def get_config
  get_parsed_response(Endpoints.new.config_api_url)
end
get_conversations(options) click to toggle source
# File lib/ayadn/api.rb, line 32
def get_conversations(options)
  options = paginate options, 'explore:replies'
  get_explore(:conversations, options)
end
get_convo(post_id, options = {}) click to toggle source
# File lib/ayadn/api.rb, line 77
def get_convo(post_id, options = {})
  get_parsed_response(Endpoints.new.convo(post_id, options))
end
get_details(post_id, options = {}) click to toggle source
# File lib/ayadn/api.rb, line 144
def get_details(post_id, options = {})
  get_parsed_response(Endpoints.new.single_post(post_id, options))
end
get_explore(explore, options) click to toggle source
# File lib/ayadn/api.rb, line 37
def get_explore(explore, options)
  url = case explore
  when :trending
    Endpoints.new.trending(options)
  when :photos
    Endpoints.new.photos(options)
  when :conversations
    Endpoints.new.conversations(options)
  end
  get_parsed_response(url)
end
get_file(file_id) click to toggle source
# File lib/ayadn/api.rb, line 166
def get_file(file_id)
  get_parsed_response(Endpoints.new.file(file_id))
end
get_files_list(options) click to toggle source
# File lib/ayadn/api.rb, line 148
def get_files_list(options)
  array_of_hashes = []
  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.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
  array_of_hashes
end
get_followers(username) click to toggle source
# File lib/ayadn/api.rb, line 109
def get_followers(username)
  build_list(username, :followers)
end
get_followings(username) click to toggle source
# File lib/ayadn/api.rb, line 105
def get_followings(username)
  build_list(username, :followings)
end
get_global(options) click to toggle source
# File lib/ayadn/api.rb, line 19
def get_global(options)
  options = paginate options, 'global'
  get_parsed_response(Endpoints.new.global(options))
end
get_hashtag(hashtag) click to toggle source
# File lib/ayadn/api.rb, line 81
def get_hashtag(hashtag)
  get_parsed_response(Endpoints.new.hashtag(hashtag))
end
get_interactions() click to toggle source
# File lib/ayadn/api.rb, line 61
def get_interactions
  get_parsed_response(Endpoints.new.interactions)
end
get_mentions(username, options) click to toggle source
# File lib/ayadn/api.rb, line 49
def get_mentions(username, options)
  get_parsed_response(Endpoints.new.mentions(username, options))
end
get_messages(channel_id, options) click to toggle source
# File lib/ayadn/api.rb, line 233
def get_messages(channel_id, options)
  options = paginate options, "channel:#{channel_id}"
  get_parsed_response(Endpoints.new.messages(channel_id, options))
end
get_muted() click to toggle source

“nil” as a first argument? the other method should be refactored

# File lib/ayadn/api.rb, line 114
def get_muted
  build_list(nil, :muted)
end
get_photos(options) click to toggle source
# File lib/ayadn/api.rb, line 28
def get_photos(options)
  options = paginate options, 'explore:photos'
  get_explore(:photos, options)
end
get_posts(username, options) click to toggle source
# File lib/ayadn/api.rb, line 53
def get_posts(username, options)
  get_parsed_response(Endpoints.new.posts(username, options))
end
get_raw_list(username, target) click to toggle source
# File lib/ayadn/api.rb, line 122
def get_raw_list(username, target)
  options = {:count => 200, :before_id => nil}
  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))
    bucket << resp
    break if resp['meta']['min_id'] == nil || resp['meta']['more'] == false
    options = {:count => 200, :before_id => resp['meta']['min_id']}
  end
  bucket
end
get_token_info() click to toggle source
# File lib/ayadn/api.rb, line 65
def get_token_info
  get_parsed_response(Endpoints.new.token_info)
end
get_unified(options) click to toggle source
# File lib/ayadn/api.rb, line 5
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'
  # Create the API endpoint URL
  endpoint = Endpoints.new.unified(options)
  # Fetch the response from the ADN servers
  get_parsed_response(endpoint)
end
get_user(username) click to toggle source
# File lib/ayadn/api.rb, line 136
def get_user(username)
  get_parsed_response(Endpoints.new.user(username))
end
get_users(usernames) click to toggle source
# File lib/ayadn/api.rb, line 140
def get_users(usernames)
  get_parsed_response(Endpoints.new.users(usernames))
end
get_whatstarred(username, options) click to toggle source
# File lib/ayadn/api.rb, line 57
def get_whatstarred(username, options)
  get_parsed_response(Endpoints.new.whatstarred(username, options))
end
get_whoreposted(post_id) click to toggle source
# File lib/ayadn/api.rb, line 69
def get_whoreposted(post_id)
  get_parsed_response(Endpoints.new.whoreposted(post_id))
end
get_whostarred(post_id) click to toggle source
# File lib/ayadn/api.rb, line 73
def get_whostarred(post_id)
  get_parsed_response(Endpoints.new.whostarred(post_id))
end
mute(username) click to toggle source
# File lib/ayadn/api.rb, line 178
def mute(username)
  JSON.parse(CNX.post(Endpoints.new.mute(username)))
end
repost(post_id) click to toggle source
# File lib/ayadn/api.rb, line 186
def repost(post_id)
  JSON.parse(CNX.post(Endpoints.new.repost(post_id)))
end
search_annotations(words, options) click to toggle source
# File lib/ayadn/api.rb, line 93
def search_annotations words, options
  get_parsed_response(Endpoints.new.search_annotations(words, options))
end
search_channels(words, options) click to toggle source
# File lib/ayadn/api.rb, line 97
def search_channels words, options
  get_parsed_response(Endpoints.new.search_channels(words, options))
end
search_messages(channel_id, words, options) click to toggle source
# File lib/ayadn/api.rb, line 101
def search_messages channel_id, words, options
  get_parsed_response(Endpoints.new.search_messages(channel_id, words, options))
end
search_users(words, options) click to toggle source
# File lib/ayadn/api.rb, line 89
def search_users words, options
  get_parsed_response(Endpoints.new.search_users(words, options))
end
star(post_id) click to toggle source
# File lib/ayadn/api.rb, line 170
def star(post_id)
  JSON.parse(CNX.post(Endpoints.new.star(post_id)))
end
unblock(username) click to toggle source
# File lib/ayadn/api.rb, line 210
def unblock(username)
  JSON.parse(CNX.delete(Endpoints.new.block(username)))
end
unfollow(username) click to toggle source
# File lib/ayadn/api.rb, line 202
def unfollow(username)
  JSON.parse(CNX.delete(Endpoints.new.follow(username)))
end
unmute(username) click to toggle source
# File lib/ayadn/api.rb, line 206
def unmute(username)
  JSON.parse(CNX.delete(Endpoints.new.mute(username)))
end
unrepost(post_id) click to toggle source
# File lib/ayadn/api.rb, line 214
def unrepost(post_id)
  resp = JSON.parse(CNX.delete(Endpoints.new.repost(post_id)))
  if resp['data']['repost_of']
    JSON.parse(CNX.delete(Endpoints.new.repost(resp['data']['repost_of']['id'])))
  else
    resp
  end
end
unstar(post_id) click to toggle source
# File lib/ayadn/api.rb, line 198
def unstar(post_id)
  JSON.parse(CNX.delete(Endpoints.new.star(post_id)))
end
update_marker(name, last_read_id) click to toggle source
# File lib/ayadn/api.rb, line 250
def update_marker(name, last_read_id)
  obj = {
    'name' => name,
    'id' => last_read_id
  }
  url = Endpoints.new.update_marker
  CNX.post(url, obj)
end

Private Instance Methods

build_list(username, target) click to toggle source
# File lib/ayadn/api.rb, line 337
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.new.user_404(username)
      exit
    end
    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
end
get_list_url(username, target, options) click to toggle source
# File lib/ayadn/api.rb, line 356
def get_list_url(username, target, options)
  case target
  when :followings
    Endpoints.new.followings(username, options)
  when :followers
    Endpoints.new.followers(username, options)
  when :muted
    Endpoints.new.muted(options)
  when :blocked
    Endpoints.new.blocked(options)
  end
end
get_original_if_repost(resp) click to toggle source
# File lib/ayadn/api.rb, line 329
def get_original_if_repost(resp)
  if resp['repost_of']
    resp['repost_of']
  else
    resp
  end
end
get_parsed_response(url) click to toggle source
# File lib/ayadn/api.rb, line 301
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
    # Retry once after 10 seconds if the response wasn't valid
    status = Status.new
    if working
      working = false
      status.server_error(true)
      begin
        sleep 10
      rescue Interrupt
        status.canceled
        exit
      end
      View.new.clear_screen
      retry
    else
      status.server_error(false)
      Errors.global_error({error: e, caller: caller, data: [resp]})
    end
  end
end
paginate(options, target) click to toggle source
# File lib/ayadn/api.rb, line 294
def paginate options, target
  if options[:new] || options[:scroll]
    return {since_id: Databases.find_last_id_from(target)}
  end
  return options
end