Class: WebHook

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/web_hook.rb

Instance Method Summary collapse

Instance Method Details

#clear_timestampsObject



9
10
11
12
13
# File 'app/models/web_hook.rb', line 9

def clear_timestamps
  return unless self.url_changed?
  self.last_sent_at = nil
  self.last_response = nil
end

#request(data) ⇒ Object



35
36
37
38
39
40
41
# File 'app/models/web_hook.rb', line 35

def request(data)
  Typhoeus::Request.new(url,
    method: :post,
    timeout_ms: 10000,
    body: Oj.dump(data, mode: :compat),
    headers: { 'Content-Type' => 'application/json', 'Accept-Encoding' => 'application/json' })
end

#send_new_version(project, platform, version_or_tag, requirements = []) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/web_hook.rb', line 20

def send_new_version(project, platform, version_or_tag, requirements = [])
  send_payload({
    event: 'new_version',
    repository: github_repository.full_name,
    platform: platform,
    name: project.name,
    version: version_or_tag.number,
    default_branch: github_repository.default_branch,
    package_manager_url: Repositories::Base.package_link(project, version_or_tag),
    published_at: version_or_tag.published_at,
    requirements: requirements,
    project: project.as_json(only: [:name, :platform, :description,  :homepage, :language, :repository_url, :stars, :latest_release_published_at, :normalized_licenses])
  })
end

#send_payload(data) ⇒ Object



43
44
45
46
# File 'app/models/web_hook.rb', line 43

def send_payload(data)
  response = request(data).run
  update_attributes(last_sent_at: Time.now.utc, last_response: response.response_code)
end

#send_test_payloadObject



15
16
17
18
# File 'app/models/web_hook.rb', line 15

def send_test_payload
  v = Version.first
  send_new_version(v.project, v.project.platform, v)
end