hudu 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hudu/api.rb +2 -0
- data/lib/hudu/asset_helper.rb +12 -0
- data/lib/hudu/client.rb +15 -6
- data/lib/hudu/connection.rb +20 -0
- data/lib/hudu/pagination.rb +8 -6
- data/lib/hudu/rate_throttle_middleware.rb +67 -0
- data/lib/hudu/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6de79a28de35dea4c3ac9ebd26823427f839192e44113fe67f9d5f5e845cade1
|
4
|
+
data.tar.gz: 1bada2b529ac16cc376b85f33b953a451a23f8e955dcc2068814171bcbf0509c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 59adb93ef27db882adc800a183ffbe1b7297e96a11014492ff61774395d0969a79603bd442c452f028fae38fc2b6a9ac591f66151276b94bc4d9b154bddda8f6
|
7
|
+
data.tar.gz: bdf2dfd500420081eb4b530f3621f90ae21f3204e0c817e7e33da8c1baae660425e2c844059693c5f5048d7ee1d16e26159987a7ff1d6562dbc716453e06ea97
|
data/lib/hudu/api.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "wrapi"
|
2
2
|
require File.expand_path('authentication', __dir__)
|
3
3
|
require File.expand_path('configuration', __dir__)
|
4
|
+
require File.expand_path('connection', __dir__)
|
4
5
|
|
5
6
|
module Hudu
|
6
7
|
# @private
|
@@ -27,6 +28,7 @@ module Hudu
|
|
27
28
|
|
28
29
|
include Configuration
|
29
30
|
include WrAPI::Connection
|
31
|
+
include Connection
|
30
32
|
include WrAPI::Request
|
31
33
|
include WrAPI::Authentication
|
32
34
|
include Authentication
|
data/lib/hudu/asset_helper.rb
CHANGED
@@ -11,6 +11,18 @@ module Hudu
|
|
11
11
|
custom_asset['custom_fields'] = self.custom_fields(asset.fields)
|
12
12
|
{ asset: custom_asset }
|
13
13
|
end
|
14
|
+
|
15
|
+
# create new asset from layout
|
16
|
+
def self.create_asset name, asset_layout_id, fields
|
17
|
+
custom_asset = {
|
18
|
+
asset: {
|
19
|
+
name: name,
|
20
|
+
asset_layout_id: asset_layout_id,
|
21
|
+
custom_fields: self.custom_fields(fields)
|
22
|
+
}
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
14
26
|
private
|
15
27
|
def self.custom_fields(fields)
|
16
28
|
[fields.map{|field| [field.label.downcase.gsub(' ','_'),field.value]}.to_h]
|
data/lib/hudu/client.rb
CHANGED
@@ -29,6 +29,11 @@ module Hudu
|
|
29
29
|
end
|
30
30
|
# update
|
31
31
|
self.send(:define_method, "update_#{method}") do |id=nil,params = {}|
|
32
|
+
r = put(api_url("#{path}/#{id}"), params)
|
33
|
+
r = hudu_data(r,method)
|
34
|
+
end
|
35
|
+
# create
|
36
|
+
self.send(:define_method, "create_#{method}") do |id=nil,params = {}|
|
32
37
|
r = post(api_url("#{path}/#{id}"), params)
|
33
38
|
r = hudu_data(r,method)
|
34
39
|
end
|
@@ -54,7 +59,7 @@ module Hudu
|
|
54
59
|
api_endpoint :expirations
|
55
60
|
api_endpoint :websites, :website
|
56
61
|
api_endpoint :relations
|
57
|
-
api_endpoint :magic_dash
|
62
|
+
api_endpoint :magic_dashes, :magic_dash, 'magic_dash'
|
58
63
|
|
59
64
|
def company_articles( company_id, params = {} )
|
60
65
|
articles({company_id: company_id}.merge(params))
|
@@ -62,12 +67,16 @@ module Hudu
|
|
62
67
|
def company_assets(id,params={})
|
63
68
|
get_paged(api_url("companies/#{id}/assets"), params)
|
64
69
|
end
|
65
|
-
def company_asset(
|
66
|
-
get(api_url("companies/#{
|
70
|
+
def company_asset(company_id,asset_id,params={})
|
71
|
+
get(api_url("companies/#{company_id}/assets/#{asset_id}"), params)
|
67
72
|
end
|
68
73
|
|
69
74
|
def update_company_asset(asset)
|
70
|
-
put(api_url("companies/#{asset.company_id}/assets/#{asset.id}"), AssetHelper.construct_asset(asset))
|
75
|
+
hudu_data(put(api_url("companies/#{asset.company_id}/assets/#{asset.id}"), AssetHelper.construct_asset(asset),false),:asset)
|
76
|
+
end
|
77
|
+
|
78
|
+
def create_company_asset(company_id,asset_layout, fields)
|
79
|
+
hudu_data(post(api_url("companies/#{company_id}/assets"), AssetHelper.create_asset(asset_layout.name,asset_layout.id,fields),false),:asset)
|
71
80
|
end
|
72
81
|
|
73
82
|
# return api path
|
@@ -77,8 +86,8 @@ module Hudu
|
|
77
86
|
|
78
87
|
# hudu returns data as {resource:{}} or {resource:[]}
|
79
88
|
def hudu_data(result,resource)
|
80
|
-
if result.is_a?(
|
81
|
-
result
|
89
|
+
if result.is_a?(WrAPI::Request::Entity) && result.attributes[resource.to_s]
|
90
|
+
result.send resource.to_s
|
82
91
|
else
|
83
92
|
result
|
84
93
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path('rate_throttle_middleware', __dir__)
|
2
|
+
|
3
|
+
module Hudu
|
4
|
+
# Create connection including authorization parameters with default Accept format and User-Agent
|
5
|
+
# By default
|
6
|
+
# - Bearer authorization is access_token is not nil override with @setup_authorization
|
7
|
+
# - Headers setup for client-id and client-secret when client_id and client_secret are not nil @setup_headers
|
8
|
+
# @private
|
9
|
+
module Connection
|
10
|
+
private
|
11
|
+
|
12
|
+
def connection
|
13
|
+
unless @connection
|
14
|
+
@connection = super
|
15
|
+
@connection.use RateThrottleMiddleware, limit: 250, period: 60
|
16
|
+
end
|
17
|
+
@connection
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/hudu/pagination.rb
CHANGED
@@ -15,7 +15,7 @@ module Hudu
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def page_options
|
18
|
-
{ page: @page }
|
18
|
+
{ page: @page, page_size: @page_size }
|
19
19
|
end
|
20
20
|
|
21
21
|
def next_page!(body)
|
@@ -31,12 +31,14 @@ module Hudu
|
|
31
31
|
|
32
32
|
def self.data(body)
|
33
33
|
# assume hash {"resource":[...]}, get first key and return array data
|
34
|
-
|
35
|
-
if
|
36
|
-
v
|
37
|
-
|
38
|
-
|
34
|
+
result = body
|
35
|
+
if result && result.respond_to?(:first)
|
36
|
+
k,v = body.first
|
37
|
+
if v.is_a?(Array) || v.is_a?(Hash)
|
38
|
+
result = v
|
39
|
+
end
|
39
40
|
end
|
41
|
+
result
|
40
42
|
end
|
41
43
|
end
|
42
44
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'thread'
|
3
|
+
|
4
|
+
# A Faraday middleware for rate limiting requests.
|
5
|
+
#
|
6
|
+
# This middleware ensures that the number of requests made through a Faraday connection
|
7
|
+
# does not exceed a specified limit within a given time period.
|
8
|
+
#
|
9
|
+
# @example Add middleware to a Faraday connection
|
10
|
+
# connection = Faraday.new(url: 'https://api.example.com') do |faraday|
|
11
|
+
# faraday.use RateThrottleMiddleware, limit: 300, period: 60
|
12
|
+
# faraday.adapter Faraday.default_adapter
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# @see https://github.com/lostisland/faraday Faraday Documentation
|
16
|
+
#
|
17
|
+
class RateThrottleMiddleware < Faraday::Middleware
|
18
|
+
# Initializes the RateThrottleMiddleware.
|
19
|
+
#
|
20
|
+
# @param app [#call] The next middleware or the actual Faraday adapter.
|
21
|
+
# @param limit [Integer] The maximum number of requests allowed within the specified period. Default is 300.
|
22
|
+
# @param period [Integer] The time period in seconds over which the limit applies. Default is 60 seconds.
|
23
|
+
#
|
24
|
+
# @example
|
25
|
+
# middleware = RateThrottleMiddleware.new(app, limit: 300, period: 60)
|
26
|
+
#
|
27
|
+
def initialize(app, limit: 300, period: 60)
|
28
|
+
super(app)
|
29
|
+
@limit = limit
|
30
|
+
@period = period
|
31
|
+
@requests = []
|
32
|
+
@mutex = Mutex.new
|
33
|
+
@condition = ConditionVariable.new
|
34
|
+
end
|
35
|
+
|
36
|
+
def call(env)
|
37
|
+
throttle_request
|
38
|
+
@app.call(env)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def throttle_request
|
44
|
+
@mutex.synchronize do
|
45
|
+
now = Time.now.to_f
|
46
|
+
# Clear requests older than the rate limit period
|
47
|
+
while !@requests.empty? && @requests[0] < (now - @period)
|
48
|
+
@requests.pop
|
49
|
+
end
|
50
|
+
|
51
|
+
# Wait if the request limit is reached
|
52
|
+
while @requests.size >= @limit
|
53
|
+
sleep_time = @requests[0] + @period - now
|
54
|
+
puts "<#{sleep_time}>"
|
55
|
+
@condition.wait(@mutex, sleep_time) if sleep_time > 0
|
56
|
+
now = Time.now.to_f
|
57
|
+
while !@requests.empty? && @requests[0] < (now - @period)
|
58
|
+
@requests.pop
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Record the new request
|
63
|
+
@requests.push(Time.now.to_f)
|
64
|
+
@condition.broadcast
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/hudu/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hudu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
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-
|
11
|
+
date: 2024-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -114,8 +114,10 @@ files:
|
|
114
114
|
- lib/hudu/authentication.rb
|
115
115
|
- lib/hudu/client.rb
|
116
116
|
- lib/hudu/configuration.rb
|
117
|
+
- lib/hudu/connection.rb
|
117
118
|
- lib/hudu/error.rb
|
118
119
|
- lib/hudu/pagination.rb
|
120
|
+
- lib/hudu/rate_throttle_middleware.rb
|
119
121
|
- lib/hudu/version.rb
|
120
122
|
homepage: https://rubygems.org/gems/hudu
|
121
123
|
licenses:
|
@@ -138,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
140
|
- !ruby/object:Gem::Version
|
139
141
|
version: '0'
|
140
142
|
requirements: []
|
141
|
-
rubygems_version: 3.
|
143
|
+
rubygems_version: 3.1.6
|
142
144
|
signing_key:
|
143
145
|
specification_version: 4
|
144
146
|
summary: A Ruby wrapper for the Hudu APIs (readonly)
|