rquest 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 127855095b8f26517572c264644b4fcdafd9b927
4
+ data.tar.gz: 53e386a7b6697c74d5b1cf970cd1f3e1b6da3195
5
+ SHA512:
6
+ metadata.gz: 692f78864dcb4bbdfe0d32b6a0436e006a2fc83b904e224b1c73ad59555e6df3dd439988c24102711c75e90263e38df4ea1f46ae98dfe66f3dc15625cbad183d
7
+ data.tar.gz: 84104f4df5815766e12d21035f752a99c753586bc356162618696d8ade49cfbff59364a2dec7571e1342d45e0604806462427ef16bb6732c8f87f551d22a4b73
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rquest.gemspec
4
+ gemspec
@@ -0,0 +1,18 @@
1
+ guard :rspec, cmd: "bundle exec rspec" do
2
+ require "guard/rspec/dsl"
3
+ dsl = Guard::RSpec::Dsl.new(self)
4
+
5
+ # watch /lib/ files
6
+ watch(%r{^lib/(.+).rb$}) do |m|
7
+ "spec/#{m[1]}_spec.rb"
8
+ end
9
+
10
+ watch(%r{^lib/rquest/(.+).rb$}) do |m|
11
+ "spec/#{m[1]}_spec.rb"
12
+ end
13
+
14
+ # watch /spec/ files
15
+ watch(%r{^spec/(.+).rb$}) do |m|
16
+ "spec/#{m[1]}.rb"
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 The Tyrel Corporation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,85 @@
1
+ # Rquest
2
+
3
+ A helper library to easily define restful web requests in ruby.
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.
6
+
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
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'rquest'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install rquest
24
+
25
+ ## Simple GET Request
26
+
27
+ Its basic setup involves setting the uri and action. Its send method will execute the rquest and return the body. You can access the full request object and the response time if you need them.
28
+
29
+ ```ruby
30
+ rquest = RQuest.new({verb: :get, uri: "https://google.com"})
31
+ response_body = rquest.send
32
+ response_time = rquest.response_time
33
+ full_request_object = rquest.response
34
+ ```
35
+
36
+ You can easily combine query params with the uri and the settings hash
37
+
38
+ ```ruby
39
+ rquest = RQuest.new({verb: :get, uri: "https://google.com?q=testing", q_params: {token: "foo"}})
40
+ ```
41
+
42
+ This will result in a request with a URI of http://google.com?q=testing&token=foo behind the sceens
43
+
44
+ ## Auto SSL
45
+ Any uri with https will set use_ssl on the request object automatically. No more.
46
+
47
+ ```ruby
48
+ https.use_ssl = true
49
+ ```
50
+
51
+ ## Form data POST/PUT/PATCH/DELETE/OPTIONS Requests
52
+
53
+ All controlled from the same settings hash with the payload key
54
+
55
+ ```ruby
56
+ rquest = RQuest.new({verb: :get, uri: "https://google.com", payload: {a_field: "stuff", another_field: "more stuff"} })
57
+ rquest.send
58
+ ```
59
+
60
+ Alright the best part for last
61
+
62
+ ## Auto multipart forum
63
+
64
+ Just pass file objects into the files key and everything will be handled for you. It will automatically switch to multipart and detect mime types!
65
+
66
+ ```ruby
67
+ f1 = File.open("path/to/file.txt")
68
+ f2 = File.open("path/to/file.jpg")
69
+ rquest = RQuest.new({verb: :get, uri: "https://google.com", payload: {a_field: "stuff", another_field: "more stuff"}, files: {file_field_1: f1, file_field_2: f2} })
70
+ rquest.send
71
+ ```
72
+
73
+ ## Development
74
+
75
+ 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.
76
+
77
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
78
+
79
+ ## Contributing
80
+
81
+ 1. Fork it ( https://github.com/[my-github-username]/rquest/fork )
82
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
83
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
84
+ 4. Push to the branch (`git push origin my-new-feature`)
85
+ 5. Create a new Pull Request
@@ -0,0 +1,10 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ RSpec::Core::RakeTask.new(:spec) do |task|
7
+ end
8
+
9
+ task :default => :spec
10
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rquest"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ require "rquest/version"
2
+ require 'net/http/post/multipart'
3
+ require 'mimemagic'
4
+ require 'benchmark'
5
+ require "rquest/requestor"
6
+
7
+ module Rquest
8
+ class << self
9
+ def new( settings )
10
+ Requestor.new( settings )
11
+ end
12
+
13
+ def client_class_for_verb( verb, use_multipart=false )
14
+ v = verb.to_s.capitalize
15
+ unless use_multipart
16
+ Object.const_get("Net::HTTP::#{v}")
17
+ else
18
+ Object.const_get("Net::HTTP::#{v}::Multipart")
19
+ end
20
+ end
21
+ end
22
+ 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,83 @@
1
+ module Rquest
2
+ class Requestor
3
+ attr_reader :response, :response_time
4
+ def initialize( settings={} )
5
+ @settings = settings
6
+ apply_default_settings
7
+ @verb = settings[:verb].to_sym
8
+ merge_string_and_hash_params
9
+ @uri = URI::parse( @settings[:uri] )
10
+ @headers = @settings[:headers]
11
+ initialize_http_client
12
+ set_headers
13
+ 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
25
+ end
26
+
27
+ def apply_default_settings
28
+ @settings[:verb] ||= :get
29
+ @settings[:q_params] ||= {}
30
+ @settings[:headers] ||= {}
31
+ @settings[:payload] ||= {}
32
+ setup_files unless @settings[:files].nil?
33
+ end
34
+
35
+ def setup_files
36
+ old_files = @settings.delete(:files)
37
+ new_files = {}
38
+ old_files.each do |field_name, file|
39
+ extname = File.extname(file)
40
+ mime_type = (extname == "") ? "text/plain" : MimeMagic.by_extension(extname).type
41
+ new_files[field_name] = UploadIO.new( file, mime_type, File.basename(file) )
42
+ end
43
+ @settings[:files] = new_files
44
+ end
45
+
46
+ def merge_string_and_hash_params
47
+ url, string_params = @settings[:uri].split("?")
48
+ string_params ||= ""
49
+ hash_of_string_params = string_params.to_q_param_hash
50
+ final_params_hash = hash_of_string_params.merge( @settings[:q_params] )
51
+ @settings[:uri] = [url, final_params_hash.to_q_param_string].join("?")
52
+ end
53
+
54
+ def q_params
55
+ q = @uri.query
56
+ q ||= ""
57
+ q.to_q_param_hash
58
+ end
59
+
60
+ def initialize_http_client
61
+ @http_client = Net::HTTP.new( @uri.host, @uri.port )
62
+ @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
+ end
66
+
67
+ def set_headers
68
+ @headers.each do |key, value|
69
+ @http_request_client[key.to_s] = value.to_s
70
+ end
71
+ end
72
+
73
+ def set_body
74
+ if @settings[:files].nil?
75
+ @http_request_client.set_form_data( @settings[:payload] )
76
+ else
77
+ klass = Rquest::client_class_for_verb( @verb, true )
78
+ multi_part_params = @settings[:payload].merge( @settings[:files] )
79
+ @http_request_client = klass.send(:new, @uri.path, multi_part_params)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ module Rquest
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rquest/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rquest"
8
+ spec.version = Rquest::VERSION
9
+ spec.authors = ["The Tyrel Corporation"]
10
+ spec.email = ["cloud.tycorp@gmail.com"]
11
+
12
+ if spec.respond_to?(:metadata)
13
+ end
14
+
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.}
17
+ spec.homepage = "https://github.com/thetyrelcorporation/rquest"
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.8"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "guard"
29
+ spec.add_development_dependency "guard-rspec"
30
+ spec.add_development_dependency "multipart-post"
31
+ spec.add_development_dependency "mimemagic"
32
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rquest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - The Tyrel Corporation
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: multipart-post
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: mimemagic
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: RQuest makes it easy to build request and gives you full control over
112
+ every aspect of the request. I modeled it after the chrome extension postman. Everything
113
+ you can do with postman you can do with RQuest and it follows the same intuitive
114
+ work flow.
115
+ email:
116
+ - cloud.tycorp@gmail.com
117
+ executables: []
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - ".gitignore"
122
+ - ".rspec"
123
+ - ".travis.yml"
124
+ - CODE_OF_CONDUCT.md
125
+ - Gemfile
126
+ - Guardfile
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - bin/console
131
+ - bin/setup
132
+ - lib/rquest.rb
133
+ - lib/rquest/requestor.rb
134
+ - lib/rquest/version.rb
135
+ - rquest.gemspec
136
+ homepage: https://github.com/thetyrelcorporation/rquest
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.4.3
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: A helper library to easily define restful web requests in ruby. It wraps
160
+ NET::HTTP in an intuitive work flow modeled off of the Postman Chrome extension.
161
+ test_files: []
162
+ has_rdoc: