Skip to content

Commit

Permalink
now supports prepath on host
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRuiz committed Feb 14, 2019
1 parent b876239 commit 2af7371
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ NiceHttp.defaults = {
}
```

To add a fixed path that would be added automatically to all your requests just before the specified request path, you can do it by adding it to `host`:

```ruby
http = NiceHttp.new('https://v2.namsor.com/NamSorAPIv2/')

resp = http.get('/api2/json/gender/Peter/Moon')
# The get request path will be: /NamSorAPIv2/api2/json/gender/Peter/Moon on server v2.namsor.com

resp = http.get('/api2/json/gender/Love/Sun?ret=true')
# The get request path will be: /NamSorAPIv2/api2/json/gender/Love/Sun on server v2.namsor.com
```

## Creating requests

Expand Down
6 changes: 5 additions & 1 deletion lib/nice_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def initialize(args = {})
require "net/https"
@host = self.class.host
@port = self.class.port
@prepath = ''
@ssl = self.class.ssl
@headers = self.class.headers.dup
@debug = self.class.debug
Expand All @@ -180,6 +181,7 @@ def initialize(args = {})
@host = uri.host unless uri.host.nil?
@port = uri.port unless uri.port.nil?
@ssl = true if !uri.scheme.nil? && (uri.scheme == "https")
@prepath = uri.path unless uri.path=='/'
elsif args.is_a?(Hash) && !args.keys.empty?
@host = args[:host] if args.keys.include?(:host)
@port = args[:port] if args.keys.include?(:port)
Expand Down Expand Up @@ -232,11 +234,12 @@ def initialize(args = {})
end


if @host.to_s != "" and (@host.include?("http:") or @host.include?("https:"))
if @host.to_s != "" and (@host.start_with?("http:") or @host.start_with?("https:"))
uri = URI.parse(@host)
@host = uri.host unless uri.host.nil?
@port = uri.port unless uri.port.nil?
@ssl = true if !uri.scheme.nil? && (uri.scheme == "https")
@prepath = uri.path unless uri.path=='/'
end

raise InfoMissing, :port if @port.to_s == ""
Expand Down Expand Up @@ -878,6 +881,7 @@ def manage_request(*arguments)
elsif arguments.size == 1 and arguments[0].kind_of?(String)
path = arguments[0].to_s()
end
path = (@prepath + path).gsub('//','/') unless path.nil? or path.start_with?('http:') or path.start_with?('https:')
@cookies.each { |cookie_path, cookies_hash|
cookie_path = "" if cookie_path == "/"
path_to_check = path
Expand Down
2 changes: 1 addition & 1 deletion nice_http.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'nice_http'
s.version = '1.3.5'
s.version = '1.4.0'
s.summary = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources."
s.description = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources. Manage different hosts on the fly. Easily get the value you want from the JSON strings. Use hashes on your requests."
s.authors = ["Mario Ruiz"]
Expand Down
16 changes: 13 additions & 3 deletions spec/nice_http/nice_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,9 @@
expect(klass.connections.size).to eq 1
expect(klass.connections[0]).to eq http2
end
end

# :no will not generate any logs.
# :screen will print the logs on the screen.

describe 'log files' do
it 'logs to file specified' do
klass.log = './example.log'
http = klass.new("https://example.com")
Expand Down Expand Up @@ -348,7 +347,9 @@
files = Dir["./*.log"]
expect(files.size).to eq 0
end
end

describe 'proxys' do
it "starts proxy supplied host and port" do
klass.proxy_host = "example.com"
klass.proxy_port = 80
Expand All @@ -363,4 +364,13 @@
end
end

describe 'prepaths' do
it "adds the prepath if supplied" do
http = klass.new("https://reqres.in/api")
resp = http.get "/users?page=2"
expect(resp.code).to eq 200
end
end


end

0 comments on commit 2af7371

Please sign in to comment.