lib/sinatra_client.rb in sinatra_client-0.0.1 vs lib/sinatra_client.rb in sinatra_client-0.0.2
- old
+ new
@@ -1,71 +1,76 @@
require "sinatra_client/version"
require "rest-client"
require "dotenv/load"
-require "active_record"
+require 'uri'
class SinatraClient
- attr_accessor :current_user, :url
+ attr_accessor :current_user_id, :url, :auth
- def initialize(current_user = nil)
- @current_user = current_user
+ def initialize(current_user_id = nil)
+ @current_user_id = current_user_id
@url = URI::HTTP.build(host: ENV['SINATRA_HOST'], port: ENV['SINATRA_PORT'], path: '/api/v1')
+ @auth = 'Basic ' + Base64.encode64( 'API_NAME:API_PASSWORD' ).chomp
end
def get_user_posts(user_id)
url.path << "/users/#{user_id}/posts"
- parse RestClient.get(url.to_s)
+ parse RestClient.get(url.to_s, auth_header)
end
def create_post(post)
url.path << '/posts'
- parse RestClient.post(url.to_s, post)
+ parse RestClient.post(url.to_s, post, auth_header)
end
def delete_post(post_id)
url.path << "/posts/#{post_id}"
- url.query = { current_user: current_user&.id }.to_query
- parse RestClient.delete(url.to_s)
+ url.query = URI.encode_www_form({ current_user: current_user_id })
+ parse RestClient.delete(url.to_s, auth_header)
end
def delete_posts_for(postable_id, postable_type)
url.path << "/postable/#{postable_id}/posts"
- url.query = { postable_type: postable_type }.to_query
- parse RestClient.delete(url.to_s)
+ url.query = URI.encode_www_form({ postable_type: postable_type })
+ parse RestClient.delete(url.to_s, auth_header)
end
def get_group_posts(group_id)
url.path << "/groups/#{group_id}/posts"
- parse RestClient.get(url.to_s)
+ parse RestClient.get(url.to_s, auth_header)
end
def get_user_posts_comment(post_id)
url.path << "/posts/#{post_id}/comments"
- parse RestClient.get(url.to_s)
+ parse RestClient.get(url.to_s, auth_header)
end
def create_comment_for_post(post_id, comment)
url.path << "/posts/#{post_id}/comments"
- parse RestClient.post(url.to_s, comment)
+ parse RestClient.post(url.to_s, comment, auth_header)
end
def delete_comment(post_id, comment_guid)
url.path << "/posts/#{post_id}/comments/#{comment_guid}"
- url.query = { current_user: current_user&.id }.to_query
- parse RestClient.delete(url.to_s)
+ url.query = URI.encode_www_form({ current_user: current_user_id })
+ parse RestClient.delete(url.to_s, auth_header)
end
def create_or_delete_like(post_id, liker_id)
url.path << "/posts/#{post_id}/toggle_like"
- parse RestClient.post(url.to_s, liker_id)
+ parse RestClient.post(url.to_s, liker_id, auth_header)
end
def get_likers(post_id)
url.path << "/posts/#{post_id}/likers"
- parse RestClient.get(url.to_s)
+ parse RestClient.get(url.to_s, auth_header)
end
private
+
+ def auth_header
+ { Authorization: auth }
+ end
def parse(response)
JSON.parse(response.body)
end
end