Class: RubygemsDownloadShieldsApp

Inherits:
Sinatra::Base
  • Object
show all
Includes:
Helper
Defined in:
lib/web.rb

Overview

class that is used to download shields for ruby gems using their name and version

Author:

  • Rada Bogdan Raul

Constant Summary collapse

CACHE_CONTROL_FLAGS =
[:no_cache,:no_store, :must_revalidate, max_age: 0]

Class Method Summary collapse

Instance Method Summary collapse

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?

Class Method Details

This method returns an undefined value.

It constructs the cookie data as a Hash from the cookie string that belongs to a particular URL

Parameters:

  • url (String)

58
59
60
61
62
63
# File 'lib/web.rb', line 58

def self.cookie_hash(url)
  db = settings.cookie_db
  CookieHash.new.tap do |cookie_hash|
    cookie_hash.add_cookies(db[url]) if db.key?(url)
  end
end

.set_time_zonevoid

This method returns an undefined value.

It sets the Time zone so that using Time.zone will give the time in that timezone


68
69
70
71
# File 'lib/web.rb', line 68

def self.set_time_zone
  Time.zone = 'UTC'
  ENV['TZ'] = 'UTC'
end

Instance Method Details

#badge_callback(out, additional_params = {}) ⇒ Lambda

Method that fetch the badge

Parameters:

  • out (Sinatra::Stream)

    The stream where the response is added to

  • additional_params (Hash) (defaults to: {})

    The additional params needed for the badge

Returns:

  • (Lambda)

    The lambda that is used as callback to other APIS


113
114
115
116
117
# File 'lib/web.rb', line 113

def badge_callback(out, additional_params = {})
  lambda do |downloads, http_response|
    BadgeApi.new(request, params.merge(additional_params), out, downloads, http_response)
  end
end

#em_request_badge(&block) ⇒ void

This method returns an undefined value.

Method that fetch the badge

Parameters:

  • block (Block)

    The block that is executed after Eventmachine starts


123
124
125
126
127
128
# File 'lib/web.rb', line 123

def em_request_badge(&block)
  use_stream do |out|
    register_em_error_handler
    run_eventmachine(out, &block)
  end
end

#register_em_error_handlervoid

This method returns an undefined value.

Method that registers a error handler on Eventmachine


144
145
146
147
148
# File 'lib/web.rb', line 144

def register_em_error_handler
  EM.error_handler do |error|
    settings.logger.debug "Error during event loop : #{format_error(error)}"
  end
end

#run_eventmachine(out) ⇒ Object

Method that runs a block after eventmachine starts. This method also sets the RequestMiddleware to be used for all EM::HttpRequest instances that will be created_at

Parameters:

  • out (Sinatra::Stream)

    The stream where the response will be appended

Yield Returns:

  • (Sinatra::Stream)

    yields the stream if a block is given

See Also:

  • RequestMiddleware

155
156
157
158
159
160
# File 'lib/web.rb', line 155

def run_eventmachine(out)
  EM.run do
    EM::HttpRequest.use RequestMiddleware
    yield out if block_given?
  end
end

#use_streamObject

Method that sets first the content type , then opens a stream and yields the stream if a block is given

Yield Returns:

  • (Sinatra::Stream)

    yields the stream that was opened if a block is given


133
134
135
136
137
138
139
# File 'lib/web.rb', line 133

def use_stream
  content_type_string = fetch_content_type(params[:extension])
  content_type(content_type_string)
  stream :keep_open do |out|
    yield out if block_given?
  end
end