Module: CDG::Services

Defined in:
lib/cdg/services.rb,
lib/cdg/services/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.get_android_version(app_id:) ⇒ Object

eg sg.codigo.app_name



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cdg/services.rb', line 23

def self.get_android_version app_id: # eg sg.codigo.app_name
  begin
    resp = Nokogiri::HTML(open("https://play.google.com/store/apps/details?id=#{ app_id }&hl=en"))
    elements = resp.css("div[class='content'][itemprop='softwareVersion']")

    if elements.empty?
      nil
    else
      elements[0].text.strip
    end
  rescue OpenURI::HTTPError => e
    return nil
  end
end

.get_ios_version(app_id:) ⇒ Object

eg 123456789



12
13
14
15
16
17
18
19
20
21
# File 'lib/cdg/services.rb', line 12

def self.get_ios_version app_id: # eg 123456789
  resp = JSON.parse Net::HTTP.get(URI("https://itunes.apple.com/lookup?id=#{app_id}"))
  results = resp["results"]

  if results.empty?
    nil
  else
    results[0]["version"]
  end
end

.ping_slack!(webhook: ENV['SLACK_URL'], color: "5bc0de", title: nil, title_link: nil, pretext: nil, text:, fields: nil, channel: nil, username: nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cdg/services.rb', line 38

def self.ping_slack!(
  webhook: ENV['SLACK_URL'],
  color: "5bc0de", # default boostrap color for info
  title: nil,
  title_link: nil,
  pretext: nil,
  text: ,
  fields: nil,
  channel: nil,
  username: nil)
  begin
    if fields && !fields.is_a?(Array)
      raise ArgumentError, "wrong argument type for \"fields\"; should be an array"
    end

    uri = URI(webhook)

    resp = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
      req = Net::HTTP::Post.new(uri)
      req['Content-Type'] = 'application/json'

      attachment = {}
      attachment[:text] = text
      attachment[:color] = color if color
      attachment[:title] = title if title
      attachment[:title_link] = title_link if title_link
      attachment[:pretext] = pretext if pretext
      attachment[:fields] = fields if fields

      body = { attachments: [attachment] }
      body[:channel] = channel if channel
      body[:username] = username if username
      
      req.body = body.to_json
      resp = http.request(req)

      ## TODO need to find a better way handle response body?
      if resp.is_a?(Net::HTTPNotFound)
        case resp.body
        when "channel_not_found"
          raise ArgumentError, "slack channel is not found"
        else
          # TODO
        end
      end
    end
  rescue => e
    raise e
  end
end