Class: Convore::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/convore/base.rb

Direct Known Subclasses

Account, Group, Message, Topic, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

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

Raises:

  • (NoMethodError)


10
11
12
13
14
15
16
17
18
# File 'lib/convore/base.rb', line 10

def method_missing(method, *args)
	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

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/convore/base.rb', line 3

def attributes
  @attributes
end

#passwordObject

Returns the value of attribute password.



3
4
5
# File 'lib/convore/base.rb', line 3

def password
  @password
end

#usernameObject

Returns the value of attribute username.



3
4
5
# File 'lib/convore/base.rb', line 3

def username
  @username
end

Class Method Details

.apiObject



52
53
54
# File 'lib/convore/base.rb', line 52

def api
	nil
end

.find(rid) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/convore/base.rb', line 56

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/convore/base.rb', line 32

def from_json(json)
	return nil if !json

	object = new
	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

.get_class(key) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/convore/base.rb', line 23

def get_class(key)
	case key
	when 'message' then	Message
	when 'user', 'creator', 'mentioned_user' then User
	when 'group' then Group
	when 'topic' then Topic
	end
end