Class: Client

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

Overview

Base class for 3taps API client classes.

Constant Summary collapse

DEFAULT_URL =
'http://3taps.net'
DEFAULT_API_PORT =
80
TIMEOUT =
15

Instance Method Summary collapse

Constructor Details

#initialize(baseUrl = DEFAULT_URL, port = DEFAULT_API_PORT) ⇒ Client

Initializes Client class with baseUrl and port parameters. By default DEFAULT_URL and DEFAULT_API_PORT are used. Examples:

Client.new
Client.new("http://3taps.com", 8080)


10
11
12
13
# File 'lib/client/client.rb', line 10

def initialize(baseUrl = DEFAULT_URL, port = DEFAULT_API_PORT)
  @baseURL = baseUrl
  @port = port
end

Instance Method Details

#execute_get(path, params = nil) ⇒ Object

Executes GET request on URL and port with path and params parameters. Example:

execute_get("/search", "data=data")


19
20
21
22
23
24
25
26
27
28
# File 'lib/client/client.rb', line 19

def execute_get( path, params = nil )
  address = params.nil? ? path : path + '?' + params 
  request = Curl::Easy.new("#{@baseURL}:#{@port}" + address) 
  begin
    request.perform
  rescue
    "Some Error with Request."
  end
  request.body_str
end

#execute_post(path, params = nil) ⇒ Object

Executes POST request on URL and port with path and params parameters. Example:

execute_post("search", "data=data")


34
35
36
37
38
39
# File 'lib/client/client.rb', line 34

def execute_post( path, params = nil )
  c = Curl::Easy.new("#{@baseURL}:#{@port}/#{path}")
  param, data = params.split("=",2)
  c.http_post(param.to_s + '=' + c.escape(data.to_s))
  c.body_str
end