Class: Coach4rb::Client

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

Overview

This class provides a simple http client.

Instance Method Summary collapse

Constructor Details

#initialize(a_hash) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
# File 'lib/coach4rb/client.rb', line 8

def initialize(a_hash)
  @service_uri = Addressable::URI.new a_hash
  @basic_options = {
      accept: :json,
      content_type: :xml
  }
end

Instance Method Details

#delete(url, options = {}, &block) ⇒ String

Performs a delete request.

Parameters:

  • url (String)
  • options (Hash) (defaults to: {})
  • block (Block)

Returns:

  • (String)

    the payload of the response as string



105
106
107
108
109
110
111
112
# File 'lib/coach4rb/client.rb', line 105

def delete(url, options={}, &block)
  http_options = options.merge(@basic_options)
  if block_given?
    RestClient.delete(url, http_options, &block)
  else
    RestClient.delete(url, http_options)
  end
end

#get(url, options = {}, &block) ⇒ String

Performs a get request.

Parameters:

  • url (String)
  • options (Hash) (defaults to: {})
  • block (Block)

Returns:

  • (String)

    the payload of the response as string



53
54
55
56
57
58
59
60
# File 'lib/coach4rb/client.rb', line 53

def get(url, options={}, &block)
  http_options = options.merge(@basic_options)
  if block_given?
    RestClient.get(url, http_options, &block)
  else
    RestClient.get(url, http_options)
  end
end

#hostObject



37
38
39
# File 'lib/coach4rb/client.rb', line 37

def host
  @service_uri.host
end

#pathObject



22
23
24
# File 'lib/coach4rb/client.rb', line 22

def path
  @service_uri.path
end

#portObject



32
33
34
# File 'lib/coach4rb/client.rb', line 32

def port
  @service_uri.port
end

#post(url, payload, options = {}, &block) ⇒ String

Performs a post request.

Parameters:

  • url (String)
  • payload (String)
  • block (Block)

Returns:

  • (String)

    the payload of the response as string



88
89
90
91
92
93
94
95
# File 'lib/coach4rb/client.rb', line 88

def post(url, payload, options={}, &block)
  http_options = options.merge(@basic_options)
  if block_given?
    RestClient.post(url, payload, http_options, &block)
  else
    RestClient.post(url, payload, http_options)
  end
end

#put(url, payload, options = {}, &block) ⇒ String

Performs a put request.

Parameters:

  • url (String)
  • payload (String)
  • block (Block)

Returns:

  • (String)

    the payload of the response as string



70
71
72
73
74
75
76
77
78
# File 'lib/coach4rb/client.rb', line 70

def put(url, payload, options={}, &block)
  http_options = options.merge(@basic_options)
  if block_given?
    RestClient.put(url, payload, http_options, &block)
  else
    RestClient.put(url, payload, http_options)
  end

end

#schemeObject



42
43
44
# File 'lib/coach4rb/client.rb', line 42

def scheme
  @service_uri.scheme
end

#service_uriObject



17
18
19
# File 'lib/coach4rb/client.rb', line 17

def service_uri
  @service_uri
end

#siteObject



27
28
29
# File 'lib/coach4rb/client.rb', line 27

def site
  @service_uri.site
end