diaspora-api 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/diaspora-api.rb +59 -33
  2. metadata +2 -2
@@ -21,49 +21,85 @@ require "net/http"
21
21
  require "net/https"
22
22
  require "uri"
23
23
  require "json"
24
+ require "logger"
24
25
 
25
26
  class DiasporaApi
26
27
  end
27
28
 
28
29
  class DiasporaApi::Client
29
- @providername
30
+ attr_writer :providername, :verify_mode, :proxy_host, :proxy_port
31
+
30
32
  @attributes
31
33
  @podhost
32
34
  @cookie
35
+ @atok
36
+ @_diaspora_session
37
+ @logger
38
+
39
+ def log_level=(level)
40
+ @logger.level = level
41
+ end
33
42
 
34
43
  def initialize
35
44
  @providername = "d*-rubygem"
36
45
  @cookie = nil
46
+ @_diaspora_session = nil
47
+ @proxy_host = nil
48
+ @proxy_port = nil
49
+ @verify_mode = nil
50
+ @logger = Logger.new(STDOUT)
51
+ @logger.datetime_format = "%H:%H:%S"
52
+ @logger.level = Logger::INFO
53
+ @logger.info("diaspora-api gem initialized")
54
+ end
55
+
56
+ def send_request(request)
57
+ request['Cookie']="#{@_diaspora_session}${@cookie}"
58
+ uri = URI.parse(@podhost)
59
+
60
+ response = nil
61
+ http = Net::HTTP.new(uri.host, uri.port,@proxy_host,@proxy_port)
62
+ http.use_ssl = true
63
+ if(@verify_mode != nil)
64
+ http.verify_mode = @verify_mode
65
+ end
66
+ response = http.request(request)
67
+
68
+ @_diaspora_session = /_diaspora_session=[[[:alnum:]]%-]+; /.match(response.response['set-cookie']).to_s
69
+ return response
70
+ end
71
+
72
+ def fetch_csrf(response)
73
+ atok_tag = /<meta[ a-zA-Z0-9=\/+"]+name=\"csrf-token\"[ a-zA-Z0-9=\/+"]+\/>/.match(response.body)[0]
74
+ @logger.debug("atok_tag:\n#{atok_tag}")
75
+ @atok = /content="([a-zA-Z0-9=\/\\+]+)"/.match(atok_tag)[1]
76
+ @logger.debug("atok:\n#{@atok}")
37
77
  end
38
78
 
39
79
  def login(podhost, username, password)
40
80
  @podhost = podhost
41
- uri = URI.parse(podhost + "/users/sign_in")
42
- http = Net::HTTP.new(uri.host, uri.port)
43
- http.use_ssl = true
44
81
 
45
- request = Net::HTTP::Get.new(uri.request_uri)
82
+ request = Net::HTTP::Get.new("/users/sign_in")
83
+ response = send_request(request)
46
84
 
47
- response = http.request(request)
48
85
  if response.code != "200"
49
- puts "Server returned error " + response.code
86
+ @logger.error("Server returned error " + response.code)
50
87
  end
51
88
 
52
- scookie = /_diaspora_session=[[[:alnum:]]%-]+; /.match(response.response['set-cookie'])
53
- atok = /<input name="authenticity_token" type="hidden" value="([a-zA-Z0-9=\/+]+)" \/>/.match(response.body)[1]
54
-
55
- request = Net::HTTP::Post.new(uri.request_uri)
56
- request.set_form_data('utf8' => '✓', 'user[username]' => username, 'user[password]' => password, 'user[remember_me]' => 1, 'commit' => 'Signin', 'authenticity_token' => atok)
57
- request['Cookie'] = scookie
89
+ fetch_csrf(response)
58
90
 
59
- response = http.request(request)
91
+ request = Net::HTTP::Post.new("/users/sign_in")
92
+ request.set_form_data('utf8' => '✓', 'user[username]' => username, 'user[password]' => password, 'user[remember_me]' => 1, 'commit' => 'Signin', 'authenticity_token' => @atok)
93
+ response = send_request(request)
94
+ @logger.debug("resp: " + response.code)
60
95
 
61
96
  if response.code.to_i >= 400
62
- puts "Login failed. Server replied with code " + response.code
97
+ @logger.error("Login failed. Server replied with code " + response.code)
63
98
  return false
64
99
  else
100
+ @logger.debug(response.response['set-cookie'])
65
101
  if not response.response['set-cookie'].include? "remember_user_token"
66
- puts "Login failed. Wrong password?"
102
+ @logger.error("Login failed. Wrong password?")
67
103
  return false
68
104
  end
69
105
  @cookie = /remember_user_token=[[[:alnum:]]%-]+; /.match(response.response['set-cookie'])
@@ -73,6 +109,7 @@ class DiasporaApi::Client
73
109
  end
74
110
 
75
111
  def post(msg, aspect)
112
+ @logger.debug(@attributes["aspects"])
76
113
  if(aspect != "public")
77
114
  for asp in @attributes["aspects"]
78
115
  if(aspect == asp["name"])
@@ -81,27 +118,16 @@ class DiasporaApi::Client
81
118
  end
82
119
  end
83
120
 
84
- uri = URI.parse(@podhost + "/status_messages")
85
- http = Net::HTTP.new(uri.host, uri.port)
86
- http.use_ssl = true
87
-
88
- request = Net::HTTP::Post.new(uri.request_uri,initheader = {'Content-Type' =>'application/json'})
89
- request['Cookie'] = @cookie
90
-
121
+ request = Net::HTTP::Post.new("/status_messages",initheader = {'Content-Type' =>'application/json','accept' => 'application/json', 'x-csrf-token' => @atok})
91
122
  request.body = { "status_message" => { "text" => msg, "provider_display_name" => @providername }, "aspect_ids" => aspect}.to_json
92
-
93
- return http.request(request)
123
+ return send_request(request)
94
124
  end
95
125
 
96
126
  def get_attributes
97
- uri = URI.parse(@podhost + "/stream")
98
- http = Net::HTTP.new(uri.host, uri.port)
99
- http.use_ssl = true
127
+ request = Net::HTTP::Get.new("/stream")
128
+ response = send_request(request)
129
+ fetch_csrf(response)
100
130
 
101
- request = Net::HTTP::Get.new(uri.request_uri)
102
- request['Cookie'] = @cookie
103
-
104
- response = http.request(request)
105
131
  names = ["gon.user", "window.current_user_attributes"]
106
132
  i = nil
107
133
 
@@ -112,7 +138,7 @@ class DiasporaApi::Client
112
138
 
113
139
 
114
140
  if i == nil
115
- puts "Unexpected format"
141
+ @logger.error("Unexpected format")
116
142
  else
117
143
  start_json = response.body.index("{", i)
118
144
  i = start_json
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diaspora-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-22 00:00:00.000000000 Z
12
+ date: 2015-05-11 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'Ruby gem to work with Diaspora*. Note: this is not wrapping an official
15
15
  API, since there is no such thing. The gem just makes HTTPS requests and parses