Class: Wechat::Response
- Inherits:
-
Object
- Object
- Wechat::Response
- Defined in:
- lib/wechat/response.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#data ⇒ Object
Returns the value of attribute data.
-
#parse_as ⇒ Object
Returns the value of attribute parse_as.
Instance Method Summary collapse
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
- #result ⇒ Object
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
#code ⇒ Object
Returns the value of attribute code.
4 5 6 |
# File 'lib/wechat/response.rb', line 4 def code @code end |
#data ⇒ Object
Returns the value of attribute data.
4 5 6 |
# File 'lib/wechat/response.rb', line 4 def data @data end |
#parse_as ⇒ Object
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
#result ⇒ Object
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 |