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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 63047f48de2d5b42fce243c5d60ae2780b7877cc
4
- data.tar.gz: 119861a81fb2f5c606efa11c31775ef8802c8dea
3
+ metadata.gz: eaba5d14b71470fb676ecb3ce4dc427de2ba103b
4
+ data.tar.gz: 5551eeeef872bf529967c987b46b5d6d01061998
5
5
  SHA512:
6
- metadata.gz: d05102a044ee2770ce99e784bc242438bfdcab5b41505405c931d2cb3b1f9da83cd5a8b3876350a5cf1a9e35faa894fded7cc1a4de03c6d7a64dad386c1c49c1
7
- data.tar.gz: 9478d4087b91eeadd5862a1be0c976b7567480dfc1e2669fd6d1698cab401c71ad93967baa5e8b100daf30847693c355bcb7c3bfeb9d96df3eabc51a4d448bed
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
- @cookie_jar = {}
8
+ @cookie_jar = {}
9
9
  end
10
10
 
11
- def get url, body: nil, header: nil
12
- inject_cookies url, header
13
- response = Typhoeus.get url, body: body, headers: header
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 = get(redirect_url(response), body, header) if has_redirect?(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, header: nil
20
- inject_cookies url, header
21
- response = Typhoeus.get url, body: body, headers: header
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, header) if has_redirect?(response)
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.class == "String"
38
- set_cookies_field = [set_cookies_field]
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
- fields = cookie.split("; ")
43
- puts fields.to_s
44
- case fields.count
45
- when 5
46
- value, expire, path, domain, only = fields
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
- puts "Wtf?!"
51
- raise StandardException("bad number of cookie fields")
58
+ domain = domain[1]
52
59
  end
53
- key, value = value.split("=")
54
- domain = domain.split("=").last
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
- url = url.gsub("www.","").split("/")
68
+ domain = get_domain url
69
+ domain = domain.gsub("www.","")
63
70
  headers = {} if headers.nil?
64
71
  cookies = []
65
- while url.split(".").count > 1
66
- cookies << @cookie_jar[url] if @cookie_jar[url]
67
- cookies << @cookie_jar["." + url] if @cookie_jar["." + url]
68
- url = url.split(".")[1..-1].join(".")
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
@@ -1,3 +1,3 @@
1
1
  module Tweakphoeus
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tweakphoeus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Martin Garcia