class Filepreviews::Response
@author Jonah Ruiz <jonah@pixelhipsters.com> Response
module to create callable methods from JSON response
Public Class Methods
new(hash = nil)
click to toggle source
Overrides OpenStruct#initialize to enable deep nesting @param hash [Hash<symbol>] JSON response body @return [Filepreviews::Response] inherited/hack version of OpenStruct @see OpenStruct#initialize
# File lib/filepreviews/response.rb, line 9 def initialize(hash = nil) responsify(hash) end
Public Instance Methods
metadata(js: false)
click to toggle source
Returns metadata response using the url from first response
flag is used to share this method with the CLI version (puerco, I know)
@param js [Boolean] flag to enable json response @return [Filepreviews::Response] api response object
# File lib/filepreviews/response.rb, line 36 def metadata(js: false) url = send(:url) response = Filepreviews::HTTP.default_connection(url).get(nil) json = JSON.parse(response.body) js ? json : self.responsify(json) end
responsify(hash = nil)
click to toggle source
@param hash [Hash<symbol>] JSON response body @return [Filepreviews::Response] inherited/hack version of OpenStruct @see OpenStruct#initialize
# File lib/filepreviews/response.rb, line 21 def responsify(hash = nil) @table, @hash_table = {}, {} if hash hash.each do |k, v| @table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v) @hash_table[k.to_sym] = v end end end
to_h()
click to toggle source
Magical method to give OpenStruct-like class deep nesting capability
# File lib/filepreviews/response.rb, line 14 def to_h @hash_table end