class Bitly4R::Response
Response
¶ ↑
A response from the bit.ly API.
The to_s
method should always return the ‘likely’ value, assuming that there is a likely one. For example:
-
shorten => shortUrl
-
expand => longUrl
All other response values can be retrieved via method_missing.
NOTE: This is not a sophisticated XML parser. It’s just Regexp’s, with handling for CDATA blocks.
Attributes
The body of the bit.ly API response, as XML
Public Class Methods
Constructs a bit.ly API response wrapper.
response can be:
-
a JSON String, which becomes the body
-
a Net::HTTPResponse, in which case its body is extracted and expected to be in JSON format
to_s_sym is optional, and it references the property which will become the ‘#to_s` value of this Response
. It can be either camel-case or underscored. See method_missing.
# File lib/bitly4r/objects.rb 77 def initialize(response, to_s_sym=nil) 78 response = response.body if Net::HTTPResponse === response 79 @body = response 80 @to_s_sym = to_s_sym 81 end
Public Instance Methods
Provides access to the other properties of the response body via camel-case or underscored names. For example, longUrl
and long_url
are equivalent. If no such property exists, you’ll get nil.
# File lib/bitly4r/objects.rb 86 def method_missing(sym, *args) 87 begin 88 json = JSON.parse(@body || '{}') 89 rescue JSON::ParserError => e 90 return nil 91 end 92 93 # their 'v4/*' JSON convention is snake_case 94 sym = Utility::decamelize(sym.to_s) 95 return json[sym] 96 end
Provides the ‘likely’ value from the response.
# File lib/bitly4r/objects.rb 99 def to_s 100 @to_s_sym ? self.__send__(@to_s_sym) : super 101 end
Provides the ‘likely’ value from the response, as a symbol.
# File lib/bitly4r/objects.rb 105 def to_sym 106 return super unless @to_s_sym 107 value = self.to_s 108 return value.nil? ? value : value.to_sym 109 end