adn-repostmentions/app/controllers/reposts_controller.rb
2013-12-12 22:48:15 +01:00

70 lines
2.1 KiB
Ruby

require 'erb' # for url_encode()
require 'net/http'
require 'uri'
class RepostsController < ApplicationController
def index
config = YAML.load_file('config/client.yml')
client_id = config["client_id"]
client_secret = config["client_secret"]
# connect to ADN via a user token
uri = URI.parse("https://account.app.net/oauth/authenticate")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = Net::HTTP::post_form( uri, {"client_id" => client_id, "response_type" => "code", "redirect_uri" => "http://repostmentions.sr-rolando.com/", "scope" => "stream"})
# [temp] status
@res_code = response.code
@res_message = response.message
@res_class = response.class.name
if not response.is_a?(Net::HTTPSuccess)
flash[:error] = "Could not connect to ADN."
return
end
# ...
end
# TODO: to secure the cronjob call test for 'InvokationPassword' header.
# store this password in client.yml for comparison
# crontab: curl -H 'InvokationPassword: ...' http://localhost/reposts/new
def new
# get user auth token
access_token = Repost.obtain_user_token
# retrieve latest reposts. this uses the real ADN stream.
res = Repost.get_most_recent_user_post(access_token)
api_res = ApiResponse.new(:raw_json => res.body)
latest_post = PostData.most_recent_post(api_res.data)
# retrieve new mentions.
res = Repost.get_mentions_stream(access_token)
api_res = ApiResponse.new(:raw_json => res.body)
todo_posts_array = PostData.extract_not_yet_reposted(api_res.data, latest_post.post_id)
Repost.repost_this_array(todo_posts_array, access_token)
# TODO 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