Class: DiscourseSubscriptionClient::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/discourse_subscription_client/request.rb

Constant Summary collapse

SUPPLIER_ERROR_LIMIT =
3
RESOURCE_ERROR_LIMIT =
5
VALID_TYPES =
%w[resource supplier].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, id) ⇒ Request

Returns a new instance of Request.



12
13
14
15
# File 'lib/discourse_subscription_client/request.rb', line 12

def initialize(type, id)
  @type = type.to_s
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/discourse_subscription_client/request.rb', line 9

def id
  @id
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/discourse_subscription_client/request.rb', line 9

def type
  @type
end

Class Method Details

.current_error(type, id) ⇒ Object



64
65
66
# File 'lib/discourse_subscription_client/request.rb', line 64

def self.current_error(type, id)
  new(type, id).current_error
end

Instance Method Details

#create_error(url, response) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/discourse_subscription_client/request.rb', line 72

def create_error(url, response)
  if (error = current_error)
    error.count = error.count.to_i + 1
  else
    error = SubscriptionClientRequest.new(
      request_id: id,
      request_type: type,
      message: I18n.t("subscription_client.notices.connection_error", url: url),
      count: 1
    )
  end

  if response.present?
    begin
      body = JSON.parse(response.body)
    rescue JSON::ParserError
      body = nil
    end

    error.response = {
      status: response.status,
      body: body
    }

    error.message = body["error"] if body && body["error"]
  end

  error.save

  @current_error = nil

  return unless reached_limit?

  SubscriptionClientNotice.notify_connection_error(type, id)

  return unless type === "supplier" && (supplier = SubscriptionClientSupplier.find_by_id(id))

  supplier.deactivate_all_subscriptions!
end

#current_error(query_only: false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/discourse_subscription_client/request.rb', line 52

def current_error(query_only: false)
  @current_error ||= begin
    query = SubscriptionClientRequest
            .where("request_id = :request_id AND request_type = :request_type AND expired_at IS NULL", request_id: id, request_type: type)

    return nil unless query.exists?
    return query if query_only

    query.first
  end
end

#expire_errorObject



112
113
114
115
116
117
118
119
120
# File 'lib/discourse_subscription_client/request.rb', line 112

def expire_error
  if (query = current_error(query_only: true))
    record = query.first
    record.expired_at = Time.now
    record.save
  end

  SubscriptionClientNotice.expire_connection_error(type, id)
end

#limitObject



68
69
70
# File 'lib/discourse_subscription_client/request.rb', line 68

def limit
  send("#{@type}_limit")
end

#perform(url, headers: {}, body: nil, opts: { method: "GET" }) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/discourse_subscription_client/request.rb', line 17

def perform(url, headers: {}, body: nil, opts: { method: "GET" })
  return nil unless VALID_TYPES.include?(type)

  if body
    uri = URI.parse(url)
    uri.query = CGI.unescape(body.to_query)
    url = uri.to_s
  end

  headers.merge!({ "Origin" => Discourse.base_url_no_prefix })

  connection = Excon.new(url, headers: headers)

  begin
    response = connection.request(opts)
  rescue Excon::Error::Socket, Excon::Error::Timeout
    response = nil
  end

  if response && response.status == 200
    expire_error

    begin
      data = JSON.parse(response.body).deep_symbolize_keys
    rescue JSON::ParserError
      return nil
    end

    data
  else
    create_error(url, response)
    nil
  end
end

#reached_limit?Boolean

Returns:

  • (Boolean)


130
131
132
133
134
# File 'lib/discourse_subscription_client/request.rb', line 130

def reached_limit?
  return false unless current_error.present?

  current_error.count.to_i >= limit
end

#resource_limitObject



126
127
128
# File 'lib/discourse_subscription_client/request.rb', line 126

def resource_limit
  RESOURCE_ERROR_LIMIT
end

#supplier_limitObject



122
123
124
# File 'lib/discourse_subscription_client/request.rb', line 122

def supplier_limit
  SUPPLIER_ERROR_LIMIT
end