hknife 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4e941458f4dc0edce8d17a078fe90d55540a9abb
4
+ data.tar.gz: c6ea9cff055b18ea6d02e374937f3cc0bd553ae6
5
+ SHA512:
6
+ metadata.gz: ee7d3bf7a6c25a8473152d6a87ba9de7fa08c150dfc38d7afd91da8fdc8538079cb0370426abc588740ef8e9a585347aa01737cbd8dd746249c818fcdfe2edd3
7
+ data.tar.gz: 1d94ee5a7e1b8730974685ed29161083664662726038155c51eae101e5cd1409b415c22c63e669517057558222c646ccd330d9a15638d60fee8a7737d1c81a58
data/.gitignore ADDED
@@ -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
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.5
5
+ before_install: gem install bundler -v 1.12.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at nyamage@server.fake. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hknife.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 nyamage
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.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # hknife
2
+
3
+ It's a client http library and inspired by sinatra.
4
+ Focus on simpler and understand easily what request will be send from the code.
5
+ Notice it's super alpha quality and stability of interface.
6
+
7
+ # How to use
8
+
9
+ ## Sending get request
10
+
11
+ ```ruby
12
+ get('http://www.example.com').send
13
+ ```
14
+
15
+ ## Sending get request and post request with reponse of previous get request
16
+
17
+ ```ruby
18
+ res = get('http://www.example.com').response
19
+ post_form('http://www.example.com/', id: res.body['id]).response
20
+ ```
21
+
22
+ ## Sending get request with customer header
23
+
24
+ ```ruby
25
+ get('http://93.184.216.34/').
26
+ header(Host: 'www.example.com').
27
+ send
28
+ ```
29
+
30
+ ## Sending request asynchronously
31
+
32
+ ```ruby
33
+ get("http://www.example.com/request1").async do |res|
34
+ puts res.body
35
+ end
36
+
37
+ get("http://www.example.com/request2").async do|res|
38
+ puts res.body
39
+ end
40
+ ```
41
+
42
+ ## Sending multiple requests and send post request after all request ends
43
+
44
+ ```ruby
45
+ reqs = get("http://www.example.com/request0").
46
+ get("http://www.example.com/request1").
47
+ get("http://www.example.com/request2")
48
+
49
+ post_form(reqs.response(0).body['uri'],
50
+ { "id" => reqs.response(1).body['id'],
51
+ "uri" => reqs.response(2).body['key1'] })
52
+ ```
53
+
54
+ ## Check response code
55
+
56
+ ```ruby
57
+ get('http://www.example.com').response.code
58
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hknife"
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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/hknife.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hknife/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hknife"
8
+ spec.version = Hknife::VERSION
9
+ spec.authors = ["nyamage"]
10
+ spec.email = ["nyamage@server.fake"]
11
+
12
+ spec.summary = "hknife"
13
+ spec.description = "hknife"
14
+ spec.homepage = "https://github.com/nyamage/hknife"
15
+ spec.license = "MIT"
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "webmock", "~> 2.0"
25
+ end
@@ -0,0 +1,3 @@
1
+ module Hknife
2
+ VERSION = "0.1.0"
3
+ end
data/lib/hknife.rb ADDED
@@ -0,0 +1,195 @@
1
+ require 'hknife/version'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'pp'
5
+ require 'json'
6
+
7
+ module Hknife
8
+
9
+ class Request
10
+ @@objects = {}
11
+
12
+ def initialize
13
+ @headers = {}
14
+ @response = nil
15
+ @requestor = nil
16
+ @request = nil
17
+ @http_client = nil
18
+ end
19
+
20
+ def get(uri)
21
+ uriObj = URI.parse(uri)
22
+ port = uriObj.default_port if uriObj.port.nil?
23
+ @http_client = Net::HTTP.new(uriObj.host, port)
24
+ @request = Net::HTTP::Get.new(uriObj.path)
25
+ @requestor = lambda do |request|
26
+ res = @http_client.request(request)
27
+ case res['Content-Type']
28
+ when 'application/json' then
29
+ res.body = JSON.parse(res.body)
30
+ end
31
+ res
32
+ end
33
+ self
34
+ end
35
+
36
+ def post_form(uri, data)
37
+ uriObj = URI.parse(uri)
38
+ port = uriObj.default_port if uriObj.port.nil?
39
+ @http_client = Net::HTTP.new(uriObj.host, port)
40
+ @request = Net::HTTP::Post.new(uriObj.path)
41
+ @requestor = lambda do |request|
42
+ res = @http_client.request(@request, URI.encode_www_form(data))
43
+
44
+ case res['Content-Type']
45
+ when 'application/json' then
46
+ res.body = JSON.parse(res.body)
47
+ end
48
+ res
49
+ end
50
+ self
51
+ end
52
+
53
+ def header(hsh)
54
+ @headers = {} if @headers.nil?
55
+ hsh.each do |key, val|
56
+ @headers[key] = val
57
+ end
58
+ self
59
+ end
60
+
61
+ def async(&block)
62
+ @async_thread = Thread.new do
63
+ @response = @requestor.call(@request)
64
+ block.call @response if block_given?
65
+ end
66
+ end
67
+
68
+ def wait
69
+ @async_thread.join
70
+ end
71
+
72
+ def request
73
+ @request
74
+ end
75
+
76
+ def response
77
+ @headers.each_pair {|k, v| @request.add_field(k, v)}
78
+ @response = @requestor.call(@request)
79
+ @response
80
+ end
81
+ end
82
+
83
+ class RequestQueue
84
+ def initialize
85
+ @queue = []
86
+ end
87
+
88
+ def get(uri)
89
+ req = Request.new()
90
+ req.get(uri)
91
+ @queue << req
92
+ self
93
+ end
94
+
95
+ def post_form(uri, data)
96
+ req = Request.new()
97
+ req.post_form(uri, data)
98
+ @queue << req
99
+ self
100
+ end
101
+
102
+ def header(hdr)
103
+ @queue.last.header(hdr)
104
+ self
105
+ end
106
+
107
+ def async(&block)
108
+ @queue.each do |req|
109
+ req.async(block)
110
+ end
111
+ end
112
+
113
+ def send()
114
+ @queue.each do |req|
115
+ req.async
116
+ end
117
+
118
+ @queue.each do |req|
119
+ req.wait
120
+ end
121
+ end
122
+
123
+ def request(idx = nil)
124
+ if idx.nil?
125
+ @queue.last.request
126
+ else
127
+ @queue[idx].request
128
+ end
129
+ end
130
+
131
+ def response(idx = nil)
132
+ if idx.nil?
133
+ @queue.last.response
134
+ else
135
+ @queue[idx].response
136
+ end
137
+ end
138
+
139
+ class << self
140
+ def get(uri)
141
+ obj = RequestQueue.new
142
+ obj.get(uri)
143
+
144
+ if block_given?
145
+ yield obj.request, obj.response
146
+ end
147
+
148
+ obj
149
+ end
150
+
151
+ def post_form(uri, data)
152
+ obj = RequestQueue.new
153
+ obj.post_form(uri, data)
154
+
155
+ if block_given?
156
+ yield obj.request, obj.response
157
+ end
158
+
159
+ obj
160
+ end
161
+
162
+ def header(hsh)
163
+ obj = RequestQueue.new
164
+ obj.header(hsh)
165
+ obj
166
+ end
167
+
168
+ def parallel
169
+ yield
170
+ end
171
+ end
172
+ end
173
+
174
+ module Delegator #:nodoc:
175
+ def self.delegate(*methods)
176
+ methods.each do |method_name|
177
+ define_method(method_name) do |*args, &block|
178
+ return super(*args, &block) if respond_to? method_name
179
+ Delegator.target.send(method_name, *args, &block)
180
+ end
181
+ private method_name
182
+ end
183
+ end
184
+
185
+ delegate :get, :post_form, :request, :header, :parallel
186
+
187
+ class << self
188
+ attr_accessor :target
189
+ end
190
+
191
+ self.target = RequestQueue
192
+ end
193
+ end
194
+
195
+ extend Hknife::Delegator
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hknife
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - nyamage
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-07 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ description: hknife
70
+ email:
71
+ - nyamage@server.fake
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - hknife.gemspec
87
+ - lib/hknife.rb
88
+ - lib/hknife/version.rb
89
+ homepage: https://github.com/nyamage/hknife
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.4.5.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: hknife
113
+ test_files: []