58 lines
1.9 KiB
Ruby
58 lines
1.9 KiB
Ruby
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"
|
|
self.make_api_get_call(uri, access_token)
|
|
end
|
|
|
|
|
|
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
|
|
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
|
|
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
|