Class: RubyMeetup::Client

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

Overview

This class is abstract. It has accessor methods for configuring the API site. By default it is set to api.meetup.com but may be changed using the class setter method site=.

The gem supports API request with two authentication strategies: API Key and OAuth 2 via the concrete classes ApiKeyClient and AuthenticatedClient, respectively. More information can be found in the specific class documentation.

Example usage:

> require 'ruby_meetup'
> RubyMeetup::ApiKeyClient.key = 'abcd000000000000000wxyz'
> client = RubyMeetup::ApiKeyClient.new
> json_string = client.get_path("/2/groups", {:group_id => 390230})
> second_result = client.get({:group_id => 939203})

If successful the captured response is a raw JSON string. The response can then be processed using a JSON parser. Otherwise an exception is raised.

Typically, ApiKeyClient class is used to read data while the AuthenticatedClient class is used for both read and write data, subject to Meetup API permission scopes.


Copyright © 2013 Long On, released under the MIT license

Direct Known Subclasses

ApiKeyClient, AuthenticatedClient

Constant Summary collapse

@@site =
"https://api.meetup.com"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pathObject

Returns the value of attribute path.



39
40
41
# File 'lib/ruby_meetup.rb', line 39

def path
  @path
end

Class Method Details

.siteObject

Retun the configured API endpoint



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

def self.site
  @@site
end

.site=(string) ⇒ Object

Set the global API endpoint



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

def self.site=string
  @@site = string
end

Instance Method Details

#get(options = {}) ⇒ Object

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby_meetup.rb', line 68

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

  Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    request = Net::HTTP::Get.new(uri)
    response = http.request(request)
    unless response.is_a?(Net::HTTPSuccess)
      raise "#{response.code} #{response.message}\n#{response.body}"
    end
    return response.body
  end
end

#get_path(path, options = {}) ⇒ Object

A convenience method to set @path and @options in the same API call. Return a JSON string if successful, otherwise an Exception



56
57
58
59
# File 'lib/ruby_meetup.rb', line 56

def get_path(path, options={})
  self.path = path
  get(options)
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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruby_meetup.rb', line 85

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

  Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    request = Net::HTTP::Post.new(uri)
    response = http.request(request)
    unless response.is_a?(Net::HTTPSuccess)
      raise "#{response.code} #{response.message}\n#{response.body}"
    end
    return response.body
  end
end

#post_path(path, options = {}) ⇒ Object



61
62
63
64
# File 'lib/ruby_meetup.rb', line 61

def post_path(path, options={})
  self.path = path
  post(options)
end

#to_sObject

:nodoc:



101
102
103
# File 'lib/ruby_meetup.rb', line 101

def to_s
  "#{self.class.name}: site=#{@@site}, path=#{path}"
end