Determine most recent (re)post. Will use this to select still open (newer) posts for reposting.

This commit is contained in:
Señor Rolando 2013-12-10 23:08:34 +01:00
parent b25b8a5654
commit c23ef8e6ce
4 changed files with 89 additions and 6 deletions

View file

@ -37,12 +37,22 @@ class RepostsController < ApplicationController
access_token = Repost.obtain_user_token
# retrieve latest reposts. this uses the real ADN stream.
# TODO: do so: select max(postid) from reposts;
puts "===< retrieve latest post >==="
res = Repost.get_most_recent_user_post(access_token)
api_res = ApiResponse.new(:raw_json => res.body)
dump_api_response(api_res)
latest_post = PostData.most_recent_post(api_res.data)
puts "=== latest postid: " + latest_post.post_id.to_s + ", created at: " + latest_post.created_at.to_s
# TODO: select max(postid)
# retrieve new mentions.
# TODO restrict to: fetch( since_id=previous max_id )
puts "===< retrieve new mentions >==="
res = Repost.get_mentions_stream(access_token)
api_response = ApiResponse.new(:raw_json => res.body)
# dump_api_response(api_response)
# TODO: res.body contains the mentions in JSON. Proceed from there.
#new_mentions.each do | post |
## API - repost(post) # http://developers.app.net/docs/resources/post/reposts/
@ -53,4 +63,20 @@ class RepostsController < ApplicationController
# store all reposts locally for later reuse.
# TODO insert into reposts values("karlsruher", post.id, latestid)
end
private
def dump_api_response(api_response)
puts "====> meta:"
api_response.meta.each do |mline|
puts "--- meta line: " + mline.to_s
puts ""
end
puts ""
puts "====> data:"
api_response.data.each do |dline|
puts "--- data line: " + dline.to_s
puts ""
end
end
end

View file

@ -0,0 +1,20 @@
class ApiResponse
attr_reader :meta
attr_reader :data
# ApiResponse.new(:raw_json => String)
def initialize(options = {})
json_input = options[:raw_json]
hash = JSON.parse(json_input)
hash.each do |line|
case line[0]
when "meta"
@meta = line[1]
when "data"
@data = line[1]
else
# Doh?
end
end
end
end

37
app/models/post_data.rb Normal file
View file

@ -0,0 +1,37 @@
# Contains data of a single ADN post.
class PostData
attr_accessor :post_id
attr_accessor :created_at
def initialize
@post_id = 1
@created_at = Date.new(1970,1,1)
end
# in: array of posts
# returns: class instance with the topmost (= most recent) post from the array
# assumes: the array of posts is sorted in reverse alphabetical order.
def self.most_recent_post(post_array)
post_id = 1
created_at = Time.new(1970,1,1)
post = PostData.new
post_array.each do |line|
line.each do |elem|
puts "--- post_data.line_elem: " + elem.to_s
case elem[0]
when "id"
post_id = elem[1].to_i
when "created_at"
created_at = Time.parse(elem[1])
else
# (currently) not of interest
end
if post_id > post.post_id then
post.post_id = post_id
post.created_at = created_at
end
end
end
post
end
end

View file

@ -10,11 +10,11 @@ class Repost < ActiveRecord::Base
config["user_token"]
end
# Get authorized user's stream. The topmost post should be the latest repost.
# Get the topmost post from the authorized user's stream.
# http://developers.app.net/docs/resources/post/streams/#retrieve-posts-created-by-a-user
def self.get_user_posts(access_token)
uri = "https://alpha-api.app.net/stream/0/users/me/posts"
self.make_api_call(uri, access_token)
def self.get_most_recent_user_post(access_token)
uri = "https://alpha-api.app.net/stream/0/users/me/posts?count=1"
self.make_api_get_call(uri, access_token)
end