I have a question.
This is my class:
class Provider
include HTTParty
CONFIG = Rails.configuration.mediaset[Rails.env]
def self.category
response = HTTParty.get(CONFIG['category_url'])
category_hash = JSON.parse response.body
if category_hash['resultCode'] == 'OK'
return convert_categories(category_hash)
end
end
def convert_categories(cat_hash)
h = cat_hash.find_all_values_for('categoryList')
category_array = h.map { |c| c.except!('categoryList') }
end
end
When I try to call 'Provider.category' the result is:
undefined method `convert_categories' for Provider:Class
How can I add a function to process my hash?
As has been suggested, just make your "simple function" a class method as well...
def self.convert_categories(cat_hash)
h = cat_hash.find_all_values_for('categoryList')
h.map { |c| c.except!('categoryList') }
return h
end
do that, and the code will work.
Without the self. the method is an instance method and can only be called on an object of the Provider class.
Although admittedly the h.map... line is pointless. The result isn't saved anywhere, you're still returning the original h object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With