Class: DiasporaApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/diaspora-api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/diaspora-api.rb', line 43

def initialize
	@providername = "d*-rubygem"
	@cookie = nil
	@_diaspora_session = nil
	@proxy_host = nil
	@proxy_port = nil
	@verify_mode = nil
	@logger = Logger.new(STDOUT)
	@logger.datetime_format = "%H:%H:%S"
	@logger.level = Logger::INFO
	@logger.info("diaspora-api gem initialized")
end

Instance Attribute Details

#providername=(value) ⇒ Object (writeonly)

Sets the attribute providername

Parameters:

  • value

    the value to set the attribute providername to.



30
31
32
# File 'lib/diaspora-api.rb', line 30

def providername=(value)
  @providername = value
end

#proxy_host=(value) ⇒ Object (writeonly)

Sets the attribute proxy_host

Parameters:

  • value

    the value to set the attribute proxy_host to.



30
31
32
# File 'lib/diaspora-api.rb', line 30

def proxy_host=(value)
  @proxy_host = value
end

#proxy_port=(value) ⇒ Object (writeonly)

Sets the attribute proxy_port

Parameters:

  • value

    the value to set the attribute proxy_port to.



30
31
32
# File 'lib/diaspora-api.rb', line 30

def proxy_port=(value)
  @proxy_port = value
end

#verify_mode=(value) ⇒ Object (writeonly)

Sets the attribute verify_mode

Parameters:

  • value

    the value to set the attribute verify_mode to.



30
31
32
# File 'lib/diaspora-api.rb', line 30

def verify_mode=(value)
  @verify_mode = value
end

Instance Method Details

#fetch_csrf(response) ⇒ Object



72
73
74
75
76
77
# File 'lib/diaspora-api.rb', line 72

def fetch_csrf(response)
	atok_tag = /<meta[ a-zA-Z0-9=\/+"]+name=\"csrf-token\"[ a-zA-Z0-9=\/+"]+\/>/.match(response.body)[0]
	@logger.debug("atok_tag:\n#{atok_tag}")
	@atok = /content="([a-zA-Z0-9=\/\\+]+)"/.match(atok_tag)[1]
	@logger.debug("atok:\n#{@atok}")
end

#get_attributesObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/diaspora-api.rb', line 126

def get_attributes
	request = Net::HTTP::Get.new("/stream")
	response = send_request(request)
	fetch_csrf(response)

	names = ["gon.user", "window.current_user_attributes"]
	i = nil

	for name in names
		i = response.body.index(name)
		break if i != nil
	end


	if i == nil
		@logger.error("Unexpected format")
	else
		start_json = response.body.index("{", i)
		i = start_json
		n = 0
		begin
			case response.body[i]
				when "{" then n += 1
				when "}" then n -= 1
			end
			i += 1
		end until n == 0
		end_json = i - 1

		@attributes = JSON.parse(response.body[start_json..end_json])
	end
end

#log_level=(level) ⇒ Object



39
40
41
# File 'lib/diaspora-api.rb', line 39

def log_level=(level)
	@logger.level = level
end

#login(podhost, username, password) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/diaspora-api.rb', line 79

def (podhost, username, password)
	@podhost = podhost

	request = Net::HTTP::Get.new("/users/sign_in")
	response = send_request(request)

	if response.code != "200"
		@logger.error("Server returned error " + response.code)
	end

	fetch_csrf(response)

	request = Net::HTTP::Post.new("/users/sign_in")
	request.set_form_data('utf8' => '', 'user[username]' => username, 'user[password]' => password, 'user[remember_me]' => 1, 'commit' => 'Signin', 'authenticity_token' => @atok)
	response = send_request(request)
	@logger.debug("resp: " + response.code)

	if response.code.to_i >= 400
		@logger.error("Login failed. Server replied with code " + response.code)
		return false
	else
		@logger.debug(response.response['set-cookie'])
		if not response.response['set-cookie'].include? "remember_user_token"
			@logger.error("Login failed. Wrong password?")
			return false
		end
		@cookie = /remember_user_token=[[[:alnum:]]%-]+; /.match(response.response['set-cookie'])
		get_attributes
		return true
	end
end

#post(msg, aspect) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/diaspora-api.rb', line 111

def post(msg, aspect)
	@logger.debug(@attributes["aspects"])
	if(aspect != "public")
		for asp in @attributes["aspects"]
			if(aspect == asp["name"])
				aspect = asp["id"].to_s
			end
		end
	end

	request = Net::HTTP::Post.new("/status_messages",initheader = {'Content-Type' =>'application/json','accept' => 'application/json', 'x-csrf-token' => @atok})
	request.body = { "status_message" => { "text" => msg, "provider_display_name" => @providername }, "aspect_ids" => aspect}.to_json
	return send_request(request)
end

#send_request(request) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/diaspora-api.rb', line 56

def send_request(request)
	request['Cookie']="#{@_diaspora_session}${@cookie}"
	uri = URI.parse(@podhost)

	response = nil
	http = Net::HTTP.new(uri.host, uri.port,@proxy_host,@proxy_port)
	http.use_ssl = true
	if(@verify_mode != nil)
		http.verify_mode = @verify_mode
	end
	response = http.request(request)

	@_diaspora_session = /_diaspora_session=[[[:alnum:]]%-]+; /.match(response.response['set-cookie']).to_s
	return response
end