rquest 0.1.0 → 1.0.0

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: 127855095b8f26517572c264644b4fcdafd9b927
4
- data.tar.gz: 53e386a7b6697c74d5b1cf970cd1f3e1b6da3195
3
+ metadata.gz: af3484f4dfb24bdf2acac109deebd9b906884d6e
4
+ data.tar.gz: 63bc4354dd605483a1dd1191a91e2187841b77a4
5
5
  SHA512:
6
- metadata.gz: 692f78864dcb4bbdfe0d32b6a0436e006a2fc83b904e224b1c73ad59555e6df3dd439988c24102711c75e90263e38df4ea1f46ae98dfe66f3dc15625cbad183d
7
- data.tar.gz: 84104f4df5815766e12d21035f752a99c753586bc356162618696d8ade49cfbff59364a2dec7571e1342d45e0604806462427ef16bb6732c8f87f551d22a4b73
6
+ metadata.gz: 45f426956256bf01ef6b9a89db86bb073229a0dfaba7ad253ae07ef9defd61b1574eab16abdaaf0531390026839c4425cdc5f97c0684d1a4741d3794c6b94eff
7
+ data.tar.gz: a76a10109e2cdfeb0072011fa4e0062f8cb4886352b6b0ca609b0cb7de9f7382b0e318ff8dc03e17a0be71f57c96569f2be8ae4631ad125bc8060e9d4f568702
data/README.md CHANGED
@@ -2,10 +2,12 @@
2
2
 
3
3
  A helper library to easily define restful web requests in ruby.
4
4
 
5
- Bassically I am tiered of constantly relooking up NET HTTP blog post to try and remember how to say override the default headers or attatch a file as a multipart post request. Also there are things about the ruby request generation process I felt I could improve such as, autodetecting the need for ssl from the URI and providing a clean DSL for request definition. As well as cleaner file attatchment.
5
+ Bassically I am tiered of constantly relooking up NET HTTP blog post to try and remember how to say override the default headers or attatch a file as a multipart post request. Also there are things about the ruby request generation process I felt I could improve such as, autodetecting the need for ssl from the URI and providing a clean DSL for request definition, as well as cleaner file attatchment.
6
6
 
7
7
  RQuest makes it easy to build request and gives you full control over every aspect of the request. I modeled it after the chrome extension postman. Everything you can do with postman you can do with RQuest and it follows the same intuitive work flow.
8
8
 
9
+ In addition Rquest is an object that can handle a full session request cycle. You can say log in have the authentication cookies set by the server and then proceed to parse your dashboard.
10
+
9
11
  ## Installation
10
12
 
11
13
  Add this line to your application's Gemfile:
@@ -29,8 +31,8 @@ Its basic setup involves setting the uri and action. Its send method will execut
29
31
  ```ruby
30
32
  rquest = RQuest.new({verb: :get, uri: "https://google.com"})
31
33
  response_body = rquest.send
32
- response_time = rquest.response_time
33
- full_request_object = rquest.response
34
+ response_time = rquest.last_response_time
35
+ response_object = rquest.last_response
34
36
  ```
35
37
 
36
38
  You can easily combine query params with the uri and the settings hash
@@ -53,7 +55,7 @@ https.use_ssl = true
53
55
  All controlled from the same settings hash with the payload key
54
56
 
55
57
  ```ruby
56
- rquest = RQuest.new({verb: :get, uri: "https://google.com", payload: {a_field: "stuff", another_field: "more stuff"} })
58
+ rquest = RQuest.new({verb: :post, uri: "https://google.com", payload: {a_field: "stuff", another_field: "more stuff"} })
57
59
  rquest.send
58
60
  ```
59
61
 
@@ -70,6 +72,50 @@ rquest = RQuest.new({verb: :get, uri: "https://google.com", payload: {a_field: "
70
72
  rquest.send
71
73
  ```
72
74
 
75
+ ## Sessions
76
+
77
+ Rquest is built to behave like a fresh incognito browser. Every time you apply send it will add the response to the transactions attribute and set the last_response and last_response_time.
78
+
79
+ Simply call update to change what you need before the next send like so. New settings will be merged so they will either override old ones ore creat new ones. Any non specified setting will remain untouched from the last request.
80
+
81
+ ```ruby
82
+ rquest = RQuest.new({verb: :get, uri: "https://google.com?q=testing", q_params: {token: "foo"}})
83
+ rquest.send
84
+ rquest.update({q_params: {q: "other search value"}}
85
+ rquest.send
86
+ first_query = rquest.transactions.first
87
+ last_query = rquest.transactions.last
88
+ ```
89
+
90
+ Transactions are stored as has with their request, response and response time available for future reference. Continuing from above we could.
91
+
92
+ ```ruby
93
+ old_request = first_query[:request]
94
+ old_response = first_query[:response]
95
+ time_it_took = first_query[:response_time]
96
+ ```
97
+
98
+ ## Cookies
99
+
100
+ You can set cookies for a request by adding them to the settings[:cookies] like
101
+
102
+ ```ruby
103
+ rquest = RQuest.new({verb: :get, uri: "https://google.com", payload: {a_field: "stuff", another_field: "more stuff"}, cookies: {"MySpecialCookie" => "SomeSuperSecretValue"} })
104
+ ```
105
+
106
+ Any response will add/merge any cookies in the "Set-Cookie" header of the response to your next request
107
+
108
+ ```ruby
109
+ rquest = RQuest.new({verb: :post, uri: "https://somesite.com/sessions", payload: {username: "foobar", password: "SuperSecret"}})
110
+ rquest.send
111
+ ```
112
+ If this authenticates correctly then the server will send the right Set-Cookie so then you can do something like.
113
+
114
+ ```ruby
115
+ rquest.update({uri: "https://somesite.com/mydashboard"}
116
+ rquest.send
117
+ ```
118
+
73
119
  ## Development
74
120
 
75
121
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/rquest.rb CHANGED
@@ -2,6 +2,7 @@ require "rquest/version"
2
2
  require 'net/http/post/multipart'
3
3
  require 'mimemagic'
4
4
  require 'benchmark'
5
+ require "rquest/core_overrides"
5
6
  require "rquest/requestor"
6
7
 
7
8
  module Rquest
@@ -20,19 +21,3 @@ module Rquest
20
21
  end
21
22
  end
22
23
  end
23
-
24
- class Hash
25
- def to_q_param_string
26
- self.inject([]){|r,(k,v)| r.push( "#{k}=#{v}" )}.join("&")
27
- end
28
- end
29
-
30
- class String
31
- def to_q_param_hash
32
- self.split("&").inject({}) do |hash, key_value|
33
- key, value = key_value.split("=")
34
- hash[key.to_sym] = value
35
- hash
36
- end
37
- end
38
- end
@@ -0,0 +1,27 @@
1
+ class Hash
2
+ def to_q_param_string
3
+ self.inject([]){|r,(k,v)| r.push( "#{k}=#{v}" )}.join("&")
4
+ end
5
+
6
+ def to_cookie_string
7
+ self.inject([]){|r,(k,v)| r.push( "#{k}=#{v}" )}.join("; ") + ";"
8
+ end
9
+ end
10
+
11
+ class String
12
+ def to_q_param_hash
13
+ self.split("&").inject({}) do |hash, key_value|
14
+ key, value = key_value.split("=")
15
+ hash[key.to_sym] = value
16
+ hash
17
+ end
18
+ end
19
+ def to_cookies_hash
20
+ tmp = self[0..-2]
21
+ tmp.split("; ").inject({}) do |hash, key_value|
22
+ key, value = key_value.split("=")
23
+ hash[key] = value
24
+ hash
25
+ end
26
+ end
27
+ end
@@ -1,37 +1,71 @@
1
1
  module Rquest
2
2
  class Requestor
3
- attr_reader :response, :response_time
3
+ attr_reader :last_response, :last_response_time, :transactions
4
4
  def initialize( settings={} )
5
- @settings = settings
5
+ @transactions = []
6
+ update( settings )
7
+ end
8
+
9
+ def update( settings={} )
10
+ @settings ||= {}
11
+ @settings = @settings.merge( settings )
6
12
  apply_default_settings
7
- @verb = settings[:verb].to_sym
13
+ merge_settings
14
+ @verb = @settings[:verb].to_sym
8
15
  merge_string_and_hash_params
9
16
  @uri = URI::parse( @settings[:uri] )
10
17
  @headers = @settings[:headers]
11
18
  initialize_http_client
12
- set_headers
13
19
  set_body
14
- end
15
-
16
- def send
17
- @response_time = Benchmark.realtime do
18
- @response = @http_client.request(@http_request_client)
19
- end
20
- @response.body
21
- end
22
-
23
- def uri_path
24
- @uri.path.empty? ? "/" : @uri.path
20
+ set_headers
25
21
  end
26
22
 
27
23
  def apply_default_settings
28
24
  @settings[:verb] ||= :get
29
25
  @settings[:q_params] ||= {}
26
+ @settings[:cookies] ||= {}
30
27
  @settings[:headers] ||= {}
28
+ @settings[:cookies] ||= {}
29
+ @settings[:headers]["User-Agent"] ||= "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36"
31
30
  @settings[:payload] ||= {}
31
+ @headers ||= {}
32
+ @cookies ||= {}
33
+ @q_params ||= {}
34
+ @files = {}
32
35
  setup_files unless @settings[:files].nil?
33
36
  end
34
37
 
38
+ def merge_settings
39
+ @headers = @headers.merge( @settings[:headers] )
40
+ @cookies = @cookies.merge( @settings[:cookies] )
41
+ @q_params = @q_params.merge( @settings[:q_params] )
42
+ end
43
+
44
+ def send
45
+ @last_response_time = Benchmark.realtime do
46
+ @last_response = @http_client.request(@http_request_client)
47
+ end
48
+ @transactions.push( { request: @http_request_client, response: @last_response, response_time: @last_response_time } )
49
+ unless @last_response["Set-Cookie"].nil?
50
+ new_cookies = @last_response["Set-Cookie"].to_cookies_hash
51
+ @cookies = @cookies.merge( new_cookies )
52
+ end
53
+ case @last_response
54
+ when Net::HTTPSuccess
55
+ @last_response.body
56
+ when Net::HTTPUnauthorized
57
+ {'error' => "#{@last_response.message}: username and password set and correct?"}
58
+ when Net::HTTPServerError
59
+ {'error' => "#{@last_response.message}: try again later?"}
60
+ else
61
+ {'error' => @last_response.message}
62
+ end
63
+ end
64
+
65
+ def uri_path
66
+ @uri.path.empty? ? "/" : @uri.path
67
+ end
68
+
35
69
  def setup_files
36
70
  old_files = @settings.delete(:files)
37
71
  new_files = {}
@@ -40,14 +74,15 @@ module Rquest
40
74
  mime_type = (extname == "") ? "text/plain" : MimeMagic.by_extension(extname).type
41
75
  new_files[field_name] = UploadIO.new( file, mime_type, File.basename(file) )
42
76
  end
43
- @settings[:files] = new_files
77
+ @files = new_files
44
78
  end
45
79
 
46
80
  def merge_string_and_hash_params
47
81
  url, string_params = @settings[:uri].split("?")
48
82
  string_params ||= ""
49
83
  hash_of_string_params = string_params.to_q_param_hash
50
- final_params_hash = hash_of_string_params.merge( @settings[:q_params] )
84
+ final_params_hash = hash_of_string_params.merge( @q_params )
85
+ @q_params = final_params_hash
51
86
  @settings[:uri] = [url, final_params_hash.to_q_param_string].join("?")
52
87
  end
53
88
 
@@ -60,22 +95,27 @@ module Rquest
60
95
  def initialize_http_client
61
96
  @http_client = Net::HTTP.new( @uri.host, @uri.port )
62
97
  @http_client.use_ssl = true if @uri.scheme == "https"
63
- klass = Rquest::client_class_for_verb( @verb )
64
- @http_request_client = klass.send(:new, uri_path)
65
98
  end
66
99
 
67
100
  def set_headers
101
+ set_cookies if @cookies.any?
68
102
  @headers.each do |key, value|
69
103
  @http_request_client[key.to_s] = value.to_s
70
104
  end
71
105
  end
72
106
 
107
+ def set_cookies
108
+ @http_request_client["Cookie"] = @cookies.to_cookie_string
109
+ end
110
+
73
111
  def set_body
74
- if @settings[:files].nil?
112
+ unless @files.any?
113
+ klass = Rquest::client_class_for_verb( @verb )
114
+ @http_request_client = klass.send(:new, uri_path)
75
115
  @http_request_client.set_form_data( @settings[:payload] )
76
116
  else
77
117
  klass = Rquest::client_class_for_verb( @verb, true )
78
- multi_part_params = @settings[:payload].merge( @settings[:files] )
118
+ multi_part_params = @settings[:payload].merge( @files )
79
119
  @http_request_client = klass.send(:new, @uri.path, multi_part_params)
80
120
  end
81
121
  end
@@ -1,3 +1,3 @@
1
1
  module Rquest
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/rquest-0.1.0.gem ADDED
Binary file
data/rquest.gemspec CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
  end
14
14
 
15
15
  spec.summary = %q{A helper library to easily define restful web requests in ruby. It wraps NET::HTTP in an intuitive work flow modeled off of the Postman Chrome extension.}
16
- spec.description = %q{RQuest makes it easy to build request and gives you full control over every aspect of the request. I modeled it after the chrome extension postman. Everything you can do with postman you can do with RQuest and it follows the same intuitive work flow.}
16
+ spec.description = %q{RQuest makes it easy to build request and gives you full control over every aspect of the request. I modeled it after the chrome extension postman. Everything you can do with postman you can do with RQuest and it follows the same intuitive work flow. It also has support for session managment.}
17
17
  spec.homepage = "https://github.com/thetyrelcorporation/rquest"
18
18
  spec.license = "MIT"
19
19
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rquest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Tyrel Corporation
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-03-23 00:00:00.000000000 Z
11
+ date: 2015-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -111,7 +111,7 @@ dependencies:
111
111
  description: RQuest makes it easy to build request and gives you full control over
112
112
  every aspect of the request. I modeled it after the chrome extension postman. Everything
113
113
  you can do with postman you can do with RQuest and it follows the same intuitive
114
- work flow.
114
+ work flow. It also has support for session managment.
115
115
  email:
116
116
  - cloud.tycorp@gmail.com
117
117
  executables: []
@@ -130,8 +130,10 @@ files:
130
130
  - bin/console
131
131
  - bin/setup
132
132
  - lib/rquest.rb
133
+ - lib/rquest/core_overrides.rb
133
134
  - lib/rquest/requestor.rb
134
135
  - lib/rquest/version.rb
136
+ - rquest-0.1.0.gem
135
137
  - rquest.gemspec
136
138
  homepage: https://github.com/thetyrelcorporation/rquest
137
139
  licenses: