adn-repostmentions/app/controllers/reposts_controller.rb

56 lines
1.8 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://localhost:3000/", "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.
# TODO: do so: select max(postid) from reposts;
# retrieve new mentions.
# TODO restrict to: fetch( since_id=previous max_id )
res = Repost.get_mentions_stream(access_token)
# 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/
#repost_uri = "https://alpha-api.app.net/stream/0/posts/" + post.id + "/repost"
#latestid = post.id
#end
# store all reposts locally for later reuse.
# TODO insert into reposts values("karlsruher", post.id, latestid)
end
end