lib/sinatra_client.rb in sinatra_client-0.0.2 vs lib/sinatra_client.rb in sinatra_client-0.0.3
- old
+ new
@@ -2,75 +2,70 @@
require "rest-client"
require "dotenv/load"
require 'uri'
class SinatraClient
- attr_accessor :current_user_id, :url, :auth
+ attr_accessor :current_user_id, :url
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
+ @url = URI::HTTP.build(userinfo: "#{ENV['API_NAME']}:#{ENV['API_PASSWORD']}", host: ENV['SINATRA_HOST'], port: ENV['SINATRA_PORT'], path: '/api/v1')
end
def get_user_posts(user_id)
url.path << "/users/#{user_id}/posts"
- parse RestClient.get(url.to_s, auth_header)
+ parse RestClient.get(url.to_s)
end
def create_post(post)
url.path << '/posts'
- parse RestClient.post(url.to_s, post, auth_header)
+ parse RestClient.post(url.to_s, post)
end
def delete_post(post_id)
url.path << "/posts/#{post_id}"
url.query = URI.encode_www_form({ current_user: current_user_id })
- parse RestClient.delete(url.to_s, auth_header)
+ parse RestClient.delete(url.to_s)
end
def delete_posts_for(postable_id, postable_type)
url.path << "/postable/#{postable_id}/posts"
url.query = URI.encode_www_form({ postable_type: postable_type })
- parse RestClient.delete(url.to_s, auth_header)
+ parse RestClient.delete(url.to_s)
end
def get_group_posts(group_id)
url.path << "/groups/#{group_id}/posts"
- parse RestClient.get(url.to_s, auth_header)
+ parse RestClient.get(url.to_s)
end
def get_user_posts_comment(post_id)
url.path << "/posts/#{post_id}/comments"
- parse RestClient.get(url.to_s, auth_header)
+ parse RestClient.get(url.to_s)
end
def create_comment_for_post(post_id, comment)
url.path << "/posts/#{post_id}/comments"
- parse RestClient.post(url.to_s, comment, auth_header)
+ parse RestClient.post(url.to_s, comment)
end
def delete_comment(post_id, comment_guid)
url.path << "/posts/#{post_id}/comments/#{comment_guid}"
url.query = URI.encode_www_form({ current_user: current_user_id })
- parse RestClient.delete(url.to_s, auth_header)
+ parse RestClient.delete(url.to_s)
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, auth_header)
+ parse RestClient.post(url.to_s, liker_id)
end
def get_likers(post_id)
url.path << "/posts/#{post_id}/likers"
- parse RestClient.get(url.to_s, auth_header)
+ parse RestClient.get(url.to_s)
end
private
-
- def auth_header
- { Authorization: auth }
- end
def parse(response)
JSON.parse(response.body)
end
end