simple_http_service 0.1.0 → 0.1.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
  SHA256:
3
- metadata.gz: 208b4b37bdff1550d24d708855354809d397b5eb26de709a60f163957def2132
4
- data.tar.gz: 5a212d318cfefb8f291751c185d6260f108dda3f18508f097c55a053ecbe757e
3
+ metadata.gz: 48c5a5d1d5a310878535fe40f8760e7e880268aa8b5903c69e6ee8d3514d3f29
4
+ data.tar.gz: b46618612ffda2302a65c4e1c4a7aa7448d894f2add044ef853e221cd7a029eb
5
5
  SHA512:
6
- metadata.gz: 3ead4015cd47260fa6166a3fd66d455371c5be60ced43e0e047dec1f0efd789c46cd1e1bd9ac2b17f57e16930332c9cf60d08710554cdc6efc63abc513bb002e
7
- data.tar.gz: 258c04992d591e900dc69cfab8d1be86db6603812312063ed76a0023dd4f8c4c4b58b56356fe81528013693b8ec824e4b66548f8c3422170b59664d613fe5dcc
6
+ metadata.gz: 568fd0858274de7ed76426e3d6d64834c1269d2456e1a37a7a89b110515676e2b84489aebf521e09ecee66f85beaa3718076fb288db0e695c2ea18b16eb09dee
7
+ data.tar.gz: ecf2e3fe4c140e06439ac1af14f968c9f31df7d95a4304ad89dae9de910e72fe3e2081d856541c1c22c8f10135065b9175f012f5642bb5f1d6e883d6dabd1385
data/Gemfile CHANGED
@@ -2,5 +2,5 @@ source "https://rubygems.org"
2
2
 
3
3
  git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
- # Specify your gem's dependencies in simple_http.gemspec
5
+ # Specify your gem's dependencies in simple_http_service.gemspec
6
6
  gemspec
data/Gemfile.lock CHANGED
@@ -1,39 +1,39 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_http (0.1.0)
5
- net-http (~> 0.3.2)
4
+ simple_http_service (0.1.2)
5
+ net-http (~> 0.4.1)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- diff-lcs (1.5.0)
11
- net-http (0.3.2)
10
+ diff-lcs (1.5.1)
11
+ net-http (0.4.1)
12
12
  uri
13
13
  rake (10.5.0)
14
- rspec (3.12.0)
15
- rspec-core (~> 3.12.0)
16
- rspec-expectations (~> 3.12.0)
17
- rspec-mocks (~> 3.12.0)
18
- rspec-core (3.12.0)
19
- rspec-support (~> 3.12.0)
20
- rspec-expectations (3.12.2)
14
+ rspec (3.13.0)
15
+ rspec-core (~> 3.13.0)
16
+ rspec-expectations (~> 3.13.0)
17
+ rspec-mocks (~> 3.13.0)
18
+ rspec-core (3.13.0)
19
+ rspec-support (~> 3.13.0)
20
+ rspec-expectations (3.13.0)
21
21
  diff-lcs (>= 1.2.0, < 2.0)
22
- rspec-support (~> 3.12.0)
23
- rspec-mocks (3.12.2)
22
+ rspec-support (~> 3.13.0)
23
+ rspec-mocks (3.13.1)
24
24
  diff-lcs (>= 1.2.0, < 2.0)
25
- rspec-support (~> 3.12.0)
26
- rspec-support (3.12.0)
25
+ rspec-support (~> 3.13.0)
26
+ rspec-support (3.13.1)
27
27
  uri (0.12.0)
28
28
 
29
29
  PLATFORMS
30
30
  ruby
31
31
 
32
32
  DEPENDENCIES
33
- bundler (~> 1.17)
33
+ bundler (~> 2.5.10)
34
34
  rake (~> 10.0)
35
35
  rspec (~> 3.0)
36
- simple_http!
36
+ simple_http_service!
37
37
 
38
38
  BUNDLED WITH
39
- 1.17.3
39
+ 2.5.11
data/README.md CHANGED
@@ -1,38 +1,122 @@
1
- # SimpleHttp
1
+ # SimpleHttpService
2
2
 
3
- 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/simple_http`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ SimpleHttpService is a simple Ruby library to make HTTP requests with customizable options for headers, timeouts, and retries. It provides a convenient way to create and send HTTP requests using a clean and simple interface.
6
4
 
7
5
  ## Installation
8
6
 
9
7
  Add this line to your application's Gemfile:
10
8
 
11
- ```ruby
12
- gem 'simple_http'
13
- ```
9
+ gem 'simple_http_service'
14
10
 
15
11
  And then execute:
16
12
 
17
- $ bundle
13
+ bundle install
18
14
 
19
15
  Or install it yourself as:
20
16
 
21
- $ gem install simple_http
17
+ gem install simple_http_service
22
18
 
23
19
  ## Usage
24
20
 
25
- TODO: Write usage instructions here
21
+ ### Creating a Client
22
+ You can create a new HTTP client using the SimpleHttpService.new method, which initializes an instance of SimpleHttpService::Client.
26
23
 
27
- ## Development
24
+ ```ruby
25
+ require 'simple_http_service'
26
+
27
+ client = SimpleHttpService.new(
28
+ url: 'https://api.example.com/endpoint',
29
+ http_method: :get,
30
+ headers: {
31
+ accept: 'application/json',
32
+ authorization: 'Bearer your_token',
33
+ content_type: 'application/json',
34
+ cookie: 'your cookie'
35
+ },
36
+ open_timeout: 5,
37
+ read_timeout: 10,
38
+ write_timeout: 5,
39
+ retry_count: 3
40
+ )
41
+ ```
42
+ ### Making a Request
43
+ After creating the client, you can call the call method to make the HTTP request:
44
+ ```ruby
45
+ response = client.call
46
+ puts response.body
47
+ ```
28
48
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+ ### Options
50
+ - `url` (required): The URL for the HTTP request.
51
+ - `http_method` (required): The HTTP method to use (:get, :post, :put).
52
+ - `headers`: A hash of headers to include in the request.
53
+ - `open_timeout`: Timeout for opening the connection (default is false).
54
+ - `read_timeout`: Timeout for reading the response (default is false).
55
+ - `write_timeout`: Timeout for writing the request (default is false).
56
+ - `retry_count`: The number of times to retry the request in case of failure.
57
+ - `request_body`: The body of the request (used for POST and PUT requests).
30
58
 
31
- 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+ ### Example
60
+ Here's a complete example of using `SimpleHttpService` to make a `GET` request:
32
61
 
33
- ## Contributing
62
+ ```ruby
63
+ require 'simple_http_service'
64
+
65
+ client = SimpleHttpService.new(
66
+ url: 'https://api.example.com/endpoint',
67
+ http_method: :get,
68
+ headers: {
69
+ accept: 'application/json',
70
+ authorization: 'Bearer your_token'
71
+ },
72
+ open_timeout: 5,
73
+ read_timeout: 10
74
+ )
75
+
76
+ response = client.call
77
+ puts response.body
78
+ ```
79
+ For a POST request with a request body:
80
+ ```ruby
81
+ require 'simple_http_service'
82
+
83
+ client = SimpleHttpService.new(
84
+ url: 'https://api.example.com/endpoint',
85
+ http_method: :post,
86
+ headers: {
87
+ accept: 'application/json',
88
+ authorization: 'Bearer your_token',
89
+ content_type: 'application/json'
90
+ },
91
+ request_body: { key: 'value' }.to_json,
92
+ open_timeout: 5,
93
+ read_timeout: 10,
94
+ write_timeout: 5
95
+ )
96
+
97
+ response = client.call
98
+ puts response.body
99
+ ```
100
+
101
+ ### Error Handling
102
+ The library defines a custom error class SimpleHttpService::Error that you can use to handle exceptions:
103
+ ```ruby
104
+ begin
105
+ response = client.call
106
+ puts response.body
107
+ rescue SimpleHttpService::Error => e
108
+ puts "An error occurred: #{e.message}"
109
+ end
110
+ ```
34
111
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/simple_http. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
112
+
113
+ ## Development
114
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
115
+
116
+ To install this gem onto your local machine, run `bundle install`. To release a new version, update the version number in `version.rb`.
117
+
118
+ ## Contributing
119
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gklsan/simple_http_service.
36
120
 
37
121
  ## License
38
122
 
@@ -40,4 +124,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
124
 
41
125
  ## Code of Conduct
42
126
 
43
- Everyone interacting in the SimpleHttp project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/simple_http/blob/master/CODE_OF_CONDUCT.md).
127
+ Everyone interacting in the SimpleHttpService project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "simple_http"
4
+ require "simple_http_service"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -0,0 +1,69 @@
1
+ require 'net/http'
2
+ module SimpleHttpService
3
+ class Client
4
+ attr_accessor :uri, :headers, :http_method, :open_timeout, :read_timeout, :write_timeout,
5
+ :retry_count, :request_body
6
+
7
+ def initialize(opts)
8
+ raise 'URL must be present' unless opts[:url]
9
+ raise 'http_method must be present' unless opts[:http_method]
10
+
11
+ @uri = URI(opts[:url])
12
+ @http_method = opts[:http_method]
13
+ @headers = opts[:headers] || {}
14
+ @request_body = opts[:request_body]
15
+ @open_timeout = opts[:open_timeout] || false
16
+ @read_timeout = opts[:read_timeout] || false
17
+ @write_timeout = opts[:write_timeout] || false
18
+ @retry_count = opts[:retry_count]
19
+ end
20
+
21
+ def call
22
+ enable_ssl
23
+ set_headers
24
+ set_timeout
25
+ http.request(request)
26
+ end
27
+
28
+ private
29
+
30
+ def set_headers
31
+ request["Accept"] = headers[:accept] if headers[:accept]
32
+ request["Authorization"] = headers[:authorization] if headers[:authorization]
33
+ request["Content-Type"] = headers[:content_type] if headers[:content_type]
34
+ request["Cookie"] = headers[:cookie] if headers[:cookie]
35
+ end
36
+
37
+ def request
38
+ @request ||= case http_method
39
+ when :post
40
+ Net::HTTP::Post.new(uri)
41
+ when :put
42
+ Net::HTTP::Put.new(uri)
43
+ else
44
+ Net::HTTP::Get.new(uri)
45
+ end
46
+ end
47
+
48
+ def enable_ssl
49
+ return unless uri.scheme == 'https'
50
+
51
+ http.use_ssl = true
52
+ http.ssl_version = :TLSv1_2
53
+ end
54
+
55
+ def set_timeout
56
+ http.open_timeout = open_timeout if open_timeout
57
+ http.read_timeout = read_timeout if read_timeout
58
+ http.write_timeout = write_timeout if write_timeout
59
+ end
60
+
61
+ def set_request_params
62
+ request.body = request_body if request_body.present?
63
+ end
64
+
65
+ def http
66
+ @http ||= Net::HTTP.new(uri.host, uri.port)
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleHttpService
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'simple_http_service/version'
2
+ require 'simple_http_service/client'
3
+
4
+ module SimpleHttpService
5
+ def self.new(*args)
6
+ Client.new(*args)
7
+ end
8
+
9
+ class Error < StandardError; end
10
+ end
@@ -1,17 +1,16 @@
1
1
 
2
2
  lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "simple_http/version"
4
+ require "simple_http_service/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "simple_http_service"
8
- spec.version = SimpleHttp::VERSION
9
- spec.authors = ["Gokul"]
8
+ spec.version = SimpleHttpService::VERSION
9
+ spec.authors = ["Gokul (gklsan)"]
10
10
  spec.email = ["pgokulmca@gmail.com"]
11
-
12
- spec.summary = %q{Welcome to SimpleHttp gem}
13
- spec.description = '%q{Welcome to SimpleHttp gem}'
14
- spec.homepage = "https://github.com/gklsan/simple_http_client"
11
+ spec.summary = 'Welcome to SimpleHttpService gem'
12
+ spec.description = 'Welcome to SimpleHttpService gem'
13
+ spec.homepage = "https://github.com/gklsan/simple_http_service"
15
14
  spec.license = "MIT"
16
15
 
17
16
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
@@ -20,8 +19,8 @@ Gem::Specification.new do |spec|
20
19
  # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
20
 
22
21
  spec.metadata["homepage_uri"] = spec.homepage
23
- spec.metadata["source_code_uri"] = "https://github.com/gklsan/simple_http_client"
24
- spec.metadata["changelog_uri"] = "https://github.com/gklsan/simple_http_client"
22
+ spec.metadata["source_code_uri"] = "https://github.com/gklsan/simple_http_service"
23
+ spec.metadata["changelog_uri"] = "https://github.com/gklsan/simple_http_service"
25
24
  else
26
25
  raise "RubyGems 2.0 or newer is required to protect against " \
27
26
  "public gem pushes."
@@ -36,8 +35,8 @@ Gem::Specification.new do |spec|
36
35
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
36
  spec.require_paths = ["lib"]
38
37
 
39
- spec.add_dependency "net-http", "~> 0.3.2"
40
- spec.add_development_dependency "bundler", "~> 1.17"
38
+ spec.add_dependency "net-http", "~> 0.4.1"
39
+ spec.add_development_dependency "bundler", "~> 2.5.10"
41
40
  spec.add_development_dependency "rake", "~> 10.0"
42
41
  spec.add_development_dependency "rspec", "~> 3.0"
43
42
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_http_service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
- - Gokul
7
+ - Gokul (gklsan)
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-16 00:00:00.000000000 Z
11
+ date: 2024-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-http
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.3.2
19
+ version: 0.4.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.3.2
26
+ version: 0.4.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.17'
33
+ version: 2.5.10
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.17'
40
+ version: 2.5.10
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +66,7 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
- description: "%q{Welcome to SimpleHttp gem}"
69
+ description: Welcome to SimpleHttpService gem
70
70
  email:
71
71
  - pgokulmca@gmail.com
72
72
  executables: []
@@ -84,17 +84,17 @@ files:
84
84
  - Rakefile
85
85
  - bin/console
86
86
  - bin/setup
87
- - lib/simple_http.rb
88
- - lib/simple_http/client.rb
89
- - lib/simple_http/version.rb
90
- - simple_http.gemspec
91
- homepage: https://github.com/gklsan/simple_http_client
87
+ - lib/simple_http_service.rb
88
+ - lib/simple_http_service/client.rb
89
+ - lib/simple_http_service/version.rb
90
+ - simple_http_service.gemspec
91
+ homepage: https://github.com/gklsan/simple_http_service
92
92
  licenses:
93
93
  - MIT
94
94
  metadata:
95
- homepage_uri: https://github.com/gklsan/simple_http_client
96
- source_code_uri: https://github.com/gklsan/simple_http_client
97
- changelog_uri: https://github.com/gklsan/simple_http_client
95
+ homepage_uri: https://github.com/gklsan/simple_http_service
96
+ source_code_uri: https://github.com/gklsan/simple_http_service
97
+ changelog_uri: https://github.com/gklsan/simple_http_service
98
98
  post_install_message:
99
99
  rdoc_options: []
100
100
  require_paths:
@@ -110,8 +110,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
112
  requirements: []
113
- rubygems_version: 3.0.9
113
+ rubygems_version: 3.5.11
114
114
  signing_key:
115
115
  specification_version: 4
116
- summary: Welcome to SimpleHttp gem
116
+ summary: Welcome to SimpleHttpService gem
117
117
  test_files: []
@@ -1,46 +0,0 @@
1
- require 'net/http'
2
- module SimpleHttp
3
- class Client
4
- attr_accessor :uri, :headers, :http_method, :query
5
-
6
- def initialize(opts)
7
- raise 'URL must be present' unless opts[:url]
8
- raise 'http_method must be present' unless opts[:http_method]
9
-
10
- @uri = URI(opts[:url])
11
- @http_method = opts[:http_method]
12
- @headers = opts[:headers] || {}
13
- @query = opts[:query]
14
- end
15
-
16
- def call
17
- enable_ssl
18
- set_headers
19
- http.request(request)
20
- end
21
-
22
- private
23
-
24
- def set_headers
25
- request["Accept"] = headers[:accept] if headers[:accept]
26
- request["Authorization"] = headers[:Authorization] if headers[:Authorization]
27
- request["Content-Type"] = headers[:content_type] if headers[:content_type]
28
- request["Cookie"] = headers[:cookie] if headers[:cookie]
29
- end
30
-
31
- def request
32
- @request ||= (http_method == :post ? Net::HTTP::Post.new(uri) : Net::HTTP::Get.new(uri))
33
- end
34
-
35
- def enable_ssl
36
- return unless uri.scheme == 'https'
37
-
38
- http.use_ssl = true
39
- http.ssl_version = :TLSv1_2
40
- end
41
-
42
- def http
43
- @http ||= Net::HTTP.new(uri.host, uri.port)
44
- end
45
- end
46
- end
@@ -1,3 +0,0 @@
1
- module SimpleHttp
2
- VERSION = "0.1.0"
3
- end
data/lib/simple_http.rb DELETED
@@ -1,7 +0,0 @@
1
- require "simple_http/version"
2
- require 'simple_http/client'
3
-
4
- module SimpleHttp
5
- class Error < StandardError; end
6
- # Your code goes here...
7
- end