Class: Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, format) ⇒ Client

Returns a new instance of Client.



6
7
8
9
# File 'lib/client.rb', line 6

def initialize(base,format)
  @base = base
  @format = format
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



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

def base
  @base
end

#formatObject

Returns the value of attribute format.



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

def format
  @format
end

#rawObject

Returns the value of attribute raw.



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

def raw
  @raw
end

#unwrapObject

Returns the value of attribute unwrap.



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

def unwrap
  @unwrap
end

Class Method Details

.hash2ostruct(object) ⇒ Object

Recursively create ostruct obj from httparty’s Hash/Array mess.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/client.rb', line 54

def self.hash2ostruct(object)
  return case object
  when Hash
    object = object.clone
    object.each do |key, value|
      object[key] = Client.hash2ostruct(value)
    end
    OpenStruct.new(object)
  when Array
    object = object.clone
    object.map! { |i| Client.hash2ostruct(i) }
  else
    object
  end
end

Instance Method Details

#bodyObject



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

def body
  @raw.body
end

#get(url) ⇒ Object



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

def get(url)
  @raw = HTTParty.get("#{@base}#{url}")
  self
end

#get_and_unwrap(url, unwrap) ⇒ Object



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

def get_and_unwrap(url,unwrap)
  @unwrap = unwrap
  get(url)
end

#headersObject



40
41
42
# File 'lib/client.rb', line 40

def headers
  Client.hash2ostruct(@raw.headers)
end

#post(url, body) ⇒ Object



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

def post(url,body)
  # TODO: remove. This is simply for my testing.
  url = 'http://localhost:3000/open311/requests.xml'
  @raw = HTTParty.post(url, :body => body)
  self
end

#raw_responseObject



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

def raw_response
  Client.hash2ostruct(@raw.parsed_response)
end

#requestObject



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

def request
  @raw.request
end

#responseObject



28
29
30
31
32
33
34
# File 'lib/client.rb', line 28

def response
  if @format == 'xml' and !@unwrap.nil?
    Array(Session.unwrap(raw_response,@unwrap))
  else
    raw_response
  end
end