Class: Grendel::Client

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

Defined Under Namespace

Classes: HTTPException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri, options = {}) ⇒ Client

Create a new Grendel client instance



8
9
10
11
12
# File 'lib/grendel/client.rb', line 8

def initialize(base_uri, options = {})
  @base_uri = base_uri
  @debug = options[:debug]
  @debug_output = options[:debug_output] || $stderr
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



5
6
7
# File 'lib/grendel/client.rb', line 5

def base_uri
  @base_uri
end

#debugObject

Returns the value of attribute debug.



4
5
6
# File 'lib/grendel/client.rb', line 4

def debug
  @debug
end

#debug_outputObject

Returns the value of attribute debug_output.



4
5
6
# File 'lib/grendel/client.rb', line 4

def debug_output
  @debug_output
end

Instance Method Details

#delete(uri, options = {}) ⇒ Object

Raises:



45
46
47
48
49
# File 'lib/grendel/client.rb', line 45

def delete(uri, options = {})
  options.merge!(:debug_output => @debug_output) if @debug
  response = HTTParty.delete(@base_uri + uri, options)
  raise HTTPException.new(response) if response.code >= 400
end

#get(uri, options = {}) ⇒ Object

Raises:



14
15
16
17
18
19
# File 'lib/grendel/client.rb', line 14

def get(uri, options = {})
  options.merge!(:debug_output => @debug_output) if @debug
  response = HTTParty.get(@base_uri + uri, options)
  raise HTTPException.new(response) if response.code >= 400
  return response
end

#post(uri, data = {}, options = {}) ⇒ Object

Raises:



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/grendel/client.rb', line 21

def post(uri, data = {}, options = {})
  data = data.to_json unless options.delete(:raw_data)        
  options.merge!(
      :body => data,
      :headers => {'Content-Type' => 'application/json'}
  )
  options.merge!(:debug_output => @debug_output) if @debug
  response = HTTParty.post(@base_uri + uri, options)
  raise HTTPException.new(response) if response.code >= 400
  return response
end

#put(uri, data = {}, options = {}) ⇒ Object

Raises:



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/grendel/client.rb', line 33

def put(uri, data = {}, options = {})
  data = data.to_json unless options.delete(:raw_data)        
  options = { 
    :body => data,
    :headers => {'Content-Type' => 'application/json'}
  }.update(options)
  options.merge!(:debug_output => @debug_output) if @debug
  response = HTTParty.put(@base_uri + uri, options)
  raise HTTPException.new(response) if response.code >= 400
  return response
end

#usersObject



51
52
53
# File 'lib/grendel/client.rb', line 51

def users
  UserManager.new(self)
end