wrapi 0.4.4 → 0.4.6

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: cbba7a5999de019875c356dc158b4061bd417cc4f2d21364c2e18bdcac715ac2
4
- data.tar.gz: 8932fd54e9872f43b3e64b4b31554ca71e55987fc35a096bcc10162022773036
3
+ metadata.gz: 5b7bed56656363a708f42ca25752e0d7235e9172e479a929e2f0c42674f42197
4
+ data.tar.gz: 0d0ea9163649d1da64a89c37c548dbb657a98f74fd0da8e3eb871c6994afe8f7
5
5
  SHA512:
6
- metadata.gz: 0f95131accc766c77a6e81ffa8702dc501137478e5fc8d58589e058f30de4886afbc1b1c469f38a33a06c0b2b4177f26db701be033122000a901485b9c02ecb8
7
- data.tar.gz: 885c32718e57b86b9bf395d392263bc0c60044929dc7cf86dba5f80209ba1b7d1c7815fc345ff904836c0838af51e39fe408d4a7f49678eae62da1c93ecf1f64
6
+ metadata.gz: 81cc81edf7e4e9cf2d6b0c837e599332dd717d0914b87aba76ce56da736cdd588a43b863dcf9196e7eec8c86fbfb1df5e5d6586292a5f58c619487f8b7952bff
7
+ data.tar.gz: d1e12b22efcaf3ba181dd62407f9acf4e15e2f45f7c03848d8c076763fdd94ad9904f3b3751bd5491a00a820e0bec8ffd53bef60e910e4f541b4df9a04ed1be3
data/CHANGELOG.md CHANGED
@@ -37,3 +37,9 @@
37
37
 
38
38
  ## [0.4.4] - 2024-03-12
39
39
  - fix typo and implement clone for entities
40
+
41
+ ## [0.4.5] - 2024-03-12
42
+ - refactorings code readability
43
+
44
+ ## [0.4.6] - 2024-06-17
45
+ - fix issue with loading Entity from yaml
data/lib/wrapi/entity.rb CHANGED
@@ -4,32 +4,39 @@ module WrAPI
4
4
  # Defines HTTP request methods
5
5
  module Request
6
6
  class Entity
7
- attr_reader :attributes
8
-
9
7
  # factory method to create entity or array of entities
10
- def self.create(attributes)
11
- if attributes.is_a? Array
12
- Entity.entify(attributes)
8
+ def self.create(attr)
9
+ if attr.is_a? Array
10
+ Entity.entify(attr)
13
11
  else
14
- Entity.new(attributes) if attributes
12
+ Entity.new(attr) if attr
15
13
  end
16
14
  end
17
15
 
18
- def initialize(attributes)
19
- case attributes
16
+ def initialize(attr)
17
+ case attr
20
18
  when Hash
21
- @attributes = attributes.clone.transform_keys(&:to_s)
19
+ @attributes = attr.clone.transform_keys(&:to_s)
22
20
  else
23
- @attributes = attributes.clone
21
+ @attributes = attr.clone
24
22
  end
25
23
  end
24
+
25
+ def attributes
26
+ @attributes || {}
27
+ end
28
+
29
+ def attributes= val
30
+ @attributes = val || {}
31
+ end
26
32
 
27
33
  def method_missing(method_sym, *arguments, &block)
28
34
  # assignment
29
- if (method = method_sym[/.*(?==\z)/m])
35
+ assignment = method_sym[/.*(?==\z)/m]
36
+ if assignment
30
37
  raise ArgumentError, "wrong number of arguments (given #{arguments.length}, expected 1)", caller(1) unless arguments.length == 1
31
38
 
32
- @attributes[method] = arguments[0]
39
+ @attributes[assignment] = arguments[0]
33
40
  elsif @attributes.include? method_sym.to_s
34
41
  accessor(method_sym.to_s)
35
42
  else
@@ -39,6 +46,7 @@ module WrAPI
39
46
  end
40
47
 
41
48
  def respond_to?(method_sym, include_private = false)
49
+ @attributes ||= {}
42
50
  if @attributes.include? method_sym.to_s
43
51
  true
44
52
  else
@@ -51,15 +59,15 @@ module WrAPI
51
59
  end
52
60
 
53
61
  def accessor(method)
54
- case @attributes[method]
55
- when Hash
56
- @attributes[method] = self.class.new(@attributes[method])
57
- when Array
58
- # make deep copy
59
- @attributes[method] = Entity.entify(@attributes[method])
60
- else
61
- @attributes[method]
62
- end
62
+ case @attributes[method]
63
+ when Hash
64
+ @attributes[method] = self.class.new(@attributes[method])
65
+ when Array
66
+ # make deep copy
67
+ @attributes[method] = Entity.entify(@attributes[method])
68
+ else
69
+ @attributes[method]
70
+ end
63
71
  end
64
72
 
65
73
  def clone
@@ -68,6 +76,11 @@ module WrAPI
68
76
  c
69
77
  end
70
78
 
79
+ def ==(other)
80
+ (self.class == other.class) && (self.attributes.eql? other.attributes)
81
+ end
82
+ alias eql? ==
83
+
71
84
  def self.entify(a)
72
85
  if ( a.count > 0 ) && ( a.first.is_a? Hash )
73
86
  a.dup.map do |item|
data/lib/wrapi/request.rb CHANGED
@@ -5,11 +5,12 @@ module WrAPI
5
5
  # Defines HTTP request methods
6
6
  # required attributes format
7
7
  module Request
8
-
8
+ CONTENT_TYPE_HDR = 'Content-Type'.freeze
9
9
  # Perform an HTTP GET request and return entity incase format is :json
10
10
  # @return if format is :json and !raw an [Entity] is returned, otherwhise the response body
11
11
  def get(path, options = {}, raw=false)
12
12
  response = request(:get, path, options) do |request|
13
+ # inject headers...
13
14
  yield(request) if block_given?
14
15
  end
15
16
  entity_response(response, raw)
@@ -25,18 +26,14 @@ module WrAPI
25
26
  pager = create_pager
26
27
  while pager.more_pages?
27
28
  response = request(:get, path, options.merge(pager.page_options)) do |req|
29
+ # inject headers...
28
30
  request_labda.call(req) if request_labda
29
31
  end
30
- if d = pager.class.data(response.body)
31
- d = Entity.create(d)
32
+ handle_data(response.body,pager) do |d|
32
33
  if block_given?
33
34
  yield(d)
34
35
  else
35
- if d.is_a? Array
36
- result += d
37
- else
38
- result << d
39
- end
36
+ result = add_data(result,d)
40
37
  end
41
38
  end
42
39
  pager.next_page!(response.body)
@@ -75,7 +72,7 @@ module WrAPI
75
72
  format && 'json'.eql?(format.to_s)
76
73
  end
77
74
 
78
- private
75
+ private
79
76
 
80
77
  def create_pager
81
78
  pagination_class ? pagination_class.new(page_size) : WrAPI::RequestPagination::DefaultPager
@@ -85,7 +82,7 @@ module WrAPI
85
82
  def request(method, path, options)
86
83
  response = connection.send(method) do |request|
87
84
  yield(request) if block_given?
88
- request.headers['Content-Type'] = "application/#{format}" unless request.headers['Content-Type']
85
+ request.headers[CONTENT_TYPE_HDR] = "application/#{format}" unless request.headers[CONTENT_TYPE_HDR]
89
86
  uri = URI::Parser.new
90
87
  _path = uri.parse(path)
91
88
  _path.path = uri.escape(_path.path)
@@ -94,11 +91,7 @@ module WrAPI
94
91
  request.url(_path.to_s, options)
95
92
  when :post, :put
96
93
  request.path = _path.to_s
97
- if is_json? && !options.empty?
98
- request.body = options.to_json
99
- else
100
- request.body = URI.encode_www_form(options) unless options.empty?
101
- end
94
+ set_body(request,options)
102
95
  end
103
96
  end
104
97
  response
@@ -111,5 +104,28 @@ module WrAPI
111
104
  response
112
105
  end
113
106
  end
107
+
108
+ # set post body depending json content-type
109
+ def set_body(request,options)
110
+ if is_json? && !options.empty?
111
+ request.body = options.to_json
112
+ else
113
+ request.body = URI.encode_www_form(options) unless options.empty?
114
+ end
115
+ end
116
+ def handle_data(body,pager)
117
+ if d = pager.class.data(body)
118
+ d = Entity.create(d)
119
+ yield(d) if block_given?
120
+ end
121
+ end
122
+ # add data to array and check if data itself is an array
123
+ def add_data(result,data)
124
+ if data.is_a? Array
125
+ result += data
126
+ else
127
+ result << data
128
+ end
129
+ end
114
130
  end
115
131
  end
data/lib/wrapi/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WrAPI
4
- VERSION = '0.4.4'
4
+ VERSION = '0.4.6'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janco Tanis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-12 00:00:00.000000000 Z
11
+ date: 2024-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday