Class: ReverseMatchJob

Inherits:
Struct
  • Object
show all
Defined in:
app/jobs/reverse_match_job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#match_classObject

Returns the value of attribute match_class

Returns:

  • (Object)

    the current value of match_class



1
2
3
# File 'app/jobs/reverse_match_job.rb', line 1

def match_class
  @match_class
end

#objectObject

Returns the value of attribute object

Returns:

  • (Object)

    the current value of object



1
2
3
# File 'app/jobs/reverse_match_job.rb', line 1

def object
  @object
end

#originObject

Returns the value of attribute origin

Returns:

  • (Object)

    the current value of origin



1
2
3
# File 'app/jobs/reverse_match_job.rb', line 1

def origin
  @origin
end

#refererObject

Returns the value of attribute referer

Returns:

  • (Object)

    the current value of referer



1
2
3
# File 'app/jobs/reverse_match_job.rb', line 1

def referer
  @referer
end

#subjectObject

Returns the value of attribute subject

Returns:

  • (Object)

    the current value of subject



1
2
3
# File 'app/jobs/reverse_match_job.rb', line 1

def subject
  @subject
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



1
2
3
# File 'app/jobs/reverse_match_job.rb', line 1

def type
  @type
end

Instance Method Details

#enqueue(job) ⇒ Object



2
3
4
# File 'app/jobs/reverse_match_job.rb', line 2

def enqueue(job)
  JobRelation.create(owner_reference: self.origin, job: job)
end

#error(job, exception) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/jobs/reverse_match_job.rb', line 28

def error(job, exception)
  error_type = nil

  case exception
  when Faraday::Error::ConnectionFailed
    error_type = 'connection_failed'
  when Faraday::Error::TimeoutError
    error_type = 'timeout_error'
  when Faraday::Error::ResourceNotFound
    error_type = 'resource_not_found'
  when Faraday::ClientError
    body = exception.response[:body] || {}
    message = JSON.parse(body) unless body.empty?
    error_type = message['type']
  end

  unless error_type.nil?
    reference = JobRelation.find_by(owner_reference: self.origin, job: job)
    reference.update_attribute(:response_error, error_type)
  end
end

#performObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/jobs/reverse_match_job.rb', line 6

def perform
  conn = connection(subject, { accept: 'application/json' })
  response = conn.get
  link = response.body['links'].detect { |h| h['rel'] == type.to_s }
  request_url = link['href']
  request_method = link['method']

  conn = connection(request_url, { content_type: 'application/json', referer: referer })

  begin
    response = conn.send(request_method) do |req|
      req.params['match_class'] = match_class
      req.params['uri'] = object
    end
  rescue Faraday::ClientError => e
    if e.response.nil? || response[:status] != 409
      raise e
    end
  end

end

#success(job) ⇒ Object



50
51
52
53
# File 'app/jobs/reverse_match_job.rb', line 50

def success(job)
  reference = JobRelation.find_by(owner_reference: self.origin, job: job)
  reference.delete
end