Class: RubyMeetup::AuthenticatedClient

Inherits:
Client
  • Object
show all
Defined in:
lib/authenticated_client.rb

Overview

This class can be used to read and write data on behalf of an authenticated user. An OAuth 2 access_token is given once user is authenticated. One of the methods is to use the ominauth-meetup gem.

An access_token is user-specific so we configure it per Client instance like so,

> client = RubyMeetup::AuthenticatedClient.new
> client.access_token = user.access_token

Then make a request,

> json_string = client.get_path("/2/groups", {:member_id => user.member_id})

The same client instance may be used to make multiple requests. Just supply a different @path and/or @options parameters as required.


Copyright © 2013 Long On, released under the MIT license

Instance Attribute Summary collapse

Attributes inherited from Client

#path

Instance Method Summary collapse

Methods inherited from Client

#get, #get_path, site, site=

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



24
25
26
# File 'lib/authenticated_client.rb', line 24

def access_token
  @access_token
end

Instance Method Details

#delete(options = {}) ⇒ Object

Make a DELETE API call with the current path value and @options. Return true if successful, otherwise an Exception

TODO - test AuthenticatedClient.delete()


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/authenticated_client.rb', line 50

def delete(options={})
  uri = new_uri
  params = merge_params(options)
  uri.query = URI.encode_www_form(params)

  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Delete.new(uri) # uri or uri.request_uri?
  response = http.request(request)
  unless response.is_a?(Net::HTTPSuccess)
    raise "#{response.code} #{response.message}\n#{response.body}"
  end
  true
end

#post(options) ⇒ Object

Make a POST API call with the current path value and @options. Return a JSON string if successful, otherwise an Exception

TODO - test AuthenticatedClient.post()


31
32
33
34
35
36
37
38
39
# File 'lib/authenticated_client.rb', line 31

def post(options)
  uri = new_uri
  params = merge_params(options)
  response = Net::HTTP.post_form(uri, params)
  unless response.is_a?(Net::HTTPSuccess)
    raise "#{response.code} #{response.message}\n#{response.body}"
  end
  response.body
end

#put(options) ⇒ Object

Fake a PUT API call since meetup.com only support POST method

TODO - test AuthenticatedClient.put()


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

def put(options)
  post(options)
end

#to_sObject

:nodoc:



65
66
67
68
# File 'lib/authenticated_client.rb', line 65

def to_s
  s = super
  s << ", access_token=#{short_token}"
end