Class: Convore::Base
- Inherits:
-
Object
- Object
- Convore::Base
- Defined in:
- lib/convore/base.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#password ⇒ Object
Returns the value of attribute password.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #method_missing(method, *args) ⇒ Object
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
5 6 7 |
# File 'lib/convore/base.rb', line 5 def initialize @attributes ||= {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/convore/base.rb', line 10 def method_missing(method, *args) @attributes = {} if @attributes.nil? if method.match(/\=/) @attributes[method.to_s.gsub(/\=$/, '').to_sym] = args.first return true else return @attributes[method] if @attributes[method] end raise NoMethodError.new("no such method: #{method}") end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
3 4 5 |
# File 'lib/convore/base.rb', line 3 def attributes @attributes end |
#password ⇒ Object
Returns the value of attribute password.
3 4 5 |
# File 'lib/convore/base.rb', line 3 def password @password end |
#username ⇒ Object
Returns the value of attribute username.
3 4 5 |
# File 'lib/convore/base.rb', line 3 def username @username end |
Class Method Details
.api ⇒ Object
53 54 55 |
# File 'lib/convore/base.rb', line 53 def api nil end |
.find(rid) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/convore/base.rb', line 57 def find(rid) return false if !api || !rid.is_a?(Numeric) api_uri = "/api/#{api[-1] == 's' ? api : "#{api}s"}/#{rid}.json" response = Convore::API.get(api_uri) json = JSON.parse(response.body) from_json(json[api]) end |
.from_json(json) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/convore/base.rb', line 33 def from_json(json) return nil if !json object = new(@username, @password) json.each {|key, val| key_class = get_class(key) value = if val.is_a?(Hash) && key_class key_class.from_json(json[key]) elsif val.is_a?(Numeric) && key_class && key_class.api key_class.find(val) else val end object.send("#{key}=", value) } object end |