Class: DiasporaApi::Client
- Inherits:
-
Object
- Object
- DiasporaApi::Client
- Defined in:
- lib/diaspora-api.rb
Instance Method Summary collapse
- #get_attributes ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #login(podhost, username, password) ⇒ Object
- #post(msg, aspect) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
34 35 36 37 |
# File 'lib/diaspora-api.rb', line 34 def initialize @providername = "d*-rubygem" @cookie = nil end |
Instance Method Details
#get_attributes ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/diaspora-api.rb', line 92 def get_attributes uri = URI.parse(@podhost + "/stream") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Get.new(uri.request_uri) request['Cookie'] = @cookie response = http.request(request) 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 puts "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 |
#login(podhost, username, password) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/diaspora-api.rb', line 39 def login(podhost, username, password) @podhost = podhost uri = URI.parse(podhost + "/users/sign_in") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Get.new(uri.request_uri) response = http.request(request) if response.code != "200" puts "Server returned error " + response.code end = /_diaspora_session=[[[:alnum:]]%-]+; /.match(response.response['set-cookie']) atok = /\<input name="authenticity_token" type="hidden" value="([[[:alnum:]][[:punct:]]]+)" \/\>/.match(response.body)[1] request = Net::HTTP::Post.new(uri.request_uri) request.set_form_data('utf8' => '✓', 'user[username]' => username, 'user[password]' => password, 'user[remember_me]' => 1, 'commit' => 'Signin', 'authenticity_token' => atok) request['Cookie'] = response = http.request(request) if response.code.to_i >= 400 puts "Login failed. Server replied with code " + response.code return false else @cookie = /remember_user_token=[[[:alnum:]]%-]+; /.match(response.response['set-cookie']) get_attributes return true end end |
#post(msg, aspect) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/diaspora-api.rb', line 71 def post(msg, aspect) if(aspect != "public") for asp in @attributes["aspects"] if(aspect == asp["name"]) aspect = asp["id"].to_s end end end uri = URI.parse(@podhost + "/status_messages") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Post.new(uri.request_uri,initheader = {'Content-Type' =>'application/json'}) request['Cookie'] = @cookie request.body = { "status_message" => { "text" => msg, "provider_display_name" => @providername }, "aspect_ids" => aspect}.to_json return http.request(request) end |