Class: Fastlane::Actions::CuttlyApiAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/cuttly_api/actions/cuttly_api_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



75
76
77
# File 'lib/fastlane/plugin/cuttly_api/actions/cuttly_api_action.rb', line 75

def self.authors
  ["Yalan"]
end

.available_optionsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fastlane/plugin/cuttly_api/actions/cuttly_api_action.rb', line 88

def self.available_options
  [
      FastlaneCore::ConfigItem.new(key: :api_key,
                                   env_name: "CUTTLY_API_KEY",
                                   description: "Your cuttly's api key",
                                   optional: true,
                                   type: String),
      FastlaneCore::ConfigItem.new(key: :shorten_url,
                                   env_name: "SHORTEN_URL",
                                   description: "Url You want shorten",
                                   optional: true,
                                   type: String),
      FastlaneCore::ConfigItem.new(key: :custom_url_alias,
                                   env_name: "CUSTOM_URL_ALIAS",
                                   description: "Custom url alias",
                                   optional: true,
                                   type: String)
  ]
end

.call_endpoint(url, http_method, params) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/fastlane/plugin/cuttly_api/actions/cuttly_api_action.rb', line 116

def self.call_endpoint(url, http_method, params)
  Actions.verify_gem!('excon')
  require 'excon'
  connection = Excon.new(url, :header => {'Content-Type' => 'text/html; charset=UTF-8'}, :query => params)
  connection.request(
      method: http_method
  )
end

.descriptionObject



71
72
73
# File 'lib/fastlane/plugin/cuttly_api/actions/cuttly_api_action.rb', line 71

def self.description
  "fastlane plugin for cuttly."
end

.detailsObject



83
84
85
86
# File 'lib/fastlane/plugin/cuttly_api/actions/cuttly_api_action.rb', line 83

def self.details
  # Optional:
  ""
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
# File 'lib/fastlane/plugin/cuttly_api/actions/cuttly_api_action.rb', line 108

def self.is_supported?(platform)
  # Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
  # See: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
  #
  # [:ios, :mac, :android].include?(platform)
  true
end

.return_valueObject



79
80
81
# File 'lib/fastlane/plugin/cuttly_api/actions/cuttly_api_action.rb', line 79

def self.return_value
  # If your method provides a return value, you can describe here what it does
end

.run(params) ⇒ Object



14
15
16
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fastlane/plugin/cuttly_api/actions/cuttly_api_action.rb', line 14

def self.run(params)
  unless params[:api_key]
    UI.error("Without cuttly api key. Please provide :api_key")
    return
  end
  unless params[:shorten_url]
    UI.error("Without shorten url you want. Please provide :shorten_url")
    return
  end

  http_method = 'GET'
  api_url = 'https://cutt.ly/api/api.php'
  if params[:custom_url_alias] and params[:custom_url_alias] != ""
    ps = {:key => params[:api_key],
          :short => params[:shorten_url],
          :name => params[:custom_url_alias]}
  else
    ps = {:key => params[:api_key],
          :short => params[:shorten_url]}
  end
  UI.message("Start call the cuttly api.")
  response = call_endpoint(
      api_url,
      http_method,
      ps
  )
  status_code = response[:status]

  if status_code.between?(200, 299)
    json = JSON.parse(response.body)
    status = json['url']['status']
    case status
    when 1..6
      UI.error("Response error code:#{status}, Please get more info on the cuttly document website: https://cutt.ly/cuttly-api.")
    when 7
      title = json['url']['title']
      short_link = json['url']['shortLink']
      date = json['url']['date']
      Actions.lane_context[SharedValues::CUTTLY_RESPONSE_TITLE] = title
      Actions.lane_context[SharedValues::CUTTLY_RESPONSE_LINK] = short_link
      Actions.lane_context[SharedValues::CUTTLY_RESPONSE_DATE] = date
    end
  else
    handled_error = error_handlers[status_code] || error_handlers['*']
    if handled_error
      handled_error.call(result)
    else
      UI.error("---")
      UI.error("Request failed:\n#{http_method}: #{url}")
      UI.error("Response:")
      UI.error(response.body)
      UI.error("Cuttly responded with #{status_code}\n---\n#{response.body}")
    end
  end

end