lucid_http 0.11.1 → 1.1.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
- SHA1:
3
- metadata.gz: 261b34c3ff617c6412a4b5928f123cf72d692df9
4
- data.tar.gz: e53200bdfd633cb285ca4d2fe3b0b28863f58bd6
2
+ SHA256:
3
+ metadata.gz: 12e62ee94cac510a96bc7ea3193be7e0c56a8d5aad3832141a870bb9ee5c1c02
4
+ data.tar.gz: de38b31034e053b7b2675c35ed65a7425feae29c746dfa26fe1d91f9cdb79023
5
5
  SHA512:
6
- metadata.gz: 7abd597d390c286da5e298ff25788dcccde01ad074b3920e24a403c545608d14692de13765017bf733d1882917b4b47ba9987ba2ad60bcd33c9bbd3fc075d2e1
7
- data.tar.gz: 676abd88bdb500abb3fa57a44dfaf90f68fca9b0466fb1561b4e0f0b4eaf59e108d2f5d0942f7dcab7448466324fe30e94567a08b0942c64b3d9d27c72bdbd1a
6
+ metadata.gz: ae325f21b42b39254a2240408e7a3e7ed19519f9e5e7fef361140daa67146997e4503ea7e170cc4cb67a06d5661251d8249badd21eb4226dba36d42cecc3f13e
7
+ data.tar.gz: 0f426752fae717c5d95efc68039b1e84569250a39a4b7c5b9e618c1f1b56e1f6634628c08a1b7d73a62b8b0a537b251b31e95746cdd77f9fad9923ca751154cf
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --color
3
+ --format=progress
@@ -0,0 +1,25 @@
1
+ module LucidHttp
2
+
3
+ module Follower
4
+
5
+ def self.for(keyword)
6
+ {
7
+ no_follow: NoFollow.new,
8
+ follow: Follow.new,
9
+ }.fetch(keyword)
10
+ end
11
+
12
+ class NoFollow
13
+ def client
14
+ HTTP
15
+ end
16
+ end
17
+
18
+ class Follow
19
+ def client
20
+ HTTP.follow
21
+ end
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,26 @@
1
+ require "json"
2
+ module LucidHttp
3
+
4
+ module Formatter
5
+
6
+ def self.for(keyword)
7
+ {
8
+ plain: PlainFormatter.new,
9
+ json: JsonFormatter.new,
10
+ }.fetch(keyword)
11
+ end
12
+
13
+ class PlainFormatter
14
+ def call(response_body)
15
+ response_body
16
+ end
17
+ end
18
+
19
+ class JsonFormatter
20
+ def call(response_body)
21
+ JSON.parse(response_body)
22
+ end
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,41 @@
1
+ module LucidHttp
2
+ class Response
3
+ attr_reader :base_url
4
+ attr_reader :path
5
+
6
+ def initialize(base_url:, path:, formatter:, follower:, verb: :get, form: {})
7
+ @base_url = base_url
8
+ @path = path
9
+ @formatter = LucidHttp::Formatter.for(formatter)
10
+ @follower = LucidHttp::Follower.for(follower).client
11
+ @verb = verb
12
+ @form = form
13
+
14
+ @response = @follower.send(@verb, url, params: form)
15
+ end
16
+
17
+ def body
18
+ @formatter.call(@response.body.to_s)
19
+ end
20
+
21
+ def status
22
+ @response.status.to_s
23
+ end
24
+
25
+ def content_type
26
+ @response.content_type.mime_type
27
+ end
28
+
29
+ def url
30
+ "#{base_url}#{path}"
31
+ end
32
+
33
+ def error
34
+ body.split("\n").first
35
+ end
36
+
37
+ def verb
38
+ @verb.to_s.upcase
39
+ end
40
+ end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module LucidHttp
2
- VERSION = "0.11.1"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/lucid_http.rb CHANGED
@@ -1,104 +1,72 @@
1
1
  require "lucid_http/version"
2
2
  require "http"
3
- require "delegate"
4
- require "json"
3
+ require "lucid_http/followers"
4
+ require "lucid_http/formatters"
5
+ require "lucid_http/response"
5
6
 
6
7
  module LucidHttp
7
- def self.target_url
8
- @target_url ||= ENV.fetch("TARGET_URL") { "http://localhost:9292" }
9
- end
10
-
11
- def self.target_url=(url="http://localhost:9292")
12
- @target_url = url
13
- end
8
+ class Client
9
+ attr_reader :base_url
14
10
 
15
- class PrettyStatus < SimpleDelegator
16
- def inspect
17
- to_s
11
+ def initialize(base_url:)
12
+ @base_url = base_url
18
13
  end
19
- end
20
- end
21
14
 
22
- def response
23
- @__lucid_http__res
24
- end
15
+ %i[get post put patch delete options].each do |verb|
16
+ define_method(verb.upcase) do |path, formatter: :plain, follower: :no_follow, form: {}|
17
+ do_verb(
18
+ verb: verb, path: path,
19
+ formatter: formatter, follower: follower,
20
+ form: form,
21
+ )
22
+ end
23
+ end
25
24
 
26
- def __lucid_http__clean
27
- instance_variables.grep(/@__lucid_http__/).each do |v|
28
- remove_instance_variable(v.to_sym)
25
+ def do_verb(verb:, path:, formatter:, follower:, form:)
26
+ LucidHttp::Response.new(
27
+ base_url: base_url, path: path, verb: verb,
28
+ formatter: formatter, follower: follower,
29
+ form: form,
30
+ )
31
+ end
29
32
  end
30
33
  end
31
34
 
32
- def __lucid_http__setup(url, action: :get, follow: false, form: nil, json: false, **opts)
33
- __lucid_http__clean
34
- @__lucid_http__client = HTTP.persistent(LucidHttp.target_url)
35
- if json
36
- @__lucid_http__client = @__lucid_http__client.accept("application/json")
35
+ %i[get post put patch delete options].each do |verb|
36
+ define_method(verb.upcase) do |path, formatter: :plain, follower: :no_follow, form: {}|
37
+ @__response = LucidHttp::Client.new(base_url: "http://localhost:9292").do_verb(
38
+ verb: verb, path: path,
39
+ formatter: formatter, follower: follower,
40
+ form: form,
41
+ )
42
+ @__response.body
37
43
  end
38
- if follow
39
- @__lucid_http__client = @__lucid_http__client.follow
40
- end
41
-
42
- @__lucid_http__path = @__lucid_http__client.default_options.persistent + url
43
- @__lucid_http__res = @__lucid_http__client.send(action.to_sym, url, form: form)
44
- @__lucid_http__json = json
45
44
  end
46
45
 
47
46
  def body
48
- @__lucid_http__body ||= __lucid_http__get_body
47
+ @__response.body
49
48
  end
50
49
 
51
- def __lucid_http__get_body
52
- original_body = response.body.to_s
53
- if !@__lucid_http__json
54
- original_body
55
- else
56
- JSON.parse(original_body)
57
- end
50
+ def verb
51
+ @__response.verb
58
52
  end
59
53
 
60
54
  def status
61
- @__lucid_http__status = LucidHttp::PrettyStatus.new(response.status)
55
+ @__response.status
62
56
  end
63
57
 
64
58
  def content_type
65
- response.content_type.mime_type
59
+ @__response.content_type
66
60
  end
67
61
 
68
- def path
69
- @__lucid_http__path
62
+ def url
63
+ @__response.url
70
64
  end
71
65
 
72
- def error
73
- if status.to_i == 500
74
- body.split("\n").first
75
- else
76
- "No 500 error found."
77
- end
66
+ def path
67
+ @__response.path
78
68
  end
79
69
 
80
- %i[get post put patch delete head options].each do |verb|
81
- define_method(verb.upcase) do |url, **opts|
82
- __lucid_http__setup(url, action: verb, **opts)
83
- new_body = case status.to_i
84
- when 200
85
- body
86
- else
87
- "STATUS: #{status}"
88
- end
89
-
90
- new_body
91
- end
70
+ def error
71
+ @__response.error
92
72
  end
93
-
94
- # def POST(url, **opts)
95
- # __lucid_http__setup(url, action: :post, **opts)
96
- # new_body = case status.to_i
97
- # when 200
98
- # body
99
- # else
100
- # "STATUS: #{status}"
101
- # end
102
-
103
- # new_body
104
- # end
data/lucid_http.gemspec CHANGED
@@ -21,9 +21,11 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.add_runtime_dependency "http", "~> 2.1"
24
+ spec.add_runtime_dependency "http"
25
25
 
26
- spec.add_development_dependency "bundler", "~> 1.13"
27
- spec.add_development_dependency "rake", "~> 10.0"
28
- spec.add_development_dependency "minitest", "~> 5.0"
26
+ spec.add_development_dependency "rackup", "~> 2.1.0"
27
+ spec.add_development_dependency "rake", "~> 13.0.6"
28
+ spec.add_development_dependency "roda", "~> 3.66.0"
29
+ spec.add_development_dependency "rspec", "~> 3.12.0"
30
+ spec.add_development_dependency "bundler", "~> 2.4.8"
29
31
  end
metadata CHANGED
@@ -1,71 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lucid_http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Federico Iachetti
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-14 00:00:00.000000000 Z
11
+ date: 2023-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '2.1'
19
+ version: '0'
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: '2.1'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: bundler
28
+ name: rackup
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.13'
33
+ version: 2.1.0
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.13'
40
+ version: 2.1.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: 13.0.6
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 13.0.6
55
+ - !ruby/object:Gem::Dependency
56
+ name: roda
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.66.0
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
- version: '10.0'
68
+ version: 3.66.0
55
69
  - !ruby/object:Gem::Dependency
56
- name: minitest
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.12.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.12.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
57
85
  requirement: !ruby/object:Gem::Requirement
58
86
  requirements:
59
87
  - - "~>"
60
88
  - !ruby/object:Gem::Version
61
- version: '5.0'
89
+ version: 2.4.8
62
90
  type: :development
63
91
  prerelease: false
64
92
  version_requirements: !ruby/object:Gem::Requirement
65
93
  requirements:
66
94
  - - "~>"
67
95
  - !ruby/object:Gem::Version
68
- version: '5.0'
96
+ version: 2.4.8
69
97
  description: Lucid Http wraps the http.rb gem in a simple DSL. Is particularly useful
70
98
  for screencasts and simple presentations.
71
99
  email:
@@ -75,6 +103,7 @@ extensions: []
75
103
  extra_rdoc_files: []
76
104
  files:
77
105
  - ".gitignore"
106
+ - ".rspec"
78
107
  - ".travis.yml"
79
108
  - CODE_OF_CONDUCT.md
80
109
  - Gemfile
@@ -84,13 +113,16 @@ files:
84
113
  - bin/console
85
114
  - bin/setup
86
115
  - lib/lucid_http.rb
116
+ - lib/lucid_http/followers.rb
117
+ - lib/lucid_http/formatters.rb
118
+ - lib/lucid_http/response.rb
87
119
  - lib/lucid_http/version.rb
88
120
  - lucid_http.gemspec
89
121
  homepage: https://github.com/iachettifederico/lucid_http
90
122
  licenses:
91
123
  - MIT
92
124
  metadata: {}
93
- post_install_message:
125
+ post_install_message:
94
126
  rdoc_options: []
95
127
  require_paths:
96
128
  - lib
@@ -105,9 +137,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
137
  - !ruby/object:Gem::Version
106
138
  version: '0'
107
139
  requirements: []
108
- rubyforge_project:
109
- rubygems_version: 2.6.10
110
- signing_key:
140
+ rubygems_version: 3.4.8
141
+ signing_key:
111
142
  specification_version: 4
112
143
  summary: Lucid Http wraps the http.rb gem in a simple DSL.
113
144
  test_files: []