Class: SinatraClient

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra_client.rb,
lib/sinatra_client/version.rb

Constant Summary collapse

VERSION =
"0.0.5"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_user_id = nil) ⇒ SinatraClient

Returns a new instance of SinatraClient.



9
10
11
12
# File 'lib/sinatra_client.rb', line 9

def initialize(current_user_id = nil)
  @current_user_id = current_user_id
  @url = URI::HTTP.build(userinfo: "#{ENV['API_NAME']}:#{ENV['API_PASSWORD']}", host: ENV['SINATRA_HOST'], port: ENV['SINATRA_PORT'], path: '/api/v1')
end

Instance Attribute Details

#current_user_idObject

Returns the value of attribute current_user_id.



7
8
9
# File 'lib/sinatra_client.rb', line 7

def current_user_id
  @current_user_id
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/sinatra_client.rb', line 7

def url
  @url
end

Instance Method Details

#create_comment_for_post(post_id, comment) ⇒ Object



48
49
50
51
# File 'lib/sinatra_client.rb', line 48

def create_comment_for_post(post_id, comment)
  url.path << "/posts/#{post_id}/comments"
  parse RestClient.post(url.to_s, comment)
end

#create_or_delete_like(post_id, liker_id) ⇒ Object



59
60
61
62
# File 'lib/sinatra_client.rb', line 59

def create_or_delete_like(post_id, liker_id)
  url.path << "/posts/#{post_id}/toggle_like"
  parse RestClient.post(url.to_s, liker_id)
end

#create_post(post) ⇒ Object



20
21
22
23
# File 'lib/sinatra_client.rb', line 20

def create_post(post)
  url.path << '/posts'
  parse RestClient.post(url.to_s, post)
end

#delete_comment(post_id, comment_guid) ⇒ Object



53
54
55
56
57
# File 'lib/sinatra_client.rb', line 53

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)
end

#delete_post(post_id) ⇒ Object



25
26
27
28
29
# File 'lib/sinatra_client.rb', line 25

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)
end

#delete_posts_for(postable_id, postable_type) ⇒ Object



31
32
33
34
35
# File 'lib/sinatra_client.rb', line 31

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)
end

#get_group_posts(group_id, page) ⇒ Object



37
38
39
40
41
# File 'lib/sinatra_client.rb', line 37

def get_group_posts(group_id, page)
  url.path << "/groups/#{group_id}/posts"
  url.query = URI.encode_www_form({ page: page })
  parse RestClient.get(url.to_s)
end

#get_likers(post_id) ⇒ Object



64
65
66
67
# File 'lib/sinatra_client.rb', line 64

def get_likers(post_id)
  url.path << "/posts/#{post_id}/likers"
  parse RestClient.get(url.to_s)
end

#get_user_posts(user_id, page) ⇒ Object



14
15
16
17
18
# File 'lib/sinatra_client.rb', line 14

def get_user_posts(user_id, page)
  url.path << "/users/#{user_id}/posts"
  url.query = URI.encode_www_form({ page: page })
  parse RestClient.get(url.to_s)
end

#get_user_posts_comment(post_id) ⇒ Object



43
44
45
46
# File 'lib/sinatra_client.rb', line 43

def get_user_posts_comment(post_id)
  url.path << "/posts/#{post_id}/comments"
  parse RestClient.get(url.to_s)
end