Class: Wechat::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/wechat/response.rb', line 6

def initialize(response)
  type = response.headers[:content_type] 

  case type
  when /image|audio|video/
    self.parse_as = :file
  else
    self.parse_as = :json
  end
  self.data = response
  self.code = response.code 
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



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

def code
  @code
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#parse_asObject

Returns the value of attribute parse_as.



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

def parse_as
  @parse_as
end

Instance Method Details

#resultObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/wechat/response.rb', line 19

def result
  if code == 200        
    case parse_as
    when :file
      file = Tempfile.new(["tmp", ".#{data.headers[:content_type].split('/')[1] }"])
      file.binmode
      file.write(data.body)
      file.close
      file.path
    when :json
      JSON.parse(data)           
    end

  elsif !code.nil? && code == 401
    raise Wechat::Unauthorized.new(code)
  else
    raise Wechat::BadRequest.new(code)
  end
end