require 'uri' require 'net/https' class Repost < ActiveRecord::Base # Returns the user auth token. # FIXME Eventually, this needs to be reworked to use the real auth workflow. # FIXME see http://developers.app.net/docs/authentication/flows/web/#server-side-flow def self.obtain_user_token config = YAML.load_file('config/client.yml') config["user_token"] end # 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_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 # http://developers.app.net/docs/resources/post/streams/#retrieve-posts-mentioning-a-user def self.get_mentions_stream(access_token) uri = "https://alpha-api.app.net/stream/0/users/me/mentions?since_id=16899626" self.make_api_get_call(uri, access_token) end # http://developers.app.net/docs/resources/post/reposts/#repost-a-post def self.repost_this_array(repost_array, access_token) repost_array.each do |post_id| uri = "https://alpha-api.app.net/stream/0/posts/" + post_id.to_s + "/repost" self.make_api_post_call(uri, nil, access_token) end end private def self.make_api_get_call(uri_string, access_token) # uri = URI("https://alpha-api.app.net/stream/0/users/me/mentions") uri = URI(uri_string) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE headers = { 'Authorization' => "Bearer " + access_token } response = http.get(uri.path, headers) return response end def self.make_api_post_call(uri_string, form_data, access_token) # uri = URI('https://alpha-api.app.net/stream/0/posts') uri = URI(uri_string) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE headers = { 'Authorization' => "Bearer " + access_token } path = uri.path.empty? ? "/" : uri.path response = http.post(path, form_data, headers) case response when Net::HTTPSuccess, Net::HTTPRedirection puts "=== response OK" else # / value = " + response.value puts "=== response NOT OK" end end end