tweakphoeus 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -0
- data/lib/tweakphoeus.rb +38 -29
- data/lib/tweakphoeus/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eaba5d14b71470fb676ecb3ce4dc427de2ba103b
|
4
|
+
data.tar.gz: 5551eeeef872bf529967c987b46b5d6d01061998
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90816e09c3fc888a9d4d06a00415d49c1f608f1fa7424b0c2d4498840151d7d1719df1cf953f9015df3d3760a8e614e62648e1bf203cf6dacd39be2dabb7405b
|
7
|
+
data.tar.gz: 5c0d04880cedac96da5df169b02bb3c1e7ea3fff6dd72610e701de7190d032f4eb5c78539793d264ab6d953bcd0e0d82ebee04d431ee91ec8ca8b23cbb1b5809
|
data/README.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
[![Stories in Ready](https://badge.waffle.io/basestylo/Tweakphoeus.png?label=ready&title=Ready)](https://waffle.io/basestylo/Tweakphoeus)
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/tweakphoeus.svg)](https://badge.fury.io/rb/tweakphoeus)
|
3
|
+
[![Circle CI](https://circleci.com/gh/basestylo/Tweakphoeus/tree/master.svg?style=svg)](https://circleci.com/gh/basestylo/Tweakphoeus/tree/master)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/basestylo/Tweakphoeus/badges/gpa.svg)](https://codeclimate.com/github/basestylo/Tweakphoeus)
|
5
|
+
[![Issue Count](https://codeclimate.com/github/basestylo/Tweakphoeus/badges/issue_count.svg)](https://codeclimate.com/github/basestylo/Tweakphoeus)
|
1
6
|
# Tweakphoeus
|
2
7
|
|
3
8
|
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tweakphoeus`. To experiment with that code, run `bin/console` for an interactive prompt.
|
data/lib/tweakphoeus.rb
CHANGED
@@ -5,22 +5,23 @@ class Tweakphoeus
|
|
5
5
|
attr_accessor :cookie_jar
|
6
6
|
|
7
7
|
def initialize()
|
8
|
-
|
8
|
+
@cookie_jar = {}
|
9
9
|
end
|
10
10
|
|
11
|
-
def get url, body: nil,
|
12
|
-
inject_cookies url,
|
13
|
-
response = Typhoeus.get url, body: body, headers:
|
11
|
+
def get url, body: nil, headers: nil, redirect: true
|
12
|
+
inject_cookies url, headers
|
13
|
+
response = Typhoeus.get url, body: body, headers: headers
|
14
14
|
obtain_cookies response
|
15
|
-
response
|
15
|
+
response.headers
|
16
|
+
response = get(redirect_url(response), body: body, headers: headers) if redirect && has_redirect?(response)
|
16
17
|
response
|
17
18
|
end
|
18
19
|
|
19
|
-
def post url, body: nil,
|
20
|
-
inject_cookies url,
|
21
|
-
response = Typhoeus.get url, body: body, headers:
|
20
|
+
def post url, body: nil, headers: nil, redirect: false
|
21
|
+
inject_cookies url, headers
|
22
|
+
response = Typhoeus.get url, body: body, headers: headers
|
22
23
|
obtain_cookies response
|
23
|
-
response = post(redirect_url(response), body,
|
24
|
+
response = post(redirect_url(response), body: body, headers: headers) if redirect && has_redirect?(response)
|
24
25
|
response
|
25
26
|
end
|
26
27
|
|
@@ -28,54 +29,62 @@ class Tweakphoeus
|
|
28
29
|
#TODO
|
29
30
|
end
|
30
31
|
|
32
|
+
def add_cookies host, key, value
|
33
|
+
domain = get_domain host
|
34
|
+
@cookie_jar[domain] = [] if @cookie_jar[domain].nil?
|
35
|
+
@cookie_jar[domain] << {key => value}
|
36
|
+
end
|
31
37
|
|
38
|
+
def get_domain domain
|
39
|
+
domain.match(/([a-zA-Z0-9]+:\/\/|)([^\/]+)/)[2]
|
40
|
+
end
|
32
41
|
|
33
42
|
private
|
34
43
|
|
35
44
|
def obtain_cookies response
|
36
45
|
set_cookies_field = response.headers["Set-Cookie"]
|
37
|
-
if set_cookies_field.
|
38
|
-
|
46
|
+
return if set_cookies_field.nil?
|
47
|
+
if set_cookies_field.is_a?(String)
|
48
|
+
set_cookies_field = [response.headers["Set-Cookie"]]
|
39
49
|
end
|
40
50
|
|
41
51
|
set_cookies_field.each do |cookie|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
when 4
|
48
|
-
value, path, domain, only = fields
|
52
|
+
key, value = cookie.match(/^([^=]+)=([^;]+)/).to_a[1..-1]
|
53
|
+
domain = cookie.match(/domain=([^;]+)/)
|
54
|
+
|
55
|
+
if domain.nil?
|
56
|
+
domain = get_domain response.request.url
|
49
57
|
else
|
50
|
-
|
51
|
-
raise StandardException("bad number of cookie fields")
|
58
|
+
domain = domain[1]
|
52
59
|
end
|
53
|
-
|
54
|
-
|
55
|
-
@cookie_jar = {} if @cookie_jar.nil? #TODO remove after debug
|
60
|
+
|
61
|
+
@cookie_jar = {} if @cookie_jar.nil? #TODO: remove after debug
|
56
62
|
@cookie_jar[domain] = [] if @cookie_jar[domain].nil?
|
57
63
|
@cookie_jar[domain] << {key => value}
|
58
64
|
end
|
59
65
|
end
|
60
66
|
|
61
67
|
def inject_cookies url, headers
|
62
|
-
|
68
|
+
domain = get_domain url
|
69
|
+
domain = domain.gsub("www.","")
|
63
70
|
headers = {} if headers.nil?
|
64
71
|
cookies = []
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
72
|
+
|
73
|
+
while domain.split(".").count > 1
|
74
|
+
cookies << @cookie_jar[domain] if @cookie_jar[domain]
|
75
|
+
cookies << @cookie_jar["." + domain] if @cookie_jar["." + domain]
|
76
|
+
domain = domain.split(".")[1..-1].join(".")
|
69
77
|
end
|
70
78
|
|
71
79
|
headers["Set-Cookie"] = cookies.flatten
|
72
80
|
end
|
73
81
|
|
74
82
|
def has_redirect? response
|
75
|
-
redirect_url(response).nil?
|
83
|
+
!redirect_url(response).nil?
|
76
84
|
end
|
77
85
|
|
78
86
|
def redirect_url response
|
79
87
|
response.headers["Location"]
|
80
88
|
end
|
89
|
+
|
81
90
|
end
|
data/lib/tweakphoeus/version.rb
CHANGED