class Shortdiary::API
Public Class Methods
new(*args)
click to toggle source
# File lib/shortdiary.rb, line 125 def initialize(*args) @username = args[0] @password = args[1] # Send & discard request to check login credentials get_request('posts/1/') nil end
Public Instance Methods
format_post(raw_post)
click to toggle source
Should probably be part of the Post
class
# File lib/shortdiary.rb, line 136 def format_post(raw_post) post = Post.new # Not pretty, but it does the job. raw_post.keys.select{ |key, _| begin post.send("#{key}=", raw_post[key]) rescue NoMethodError; end } post.date = Date.strptime(raw_post['date'], '%Y-%m-%d') post.api = self post end
get_post_for(date)
click to toggle source
# File lib/shortdiary.rb, line 169 def get_post_for(date) # The API endpoint should allow filtering. posts.select {|post| post.date == date }[0] end
get_request(api_endpoint)
click to toggle source
# File lib/shortdiary.rb, line 113 def get_request(api_endpoint) send_request(api_endpoint, 'GET') end
handle_server_error(res)
click to toggle source
# File lib/shortdiary.rb, line 77 def handle_server_error(res) begin json_data = JSON.parse res.body rescue JSON::ParserError raise ServerError, res.body end raise AuthenticationError if res.is_a?(Net::HTTPUnauthorized) raise ServerError, json_data['Error'] if json_data['Error'] # Fallback raise ServerError, res.body end
new_post()
click to toggle source
This doesn’t really make any sense at all
# File lib/shortdiary.rb, line 152 def new_post() created_post = Post.new created_post.api = self created_post end
post_request(api_endpoint, data)
click to toggle source
# File lib/shortdiary.rb, line 117 def post_request(api_endpoint, data) send_request(api_endpoint, 'POST', data) end
posts()
click to toggle source
# File lib/shortdiary.rb, line 158 def posts() raw_posts = get_request('posts/') api_posts = [] raw_posts.each {|raw_post| api_posts << format_post(raw_post) } api_posts end
put_request(api_endpoint, data)
click to toggle source
# File lib/shortdiary.rb, line 121 def put_request(api_endpoint, data) send_request(api_endpoint, 'PUT', data) end
random_public()
click to toggle source
# File lib/shortdiary.rb, line 174 def random_public() post = get_request('public/') format_post(post) end
send_request(api_endpoint, type = 'GET', data = {})
click to toggle source
Todo open one permanent connection and keep it alive
# File lib/shortdiary.rb, line 92 def send_request(api_endpoint, type = 'GET', data = {}) # Note: All 'real' API applications should probably use OAuth instead… uri = URI("#{API_ROOT}/#{api_endpoint}") case type when 'POST' then req = Net::HTTP::Post.new(uri) when 'PUT' then req = Net::HTTP::Put.new(uri) else req = Net::HTTP::Get.new(uri) end req.set_form_data(data) if type != 'GET' req.basic_auth(@username, @password) res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') {|http| http.request(req) } handle_server_error(res) if not res.is_a?(Net::HTTPSuccess) JSON.parse res.body end