Class: BadgeApi

Inherits:
CoreApi show all
Defined in:
lib/badge_api.rb

Overview

class used to download badges from shields.io

Constant Summary collapse

INVALID_COUNT =

constant that is used to show message for invalid badge

'invalid'
BASE_URL =

constant that is used for fetching badges.

'https://img.shields.io'

Instance Attribute Summary collapse

Attributes inherited from CoreApi

#base_url, #hostname

Instance Method Summary collapse

Methods inherited from CoreApi

#add_cookie_header, #callback_before_success, #callback_error, #do_fetch_real_data, #em_connection_options, #em_request, #em_request_options, #fetch_additional_headers, #fetch_data, #fetch_real_data, #get_cookie_string_for_base_url, #handle_http_callback, #persist_cookies, #persist_cookies_for_url, #register_error_callback, #register_success_callback

Methods included from Helper

available_extension?, clean_image_label, dispatch_http_response, display_total, display_type, env_production?, fetch_content_type, find_version, force_utf8_encoding, format_error, get_latest_stable_version_details, get_string_from_cookie_data, http_valid_content_types?, http_valid_status_code?, last_version, metric_power, metric_prefixes, non_empty_http_response?, options_base_url, parse_gem_version, parse_json, parsed_url_property, print_to_output_buffer, root, rubygems_valid_response?, setup_options_for_url, shields_io_valid_response?, sorted_versions, stable_gem_versions, valid_http_code_returned?, valid_http_response?

Constructor Details

#initialize(request, params, output_buffer, downloads, http_response) ⇒ void

Initializes the instance with the params from controller, and will try to download the information about the rubygems and then will try to download the badge to the output stream

Parameters:

  • request (Rack::Request)

    THe request received by Sinatra (used by the middleware to detect bad responses)

  • params (Hash)

    THe params parsed by Sinatra

  • output_buffer (Sinatra::Stream)

    describe output_buffer

  • downloads (Number)

    The downloads number received after parsing the http_response from the other service

  • http_response (JSON)

    The HTTP response parsed as JSON from the other service

Options Hash (params):

  • :color (String)

    The color of the badge

  • :style (String)

    The style of the badge

  • :metric (Boolean)

    This will decide if the number will be formatted using metric or delimiters

See Also:


56
57
58
59
60
61
62
63
64
# File 'lib/badge_api.rb', line 56

def initialize(request, params, output_buffer, downloads, http_response)
  @params = params
  @request = request
  @output_buffer = output_buffer
  @downloads = downloads
  @http_response = http_response
  @default_options = { 'request_name' => @params.fetch('request_name', nil) }
  fetch_image_shield
end

Instance Attribute Details

#default_optionsHash (readonly)

Returns THe default options that are sent to #fetch_data method.

Returns:

  • (Hash)

    THe default options that are sent to #fetch_data method


40
41
42
# File 'lib/badge_api.rb', line 40

def default_options
  @default_options
end

#downloadsHash

Returns THe downloads count that will need to be displayed on the badge.

Returns:

  • (Hash)

    THe downloads count that will need to be displayed on the badge


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
70
71
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/badge_api.rb', line 17

class BadgeApi < CoreApi
  # constant that is used to show message for invalid badge
  INVALID_COUNT = 'invalid'

  # constant that is used for fetching badges.
  BASE_URL = 'https://img.shields.io'

  # @return [Rack::Request] THe request received by Sinatra (used by the middleware to detect bad responses)
  attr_reader :request

  # @return [Hash] The params that Sinatra received
  attr_reader :params

  # @return [Sinatra::Stream] The Sinatra Stream to which the badge will be inserted into
  attr_reader :output_buffer

  # @return [Hash] THe downloads count that will need to be displayed on the badge
  attr_reader :downloads

  # @return [Hash] THe http response receives from the other service, used for providing JSON format
  attr_reader :http_response

  # @return [Hash] THe default options that are sent to #fetch_data method
  attr_reader :default_options

  # Initializes the instance with the params from controller, and will try to download the information about the rubygems
  # and then will try to download the badge to the output stream
  # @see #fetch_image_shield
  # @see RubygemsApi#fetch_downloads_data
  #
  # @param [Rack::Request] request THe request received by Sinatra (used by the middleware to detect bad responses)
  # @param [Hash] params THe params parsed by Sinatra
  # @option params [String] :color The color of the badge
  # @option params [String]:style The style of the badge
  # @option params [Boolean] :metric This will decide if the number will be formatted using metric or delimiters
  # @param [Sinatra::Stream] output_buffer describe output_buffer
  # @param [Number] downloads The downloads number received after parsing the http_response from the other service
  # @param [JSON] http_response The HTTP response parsed as JSON from the other service
  # @return [void]
  def initialize(request, params, output_buffer, downloads, http_response)
    @params = params
    @request = request
    @output_buffer = output_buffer
    @downloads = downloads
    @http_response = http_response
    @default_options = { 'request_name' => @params.fetch('request_name', nil) }
    fetch_image_shield
  end

  # Parses the query string from the request with CGI in order to provide a integration with social badges
  # @see Rack::Request#query_string
  # @see CGI::parse
  #
  # @return [Hash] the parsed query string in Hash format but making array params as arrays instead of hashes
  def original_params
    @original_params ||= CGI::parse(@request.query_string)
  end

  # Fetches the param style from the params , and if is not present will return by default 'flat'
  #
  # @return [String] Returns the param style from params , otherwise will return by default 'flat'
  def style_param
    @style_param ||= @params.fetch('style', 'flat') || 'flat'
  end

  # Fetches the maxAge from the params , and if is not present will return by default 2592000
  #
  # @return [Integer] Returns the maxAge from params , otherwise will return by default 2592000
  def max_age_param
    @max_age_param ||= @params.fetch('maxAge', 2_592_000) || 2_592_000
  end

  # Fetches the link params from the original params used for social badges
  #
  # @return [String] Returns the link param otherwise empty string
  def link_param
    @link_param ||= original_params.fetch('link', '') || ''
  end

  # Checks if the badge is a social badge and if the params contains links and returns the links for the badge
  #
  # @return [String] Returns the links used for social badges
  def style_additionals
    return if style_param != 'social' || link_param.blank?
    "&link=#{link_param[0]}&link=#{link_param[1]}"
  end

  # Fetches the logo from the params, otherwise empty string
  #
  # @return [String] Returns the logo from the params, otherwise empty string
  def logo_param
    @logo_param ||= @params.fetch('logo', '') || ''
  end

  # Fetches the logo width from the params, otherwise empty string
  #
  # @return [String] Returns the logo width from the params, otherwise empty string
  def logo_width
    @logo_width ||= @params.fetch('logoWidth', 0).to_s.to_i || 0
  end

  # Fetches the logo padding from the params, otherwise empty string
  #
  # @return [String] Returns the logo padding from the params, otherwise empty string
  def logo_padding
    @logo_padding ||= @params.fetch('logoPadding', 0).to_s.to_i || 0
  end

  # Checks if any additional params are present in URL and adds them to the URL constructed for the badge
  #
  # @return [String] Returns the URL query string used for displaying the badge
  def additional_params
    additionals = {
      'logo': logo_param,
      'logoWidth': logo_width,
      'logoPadding': logo_padding,
      'style': style_param,
      'maxAge': max_age_param.to_i
    }.delete_if { |_key, value| value.blank? || (value.is_a?(Numeric) && value.zero?) }
    additionals = additionals.to_query
    "#{additionals}#{style_additionals}"
  end

  # Method that is used to fetch the status of the badge
  #
  # @return [String] Returns the status of the badge
  def status_param
    @status_param ||= begin
      status_param = @params.fetch('label', 'downloads') || 'downloads'
      status_param = status_param.present? ? status_param : 'downloads'
      clean_image_label(status_param)
    end
  end

  # Method that is used to set the image extension
  #
  # @return [String] Returns the status of the badge
  def image_extension
    @image_extension ||= begin
      param_extension = @params['extension'].to_s
      available_extension?(param_extension) ? param_extension : 'svg'
    end
  end

  # Method that is used to determine the image color, by default blue.
  # In case the downloads are blank , will return lightgrey
  #
  # @return [String] Returns the color of the badge (Default: blue)
  def image_colour
    @image_colour ||= @downloads.blank? ? 'lightgrey' : @params.fetch('color', 'blue')
  end

  # Method used to build the shield URL for fetching the SVG image
  # @see #format_number_of_downloads
  # @return [String] The URL that will be used in fetching the SVG image from shields.io server
  def build_badge_url(extension = image_extension)
    "#{BadgeApi::BASE_URL}/badge/#{status_param}-#{format_number_of_downloads}-#{image_colour}.#{extension}?#{additional_params}"
  end

  # Method that is used for building the URL for fetching the SVG Image, and actually
  # making the HTTP connection and adding the response to the stream
  # @see #build_badge_url
  # @see Helper#fetch_data
  # @see Helper#print_to_output_buffer
  #
  # @return [void]
  def fetch_image_shield
    fetch_data(build_badge_url, @default_options) do |http_response|
      print_to_output_buffer(http_response, @output_buffer)
    end
  end

  # callback that is called when http request fails
  # def callback_error(error, options)
  #  super(error, options)
  #  output = svg_template.fetch_badge_image
  #  print_to_output_buffer(output, @output_buffer)
  # end

  # Method that is used for formatting the number of downloads , if the number is blank, will return invalid,
  # otherwise will format the number using the configuration from params, either using metrics or delimiters
  # @see  NumberFormatter#initialize
  # @see NumberFormatter#formatted_display
  #
  # @return [String] If the downloads argument is blank will return invalid, otherwise will format the numbere either with metrics or delimiters
  def format_number_of_downloads
    @format_number_of_downloads ||= (@downloads.blank? ? BadgeApi::INVALID_COUNT : NumberFormatter.new(@downloads, @params).to_s)
  end
end

#http_responseHash

Returns THe http response receives from the other service, used for providing JSON format.

Returns:

  • (Hash)

    THe http response receives from the other service, used for providing JSON format


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
70
71
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/badge_api.rb', line 17

class BadgeApi < CoreApi
  # constant that is used to show message for invalid badge
  INVALID_COUNT = 'invalid'

  # constant that is used for fetching badges.
  BASE_URL = 'https://img.shields.io'

  # @return [Rack::Request] THe request received by Sinatra (used by the middleware to detect bad responses)
  attr_reader :request

  # @return [Hash] The params that Sinatra received
  attr_reader :params

  # @return [Sinatra::Stream] The Sinatra Stream to which the badge will be inserted into
  attr_reader :output_buffer

  # @return [Hash] THe downloads count that will need to be displayed on the badge
  attr_reader :downloads

  # @return [Hash] THe http response receives from the other service, used for providing JSON format
  attr_reader :http_response

  # @return [Hash] THe default options that are sent to #fetch_data method
  attr_reader :default_options

  # Initializes the instance with the params from controller, and will try to download the information about the rubygems
  # and then will try to download the badge to the output stream
  # @see #fetch_image_shield
  # @see RubygemsApi#fetch_downloads_data
  #
  # @param [Rack::Request] request THe request received by Sinatra (used by the middleware to detect bad responses)
  # @param [Hash] params THe params parsed by Sinatra
  # @option params [String] :color The color of the badge
  # @option params [String]:style The style of the badge
  # @option params [Boolean] :metric This will decide if the number will be formatted using metric or delimiters
  # @param [Sinatra::Stream] output_buffer describe output_buffer
  # @param [Number] downloads The downloads number received after parsing the http_response from the other service
  # @param [JSON] http_response The HTTP response parsed as JSON from the other service
  # @return [void]
  def initialize(request, params, output_buffer, downloads, http_response)
    @params = params
    @request = request
    @output_buffer = output_buffer
    @downloads = downloads
    @http_response = http_response
    @default_options = { 'request_name' => @params.fetch('request_name', nil) }
    fetch_image_shield
  end

  # Parses the query string from the request with CGI in order to provide a integration with social badges
  # @see Rack::Request#query_string
  # @see CGI::parse
  #
  # @return [Hash] the parsed query string in Hash format but making array params as arrays instead of hashes
  def original_params
    @original_params ||= CGI::parse(@request.query_string)
  end

  # Fetches the param style from the params , and if is not present will return by default 'flat'
  #
  # @return [String] Returns the param style from params , otherwise will return by default 'flat'
  def style_param
    @style_param ||= @params.fetch('style', 'flat') || 'flat'
  end

  # Fetches the maxAge from the params , and if is not present will return by default 2592000
  #
  # @return [Integer] Returns the maxAge from params , otherwise will return by default 2592000
  def max_age_param
    @max_age_param ||= @params.fetch('maxAge', 2_592_000) || 2_592_000
  end

  # Fetches the link params from the original params used for social badges
  #
  # @return [String] Returns the link param otherwise empty string
  def link_param
    @link_param ||= original_params.fetch('link', '') || ''
  end

  # Checks if the badge is a social badge and if the params contains links and returns the links for the badge
  #
  # @return [String] Returns the links used for social badges
  def style_additionals
    return if style_param != 'social' || link_param.blank?
    "&link=#{link_param[0]}&link=#{link_param[1]}"
  end

  # Fetches the logo from the params, otherwise empty string
  #
  # @return [String] Returns the logo from the params, otherwise empty string
  def logo_param
    @logo_param ||= @params.fetch('logo', '') || ''
  end

  # Fetches the logo width from the params, otherwise empty string
  #
  # @return [String] Returns the logo width from the params, otherwise empty string
  def logo_width
    @logo_width ||= @params.fetch('logoWidth', 0).to_s.to_i || 0
  end

  # Fetches the logo padding from the params, otherwise empty string
  #
  # @return [String] Returns the logo padding from the params, otherwise empty string
  def logo_padding
    @logo_padding ||= @params.fetch('logoPadding', 0).to_s.to_i || 0
  end

  # Checks if any additional params are present in URL and adds them to the URL constructed for the badge
  #
  # @return [String] Returns the URL query string used for displaying the badge
  def additional_params
    additionals = {
      'logo': logo_param,
      'logoWidth': logo_width,
      'logoPadding': logo_padding,
      'style': style_param,
      'maxAge': max_age_param.to_i
    }.delete_if { |_key, value| value.blank? || (value.is_a?(Numeric) && value.zero?) }
    additionals = additionals.to_query
    "#{additionals}#{style_additionals}"
  end

  # Method that is used to fetch the status of the badge
  #
  # @return [String] Returns the status of the badge
  def status_param
    @status_param ||= begin
      status_param = @params.fetch('label', 'downloads') || 'downloads'
      status_param = status_param.present? ? status_param : 'downloads'
      clean_image_label(status_param)
    end
  end

  # Method that is used to set the image extension
  #
  # @return [String] Returns the status of the badge
  def image_extension
    @image_extension ||= begin
      param_extension = @params['extension'].to_s
      available_extension?(param_extension) ? param_extension : 'svg'
    end
  end

  # Method that is used to determine the image color, by default blue.
  # In case the downloads are blank , will return lightgrey
  #
  # @return [String] Returns the color of the badge (Default: blue)
  def image_colour
    @image_colour ||= @downloads.blank? ? 'lightgrey' : @params.fetch('color', 'blue')
  end

  # Method used to build the shield URL for fetching the SVG image
  # @see #format_number_of_downloads
  # @return [String] The URL that will be used in fetching the SVG image from shields.io server
  def build_badge_url(extension = image_extension)
    "#{BadgeApi::BASE_URL}/badge/#{status_param}-#{format_number_of_downloads}-#{image_colour}.#{extension}?#{additional_params}"
  end

  # Method that is used for building the URL for fetching the SVG Image, and actually
  # making the HTTP connection and adding the response to the stream
  # @see #build_badge_url
  # @see Helper#fetch_data
  # @see Helper#print_to_output_buffer
  #
  # @return [void]
  def fetch_image_shield
    fetch_data(build_badge_url, @default_options) do |http_response|
      print_to_output_buffer(http_response, @output_buffer)
    end
  end

  # callback that is called when http request fails
  # def callback_error(error, options)
  #  super(error, options)
  #  output = svg_template.fetch_badge_image
  #  print_to_output_buffer(output, @output_buffer)
  # end

  # Method that is used for formatting the number of downloads , if the number is blank, will return invalid,
  # otherwise will format the number using the configuration from params, either using metrics or delimiters
  # @see  NumberFormatter#initialize
  # @see NumberFormatter#formatted_display
  #
  # @return [String] If the downloads argument is blank will return invalid, otherwise will format the numbere either with metrics or delimiters
  def format_number_of_downloads
    @format_number_of_downloads ||= (@downloads.blank? ? BadgeApi::INVALID_COUNT : NumberFormatter.new(@downloads, @params).to_s)
  end
end

#output_bufferSinatra::Stream

Returns The Sinatra Stream to which the badge will be inserted into.

Returns:

  • (Sinatra::Stream)

    The Sinatra Stream to which the badge will be inserted into


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
70
71
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/badge_api.rb', line 17

class BadgeApi < CoreApi
  # constant that is used to show message for invalid badge
  INVALID_COUNT = 'invalid'

  # constant that is used for fetching badges.
  BASE_URL = 'https://img.shields.io'

  # @return [Rack::Request] THe request received by Sinatra (used by the middleware to detect bad responses)
  attr_reader :request

  # @return [Hash] The params that Sinatra received
  attr_reader :params

  # @return [Sinatra::Stream] The Sinatra Stream to which the badge will be inserted into
  attr_reader :output_buffer

  # @return [Hash] THe downloads count that will need to be displayed on the badge
  attr_reader :downloads

  # @return [Hash] THe http response receives from the other service, used for providing JSON format
  attr_reader :http_response

  # @return [Hash] THe default options that are sent to #fetch_data method
  attr_reader :default_options

  # Initializes the instance with the params from controller, and will try to download the information about the rubygems
  # and then will try to download the badge to the output stream
  # @see #fetch_image_shield
  # @see RubygemsApi#fetch_downloads_data
  #
  # @param [Rack::Request] request THe request received by Sinatra (used by the middleware to detect bad responses)
  # @param [Hash] params THe params parsed by Sinatra
  # @option params [String] :color The color of the badge
  # @option params [String]:style The style of the badge
  # @option params [Boolean] :metric This will decide if the number will be formatted using metric or delimiters
  # @param [Sinatra::Stream] output_buffer describe output_buffer
  # @param [Number] downloads The downloads number received after parsing the http_response from the other service
  # @param [JSON] http_response The HTTP response parsed as JSON from the other service
  # @return [void]
  def initialize(request, params, output_buffer, downloads, http_response)
    @params = params
    @request = request
    @output_buffer = output_buffer
    @downloads = downloads
    @http_response = http_response
    @default_options = { 'request_name' => @params.fetch('request_name', nil) }
    fetch_image_shield
  end

  # Parses the query string from the request with CGI in order to provide a integration with social badges
  # @see Rack::Request#query_string
  # @see CGI::parse
  #
  # @return [Hash] the parsed query string in Hash format but making array params as arrays instead of hashes
  def original_params
    @original_params ||= CGI::parse(@request.query_string)
  end

  # Fetches the param style from the params , and if is not present will return by default 'flat'
  #
  # @return [String] Returns the param style from params , otherwise will return by default 'flat'
  def style_param
    @style_param ||= @params.fetch('style', 'flat') || 'flat'
  end

  # Fetches the maxAge from the params , and if is not present will return by default 2592000
  #
  # @return [Integer] Returns the maxAge from params , otherwise will return by default 2592000
  def max_age_param
    @max_age_param ||= @params.fetch('maxAge', 2_592_000) || 2_592_000
  end

  # Fetches the link params from the original params used for social badges
  #
  # @return [String] Returns the link param otherwise empty string
  def link_param
    @link_param ||= original_params.fetch('link', '') || ''
  end

  # Checks if the badge is a social badge and if the params contains links and returns the links for the badge
  #
  # @return [String] Returns the links used for social badges
  def style_additionals
    return if style_param != 'social' || link_param.blank?
    "&link=#{link_param[0]}&link=#{link_param[1]}"
  end

  # Fetches the logo from the params, otherwise empty string
  #
  # @return [String] Returns the logo from the params, otherwise empty string
  def logo_param
    @logo_param ||= @params.fetch('logo', '') || ''
  end

  # Fetches the logo width from the params, otherwise empty string
  #
  # @return [String] Returns the logo width from the params, otherwise empty string
  def logo_width
    @logo_width ||= @params.fetch('logoWidth', 0).to_s.to_i || 0
  end

  # Fetches the logo padding from the params, otherwise empty string
  #
  # @return [String] Returns the logo padding from the params, otherwise empty string
  def logo_padding
    @logo_padding ||= @params.fetch('logoPadding', 0).to_s.to_i || 0
  end

  # Checks if any additional params are present in URL and adds them to the URL constructed for the badge
  #
  # @return [String] Returns the URL query string used for displaying the badge
  def additional_params
    additionals = {
      'logo': logo_param,
      'logoWidth': logo_width,
      'logoPadding': logo_padding,
      'style': style_param,
      'maxAge': max_age_param.to_i
    }.delete_if { |_key, value| value.blank? || (value.is_a?(Numeric) && value.zero?) }
    additionals = additionals.to_query
    "#{additionals}#{style_additionals}"
  end

  # Method that is used to fetch the status of the badge
  #
  # @return [String] Returns the status of the badge
  def status_param
    @status_param ||= begin
      status_param = @params.fetch('label', 'downloads') || 'downloads'
      status_param = status_param.present? ? status_param : 'downloads'
      clean_image_label(status_param)
    end
  end

  # Method that is used to set the image extension
  #
  # @return [String] Returns the status of the badge
  def image_extension
    @image_extension ||= begin
      param_extension = @params['extension'].to_s
      available_extension?(param_extension) ? param_extension : 'svg'
    end
  end

  # Method that is used to determine the image color, by default blue.
  # In case the downloads are blank , will return lightgrey
  #
  # @return [String] Returns the color of the badge (Default: blue)
  def image_colour
    @image_colour ||= @downloads.blank? ? 'lightgrey' : @params.fetch('color', 'blue')
  end

  # Method used to build the shield URL for fetching the SVG image
  # @see #format_number_of_downloads
  # @return [String] The URL that will be used in fetching the SVG image from shields.io server
  def build_badge_url(extension = image_extension)
    "#{BadgeApi::BASE_URL}/badge/#{status_param}-#{format_number_of_downloads}-#{image_colour}.#{extension}?#{additional_params}"
  end

  # Method that is used for building the URL for fetching the SVG Image, and actually
  # making the HTTP connection and adding the response to the stream
  # @see #build_badge_url
  # @see Helper#fetch_data
  # @see Helper#print_to_output_buffer
  #
  # @return [void]
  def fetch_image_shield
    fetch_data(build_badge_url, @default_options) do |http_response|
      print_to_output_buffer(http_response, @output_buffer)
    end
  end

  # callback that is called when http request fails
  # def callback_error(error, options)
  #  super(error, options)
  #  output = svg_template.fetch_badge_image
  #  print_to_output_buffer(output, @output_buffer)
  # end

  # Method that is used for formatting the number of downloads , if the number is blank, will return invalid,
  # otherwise will format the number using the configuration from params, either using metrics or delimiters
  # @see  NumberFormatter#initialize
  # @see NumberFormatter#formatted_display
  #
  # @return [String] If the downloads argument is blank will return invalid, otherwise will format the numbere either with metrics or delimiters
  def format_number_of_downloads
    @format_number_of_downloads ||= (@downloads.blank? ? BadgeApi::INVALID_COUNT : NumberFormatter.new(@downloads, @params).to_s)
  end
end

#paramsHash

Returns The params that Sinatra received.

Returns:

  • (Hash)

    The params that Sinatra received


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
70
71
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/badge_api.rb', line 17

class BadgeApi < CoreApi
  # constant that is used to show message for invalid badge
  INVALID_COUNT = 'invalid'

  # constant that is used for fetching badges.
  BASE_URL = 'https://img.shields.io'

  # @return [Rack::Request] THe request received by Sinatra (used by the middleware to detect bad responses)
  attr_reader :request

  # @return [Hash] The params that Sinatra received
  attr_reader :params

  # @return [Sinatra::Stream] The Sinatra Stream to which the badge will be inserted into
  attr_reader :output_buffer

  # @return [Hash] THe downloads count that will need to be displayed on the badge
  attr_reader :downloads

  # @return [Hash] THe http response receives from the other service, used for providing JSON format
  attr_reader :http_response

  # @return [Hash] THe default options that are sent to #fetch_data method
  attr_reader :default_options

  # Initializes the instance with the params from controller, and will try to download the information about the rubygems
  # and then will try to download the badge to the output stream
  # @see #fetch_image_shield
  # @see RubygemsApi#fetch_downloads_data
  #
  # @param [Rack::Request] request THe request received by Sinatra (used by the middleware to detect bad responses)
  # @param [Hash] params THe params parsed by Sinatra
  # @option params [String] :color The color of the badge
  # @option params [String]:style The style of the badge
  # @option params [Boolean] :metric This will decide if the number will be formatted using metric or delimiters
  # @param [Sinatra::Stream] output_buffer describe output_buffer
  # @param [Number] downloads The downloads number received after parsing the http_response from the other service
  # @param [JSON] http_response The HTTP response parsed as JSON from the other service
  # @return [void]
  def initialize(request, params, output_buffer, downloads, http_response)
    @params = params
    @request = request
    @output_buffer = output_buffer
    @downloads = downloads
    @http_response = http_response
    @default_options = { 'request_name' => @params.fetch('request_name', nil) }
    fetch_image_shield
  end

  # Parses the query string from the request with CGI in order to provide a integration with social badges
  # @see Rack::Request#query_string
  # @see CGI::parse
  #
  # @return [Hash] the parsed query string in Hash format but making array params as arrays instead of hashes
  def original_params
    @original_params ||= CGI::parse(@request.query_string)
  end

  # Fetches the param style from the params , and if is not present will return by default 'flat'
  #
  # @return [String] Returns the param style from params , otherwise will return by default 'flat'
  def style_param
    @style_param ||= @params.fetch('style', 'flat') || 'flat'
  end

  # Fetches the maxAge from the params , and if is not present will return by default 2592000
  #
  # @return [Integer] Returns the maxAge from params , otherwise will return by default 2592000
  def max_age_param
    @max_age_param ||= @params.fetch('maxAge', 2_592_000) || 2_592_000
  end

  # Fetches the link params from the original params used for social badges
  #
  # @return [String] Returns the link param otherwise empty string
  def link_param
    @link_param ||= original_params.fetch('link', '') || ''
  end

  # Checks if the badge is a social badge and if the params contains links and returns the links for the badge
  #
  # @return [String] Returns the links used for social badges
  def style_additionals
    return if style_param != 'social' || link_param.blank?
    "&link=#{link_param[0]}&link=#{link_param[1]}"
  end

  # Fetches the logo from the params, otherwise empty string
  #
  # @return [String] Returns the logo from the params, otherwise empty string
  def logo_param
    @logo_param ||= @params.fetch('logo', '') || ''
  end

  # Fetches the logo width from the params, otherwise empty string
  #
  # @return [String] Returns the logo width from the params, otherwise empty string
  def logo_width
    @logo_width ||= @params.fetch('logoWidth', 0).to_s.to_i || 0
  end

  # Fetches the logo padding from the params, otherwise empty string
  #
  # @return [String] Returns the logo padding from the params, otherwise empty string
  def logo_padding
    @logo_padding ||= @params.fetch('logoPadding', 0).to_s.to_i || 0
  end

  # Checks if any additional params are present in URL and adds them to the URL constructed for the badge
  #
  # @return [String] Returns the URL query string used for displaying the badge
  def additional_params
    additionals = {
      'logo': logo_param,
      'logoWidth': logo_width,
      'logoPadding': logo_padding,
      'style': style_param,
      'maxAge': max_age_param.to_i
    }.delete_if { |_key, value| value.blank? || (value.is_a?(Numeric) && value.zero?) }
    additionals = additionals.to_query
    "#{additionals}#{style_additionals}"
  end

  # Method that is used to fetch the status of the badge
  #
  # @return [String] Returns the status of the badge
  def status_param
    @status_param ||= begin
      status_param = @params.fetch('label', 'downloads') || 'downloads'
      status_param = status_param.present? ? status_param : 'downloads'
      clean_image_label(status_param)
    end
  end

  # Method that is used to set the image extension
  #
  # @return [String] Returns the status of the badge
  def image_extension
    @image_extension ||= begin
      param_extension = @params['extension'].to_s
      available_extension?(param_extension) ? param_extension : 'svg'
    end
  end

  # Method that is used to determine the image color, by default blue.
  # In case the downloads are blank , will return lightgrey
  #
  # @return [String] Returns the color of the badge (Default: blue)
  def image_colour
    @image_colour ||= @downloads.blank? ? 'lightgrey' : @params.fetch('color', 'blue')
  end

  # Method used to build the shield URL for fetching the SVG image
  # @see #format_number_of_downloads
  # @return [String] The URL that will be used in fetching the SVG image from shields.io server
  def build_badge_url(extension = image_extension)
    "#{BadgeApi::BASE_URL}/badge/#{status_param}-#{format_number_of_downloads}-#{image_colour}.#{extension}?#{additional_params}"
  end

  # Method that is used for building the URL for fetching the SVG Image, and actually
  # making the HTTP connection and adding the response to the stream
  # @see #build_badge_url
  # @see Helper#fetch_data
  # @see Helper#print_to_output_buffer
  #
  # @return [void]
  def fetch_image_shield
    fetch_data(build_badge_url, @default_options) do |http_response|
      print_to_output_buffer(http_response, @output_buffer)
    end
  end

  # callback that is called when http request fails
  # def callback_error(error, options)
  #  super(error, options)
  #  output = svg_template.fetch_badge_image
  #  print_to_output_buffer(output, @output_buffer)
  # end

  # Method that is used for formatting the number of downloads , if the number is blank, will return invalid,
  # otherwise will format the number using the configuration from params, either using metrics or delimiters
  # @see  NumberFormatter#initialize
  # @see NumberFormatter#formatted_display
  #
  # @return [String] If the downloads argument is blank will return invalid, otherwise will format the numbere either with metrics or delimiters
  def format_number_of_downloads
    @format_number_of_downloads ||= (@downloads.blank? ? BadgeApi::INVALID_COUNT : NumberFormatter.new(@downloads, @params).to_s)
  end
end

#requestRack::Request

Returns THe request received by Sinatra (used by the middleware to detect bad responses).

Returns:

  • (Rack::Request)

    THe request received by Sinatra (used by the middleware to detect bad responses)


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
70
71
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/badge_api.rb', line 17

class BadgeApi < CoreApi
  # constant that is used to show message for invalid badge
  INVALID_COUNT = 'invalid'

  # constant that is used for fetching badges.
  BASE_URL = 'https://img.shields.io'

  # @return [Rack::Request] THe request received by Sinatra (used by the middleware to detect bad responses)
  attr_reader :request

  # @return [Hash] The params that Sinatra received
  attr_reader :params

  # @return [Sinatra::Stream] The Sinatra Stream to which the badge will be inserted into
  attr_reader :output_buffer

  # @return [Hash] THe downloads count that will need to be displayed on the badge
  attr_reader :downloads

  # @return [Hash] THe http response receives from the other service, used for providing JSON format
  attr_reader :http_response

  # @return [Hash] THe default options that are sent to #fetch_data method
  attr_reader :default_options

  # Initializes the instance with the params from controller, and will try to download the information about the rubygems
  # and then will try to download the badge to the output stream
  # @see #fetch_image_shield
  # @see RubygemsApi#fetch_downloads_data
  #
  # @param [Rack::Request] request THe request received by Sinatra (used by the middleware to detect bad responses)
  # @param [Hash] params THe params parsed by Sinatra
  # @option params [String] :color The color of the badge
  # @option params [String]:style The style of the badge
  # @option params [Boolean] :metric This will decide if the number will be formatted using metric or delimiters
  # @param [Sinatra::Stream] output_buffer describe output_buffer
  # @param [Number] downloads The downloads number received after parsing the http_response from the other service
  # @param [JSON] http_response The HTTP response parsed as JSON from the other service
  # @return [void]
  def initialize(request, params, output_buffer, downloads, http_response)
    @params = params
    @request = request
    @output_buffer = output_buffer
    @downloads = downloads
    @http_response = http_response
    @default_options = { 'request_name' => @params.fetch('request_name', nil) }
    fetch_image_shield
  end

  # Parses the query string from the request with CGI in order to provide a integration with social badges
  # @see Rack::Request#query_string
  # @see CGI::parse
  #
  # @return [Hash] the parsed query string in Hash format but making array params as arrays instead of hashes
  def original_params
    @original_params ||= CGI::parse(@request.query_string)
  end

  # Fetches the param style from the params , and if is not present will return by default 'flat'
  #
  # @return [String] Returns the param style from params , otherwise will return by default 'flat'
  def style_param
    @style_param ||= @params.fetch('style', 'flat') || 'flat'
  end

  # Fetches the maxAge from the params , and if is not present will return by default 2592000
  #
  # @return [Integer] Returns the maxAge from params , otherwise will return by default 2592000
  def max_age_param
    @max_age_param ||= @params.fetch('maxAge', 2_592_000) || 2_592_000
  end

  # Fetches the link params from the original params used for social badges
  #
  # @return [String] Returns the link param otherwise empty string
  def link_param
    @link_param ||= original_params.fetch('link', '') || ''
  end

  # Checks if the badge is a social badge and if the params contains links and returns the links for the badge
  #
  # @return [String] Returns the links used for social badges
  def style_additionals
    return if style_param != 'social' || link_param.blank?
    "&link=#{link_param[0]}&link=#{link_param[1]}"
  end

  # Fetches the logo from the params, otherwise empty string
  #
  # @return [String] Returns the logo from the params, otherwise empty string
  def logo_param
    @logo_param ||= @params.fetch('logo', '') || ''
  end

  # Fetches the logo width from the params, otherwise empty string
  #
  # @return [String] Returns the logo width from the params, otherwise empty string
  def logo_width
    @logo_width ||= @params.fetch('logoWidth', 0).to_s.to_i || 0
  end

  # Fetches the logo padding from the params, otherwise empty string
  #
  # @return [String] Returns the logo padding from the params, otherwise empty string
  def logo_padding
    @logo_padding ||= @params.fetch('logoPadding', 0).to_s.to_i || 0
  end

  # Checks if any additional params are present in URL and adds them to the URL constructed for the badge
  #
  # @return [String] Returns the URL query string used for displaying the badge
  def additional_params
    additionals = {
      'logo': logo_param,
      'logoWidth': logo_width,
      'logoPadding': logo_padding,
      'style': style_param,
      'maxAge': max_age_param.to_i
    }.delete_if { |_key, value| value.blank? || (value.is_a?(Numeric) && value.zero?) }
    additionals = additionals.to_query
    "#{additionals}#{style_additionals}"
  end

  # Method that is used to fetch the status of the badge
  #
  # @return [String] Returns the status of the badge
  def status_param
    @status_param ||= begin
      status_param = @params.fetch('label', 'downloads') || 'downloads'
      status_param = status_param.present? ? status_param : 'downloads'
      clean_image_label(status_param)
    end
  end

  # Method that is used to set the image extension
  #
  # @return [String] Returns the status of the badge
  def image_extension
    @image_extension ||= begin
      param_extension = @params['extension'].to_s
      available_extension?(param_extension) ? param_extension : 'svg'
    end
  end

  # Method that is used to determine the image color, by default blue.
  # In case the downloads are blank , will return lightgrey
  #
  # @return [String] Returns the color of the badge (Default: blue)
  def image_colour
    @image_colour ||= @downloads.blank? ? 'lightgrey' : @params.fetch('color', 'blue')
  end

  # Method used to build the shield URL for fetching the SVG image
  # @see #format_number_of_downloads
  # @return [String] The URL that will be used in fetching the SVG image from shields.io server
  def build_badge_url(extension = image_extension)
    "#{BadgeApi::BASE_URL}/badge/#{status_param}-#{format_number_of_downloads}-#{image_colour}.#{extension}?#{additional_params}"
  end

  # Method that is used for building the URL for fetching the SVG Image, and actually
  # making the HTTP connection and adding the response to the stream
  # @see #build_badge_url
  # @see Helper#fetch_data
  # @see Helper#print_to_output_buffer
  #
  # @return [void]
  def fetch_image_shield
    fetch_data(build_badge_url, @default_options) do |http_response|
      print_to_output_buffer(http_response, @output_buffer)
    end
  end

  # callback that is called when http request fails
  # def callback_error(error, options)
  #  super(error, options)
  #  output = svg_template.fetch_badge_image
  #  print_to_output_buffer(output, @output_buffer)
  # end

  # Method that is used for formatting the number of downloads , if the number is blank, will return invalid,
  # otherwise will format the number using the configuration from params, either using metrics or delimiters
  # @see  NumberFormatter#initialize
  # @see NumberFormatter#formatted_display
  #
  # @return [String] If the downloads argument is blank will return invalid, otherwise will format the numbere either with metrics or delimiters
  def format_number_of_downloads
    @format_number_of_downloads ||= (@downloads.blank? ? BadgeApi::INVALID_COUNT : NumberFormatter.new(@downloads, @params).to_s)
  end
end

Instance Method Details

#additional_paramsString

Checks if any additional params are present in URL and adds them to the URL constructed for the badge

Returns:

  • (String)

    Returns the URL query string used for displaying the badge


128
129
130
131
132
133
134
135
136
137
138
# File 'lib/badge_api.rb', line 128

def additional_params
  additionals = {
    'logo': logo_param,
    'logoWidth': logo_width,
    'logoPadding': logo_padding,
    'style': style_param,
    'maxAge': max_age_param.to_i
  }.delete_if { |_key, value| value.blank? || (value.is_a?(Numeric) && value.zero?) }
  additionals = additionals.to_query
  "#{additionals}#{style_additionals}"
end

#build_badge_url(extension = image_extension) ⇒ String

Method used to build the shield URL for fetching the SVG image

Returns:

  • (String)

    The URL that will be used in fetching the SVG image from shields.io server

See Also:


172
173
174
# File 'lib/badge_api.rb', line 172

def build_badge_url(extension = image_extension)
  "#{BadgeApi::BASE_URL}/badge/#{status_param}-#{format_number_of_downloads}-#{image_colour}.#{extension}?#{additional_params}"
end

#fetch_image_shieldvoid

This method returns an undefined value.

Method that is used for building the URL for fetching the SVG Image, and actually making the HTTP connection and adding the response to the stream

See Also:


183
184
185
186
187
# File 'lib/badge_api.rb', line 183

def fetch_image_shield
  fetch_data(build_badge_url, @default_options) do |http_response|
    print_to_output_buffer(http_response, @output_buffer)
  end
end

#format_number_of_downloadsString

Method that is used for formatting the number of downloads , if the number is blank, will return invalid, otherwise will format the number using the configuration from params, either using metrics or delimiters

Returns:

  • (String)

    If the downloads argument is blank will return invalid, otherwise will format the numbere either with metrics or delimiters

See Also:


202
203
204
# File 'lib/badge_api.rb', line 202

def format_number_of_downloads
  @format_number_of_downloads ||= (@downloads.blank? ? BadgeApi::INVALID_COUNT : NumberFormatter.new(@downloads, @params).to_s)
end

#image_colourString

Method that is used to determine the image color, by default blue. In case the downloads are blank , will return lightgrey

Returns:

  • (String)

    Returns the color of the badge (Default: blue)


165
166
167
# File 'lib/badge_api.rb', line 165

def image_colour
  @image_colour ||= @downloads.blank? ? 'lightgrey' : @params.fetch('color', 'blue')
end

#image_extensionString

Method that is used to set the image extension

Returns:

  • (String)

    Returns the status of the badge


154
155
156
157
158
159
# File 'lib/badge_api.rb', line 154

def image_extension
  @image_extension ||= begin
    param_extension = @params['extension'].to_s
    available_extension?(param_extension) ? param_extension : 'svg'
  end
end

Fetches the link params from the original params used for social badges

Returns:

  • (String)

    Returns the link param otherwise empty string


92
93
94
# File 'lib/badge_api.rb', line 92

def link_param
  @link_param ||= original_params.fetch('link', '') || ''
end

#logo_paddingString

Fetches the logo padding from the params, otherwise empty string

Returns:

  • (String)

    Returns the logo padding from the params, otherwise empty string


121
122
123
# File 'lib/badge_api.rb', line 121

def logo_padding
  @logo_padding ||= @params.fetch('logoPadding', 0).to_s.to_i || 0
end

#logo_paramString

Fetches the logo from the params, otherwise empty string

Returns:

  • (String)

    Returns the logo from the params, otherwise empty string


107
108
109
# File 'lib/badge_api.rb', line 107

def logo_param
  @logo_param ||= @params.fetch('logo', '') || ''
end

#logo_widthString

Fetches the logo width from the params, otherwise empty string

Returns:

  • (String)

    Returns the logo width from the params, otherwise empty string


114
115
116
# File 'lib/badge_api.rb', line 114

def logo_width
  @logo_width ||= @params.fetch('logoWidth', 0).to_s.to_i || 0
end

#max_age_paramInteger

Fetches the maxAge from the params , and if is not present will return by default 2592000

Returns:

  • (Integer)

    Returns the maxAge from params , otherwise will return by default 2592000


85
86
87
# File 'lib/badge_api.rb', line 85

def max_age_param
  @max_age_param ||= @params.fetch('maxAge', 2_592_000) || 2_592_000
end

#original_paramsHash

Parses the query string from the request with CGI in order to provide a integration with social badges

Returns:

  • (Hash)

    the parsed query string in Hash format but making array params as arrays instead of hashes

See Also:

  • Rack::Request#query_string
  • CGI::parse

71
72
73
# File 'lib/badge_api.rb', line 71

def original_params
  @original_params ||= CGI::parse(@request.query_string)
end

#status_paramString

Method that is used to fetch the status of the badge

Returns:

  • (String)

    Returns the status of the badge


143
144
145
146
147
148
149
# File 'lib/badge_api.rb', line 143

def status_param
  @status_param ||= begin
    status_param = @params.fetch('label', 'downloads') || 'downloads'
    status_param = status_param.present? ? status_param : 'downloads'
    clean_image_label(status_param)
  end
end

#style_additionalsString

Checks if the badge is a social badge and if the params contains links and returns the links for the badge

Returns:

  • (String)

    Returns the links used for social badges


99
100
101
102
# File 'lib/badge_api.rb', line 99

def style_additionals
  return if style_param != 'social' || link_param.blank?
  "&link=#{link_param[0]}&link=#{link_param[1]}"
end

#style_paramString

Fetches the param style from the params , and if is not present will return by default 'flat'

Returns:

  • (String)

    Returns the param style from params , otherwise will return by default 'flat'


78
79
80
# File 'lib/badge_api.rb', line 78

def style_param
  @style_param ||= @params.fetch('style', 'flat') || 'flat'
end