From c23ef8e6ce60886c0b77a20f580c7991235d3271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=C3=B1or=20Rolando?= Date: Tue, 10 Dec 2013 23:08:34 +0100 Subject: [PATCH] Determine most recent (re)post. Will use this to select still open (newer) posts for reposting. --- app/controllers/reposts_controller.rb | 30 ++++++++++++++++++++-- app/models/api_response.rb | 20 +++++++++++++++ app/models/post_data.rb | 37 +++++++++++++++++++++++++++ app/models/repost.rb | 8 +++--- 4 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 app/models/api_response.rb create mode 100644 app/models/post_data.rb diff --git a/app/controllers/reposts_controller.rb b/app/controllers/reposts_controller.rb index a149f77..36ab6b9 100644 --- a/app/controllers/reposts_controller.rb +++ b/app/controllers/reposts_controller.rb @@ -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 diff --git a/app/models/api_response.rb b/app/models/api_response.rb new file mode 100644 index 0000000..83fadbd --- /dev/null +++ b/app/models/api_response.rb @@ -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 diff --git a/app/models/post_data.rb b/app/models/post_data.rb new file mode 100644 index 0000000..79f390c --- /dev/null +++ b/app/models/post_data.rb @@ -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 diff --git a/app/models/repost.rb b/app/models/repost.rb index 4e941e7..d71f86d 100644 --- a/app/models/repost.rb +++ b/app/models/repost.rb @@ -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