Class: RubygemsApi

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

Overview

class used for connecting to runygems.org and downloading info about a gem

Constant Summary collapse

BASE_URL =

the base url to which the API will connect for fetching information about gems

'https://rubygems.org'

Instance Attribute Summary collapse

Attributes inherited from CoreApi

#base_url, #hostname

Instance Method Summary collapse

Methods inherited from CoreApi

#add_cookie_header, #do_fetch_real_data, #em_connection_options, #em_request, #em_request_options, #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, callback) ⇒ void

Method used to instantiate an instance of RubygemsApi class with the params received from URL

Parameters:

  • request (Rack::Request)

    The request that Sinatra received

  • params (Hash)

    The params received from URL

  • callback (Proc)

    The callback that is executed after info is fetched

Options Hash (params):

  • :gem (String)

    The name of the gem

  • :version (String)

    The version of the gem

  • :type (String)

    The type of display , if we want to display total downloads, this will have value 'total'


27
28
29
30
31
32
33
# File 'lib/rubygems_api.rb', line 27

def initialize(request, params, callback)
  @request = request
  @params = params.stringify_keys
  @callback = callback
  @default_options = { 'callback' => @callback }
  fetch_downloads_data
end

Instance Attribute Details

#callbackProc

Returns The callback that is executed after info is fetched.

Returns:

  • (Proc)

    The callback that is executed after info is fetched


12
13
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
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
# File 'lib/rubygems_api.rb', line 12

class RubygemsApi < CoreApi
  # the base url to which the API will connect for fetching information about gems
  BASE_URL = 'https://rubygems.org'

  attr_reader :callback, :params, :request, :default_options

  # Method used to instantiate an instance of RubygemsApi class with the params received from URL
  #
  # @param [Rack::Request] request The request that Sinatra received
  # @param [Hash] params The params received from URL
  # @option params [String] :gem The name of the gem
  # @option params [String]:version The version of the gem
  # @option params [String] :type The type of display , if we want to display total downloads, this will have value 'total'
  # @param [Proc] callback The callback that is executed after info is fetched
  # @return [void]
  def initialize(request, params, callback)
    @request = request
    @params = params.stringify_keys
    @callback = callback
    @default_options = { 'callback' => @callback }
    fetch_downloads_data
  end

  # Method that checks if the call to rubygems.org needs to be authorized
  # and adds the authorization header if is needed
  # @return [Hash] The additional headers needed for request to rubygems.org
  #   * :Authorization [String] The authorization key that holds the API KEY for the request
  def fetch_additional_headers
    api_key = params.fetch('api_key', '')
    { 'Authorization' => api_key }
  end

  # Method that checks if the gem is valid , and if it is will fetch the infromation about the gem
  # and pass the callback to the method . If is not valid the callback will be called with nil value
  # @see #valid?
  # @see #fetch_dowloads_info
  #
  # @return [void]
  def fetch_downloads_data
    if valid?
      fetch_dowloads_info
    else
      @callback.call(nil, nil)
    end
  end

  # This method will decide what API method need to be called, depending if we want the latest stable version,
  # a specific version or the latest one and passs the callback to the method call
  # @see #fetch_gem_data_without_version
  # @see #gem_stable_version?
  # @see #fetch_specific_version_data
  # @see #fetch_gem_stable_version_data
  #
  # @return [void]
  def fetch_dowloads_info
    if gem_version.blank?
      fetch_gem_data_without_version
    elsif !gem_stable_version?
      fetch_specific_version_data
    elsif gem_stable_version?
      fetch_gem_stable_version_data
    end
  end

  # Method that is used to determine if the gem is valid by checking his name and version
  # THe name is required and the version need to checked if is stable or sintactically valid
  # @see  #gem_with_version?
  #
  # @return [Boolean] Returns true if the gem is valid
  def valid?
    gem_name.present? || gem_with_version?
  end

  # Returns the gem name from the params , otherwise nil
  #
  # @return [String, nil] Returns the gem name from the params , otherwise nil
  def gem_name
    @params.fetch('gem', nil)
  end

  # Returns the gem version from the params , otherwise nil
  #
  # @return [String, nil] Returns the gem version from the params , otherwise nil
  def gem_version
    @params.fetch('version', nil)
  end

  # Method that checks if the version is 'stable'
  #
  # @return [Boolean] Returns true if the version is 'stable'
  def gem_stable_version?
    gem_version.present? && gem_version == 'stable'
  end

  # Method that checks if the version of the gem is sintactically valid
  #
  # @return [Boolean] returns true if the version of the gem is sintactically valid
  def gem_valid_version?
    gem_version.present? && parse_gem_version(gem_version).present?
  end

  # Method that check if the gem name and gem version are present and the gem version is either 'stable'
  # or sintactically valid
  # @see #gem_stable_version?
  # @see #gem_valid_version?
  #
  # @return [Boolean] returns true if the name and version of gem are present and valid sintactically
  def gem_with_version?
    gem_name.present? && gem_version.present? && (gem_stable_version? || gem_valid_version?)
  end

  # Method that downloads all the versions of a gem, finds the latest stable version and sends the downloads count to the callback
  # The count defers depending if we need to display total amount or not
  # @see #get_latest_stable_version_details
  # @see #latest_stable_version_details
  #
  # @return [void]
  def fetch_gem_stable_version_data
    fetch_data("#{RubygemsApi::BASE_URL}/api/v1/versions/#{gem_name}.json", @default_options) do |http_response|
      latest_stable_version_details = get_latest_stable_version_details(http_response)
      downloads_count = latest_stable_version_details['downloads_count'] unless latest_stable_version_details.blank?
      @callback.call(downloads_count, http_response)
    end
  end

  # Method that downloads information about a specifc version of a gem and send the count to the callback
  # The count defers depending if we need to display total amount or not
  # @see #display_total
  #
  # @return [void]
  def fetch_specific_version_data
    fetch_data("#{RubygemsApi::BASE_URL}/api/v1/downloads/#{gem_name}-#{gem_version}.json", @default_options) do |http_response|
      downloads_count = http_response['version_downloads']
      downloads_count = http_response['total_downloads'] if display_total
      @callback.call(downloads_count, http_response)
    end
  end

  # Method that downloads information about the latest version and sends the count to the callback
  # The count defers depending if we need to display total amount or not
  # @see #display_total
  #
  # @return [void]
  def fetch_gem_data_without_version
    fetch_data("#{RubygemsApi::BASE_URL}/api/v1/gems/#{gem_name}.json", @default_options) do |http_response|
      downloads_count = http_response['version_downloads']
      downloads_count = http_response['downloads'] if display_total
      @callback.call(downloads_count, http_response)
    end
  end

  # Method that is used when a HTTP error happens for this service,
  # and this also makes sure that the success callback is called with nil values,
  # so further processing can happen
  # @see CoreApi#callback_error
  #
  # @return [void]
  def callback_error(error, options = {})
    super(error, options)
    @callback.call(nil, nil)
  end

  # Method that is executed after we receive an successful response.
  # This method willt try and parse the response as JSON, and if the
  # parsing fails will return  nil
  #
  # @param [String] response The response received after successful HTTP request
  # @return [Hash, nil] Returns the response parsed to JSON, and if the parsing fails returns nil
  def callback_before_success(response)
    parse_json(response)
  end
end

#default_optionsObject (readonly)

Returns the value of attribute default_options


16
17
18
# File 'lib/rubygems_api.rb', line 16

def default_options
  @default_options
end

#paramsHash

Returns The params received by Sinatra.

Returns:

  • (Hash)

    The params received by Sinatra


12
13
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
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
# File 'lib/rubygems_api.rb', line 12

class RubygemsApi < CoreApi
  # the base url to which the API will connect for fetching information about gems
  BASE_URL = 'https://rubygems.org'

  attr_reader :callback, :params, :request, :default_options

  # Method used to instantiate an instance of RubygemsApi class with the params received from URL
  #
  # @param [Rack::Request] request The request that Sinatra received
  # @param [Hash] params The params received from URL
  # @option params [String] :gem The name of the gem
  # @option params [String]:version The version of the gem
  # @option params [String] :type The type of display , if we want to display total downloads, this will have value 'total'
  # @param [Proc] callback The callback that is executed after info is fetched
  # @return [void]
  def initialize(request, params, callback)
    @request = request
    @params = params.stringify_keys
    @callback = callback
    @default_options = { 'callback' => @callback }
    fetch_downloads_data
  end

  # Method that checks if the call to rubygems.org needs to be authorized
  # and adds the authorization header if is needed
  # @return [Hash] The additional headers needed for request to rubygems.org
  #   * :Authorization [String] The authorization key that holds the API KEY for the request
  def fetch_additional_headers
    api_key = params.fetch('api_key', '')
    { 'Authorization' => api_key }
  end

  # Method that checks if the gem is valid , and if it is will fetch the infromation about the gem
  # and pass the callback to the method . If is not valid the callback will be called with nil value
  # @see #valid?
  # @see #fetch_dowloads_info
  #
  # @return [void]
  def fetch_downloads_data
    if valid?
      fetch_dowloads_info
    else
      @callback.call(nil, nil)
    end
  end

  # This method will decide what API method need to be called, depending if we want the latest stable version,
  # a specific version or the latest one and passs the callback to the method call
  # @see #fetch_gem_data_without_version
  # @see #gem_stable_version?
  # @see #fetch_specific_version_data
  # @see #fetch_gem_stable_version_data
  #
  # @return [void]
  def fetch_dowloads_info
    if gem_version.blank?
      fetch_gem_data_without_version
    elsif !gem_stable_version?
      fetch_specific_version_data
    elsif gem_stable_version?
      fetch_gem_stable_version_data
    end
  end

  # Method that is used to determine if the gem is valid by checking his name and version
  # THe name is required and the version need to checked if is stable or sintactically valid
  # @see  #gem_with_version?
  #
  # @return [Boolean] Returns true if the gem is valid
  def valid?
    gem_name.present? || gem_with_version?
  end

  # Returns the gem name from the params , otherwise nil
  #
  # @return [String, nil] Returns the gem name from the params , otherwise nil
  def gem_name
    @params.fetch('gem', nil)
  end

  # Returns the gem version from the params , otherwise nil
  #
  # @return [String, nil] Returns the gem version from the params , otherwise nil
  def gem_version
    @params.fetch('version', nil)
  end

  # Method that checks if the version is 'stable'
  #
  # @return [Boolean] Returns true if the version is 'stable'
  def gem_stable_version?
    gem_version.present? && gem_version == 'stable'
  end

  # Method that checks if the version of the gem is sintactically valid
  #
  # @return [Boolean] returns true if the version of the gem is sintactically valid
  def gem_valid_version?
    gem_version.present? && parse_gem_version(gem_version).present?
  end

  # Method that check if the gem name and gem version are present and the gem version is either 'stable'
  # or sintactically valid
  # @see #gem_stable_version?
  # @see #gem_valid_version?
  #
  # @return [Boolean] returns true if the name and version of gem are present and valid sintactically
  def gem_with_version?
    gem_name.present? && gem_version.present? && (gem_stable_version? || gem_valid_version?)
  end

  # Method that downloads all the versions of a gem, finds the latest stable version and sends the downloads count to the callback
  # The count defers depending if we need to display total amount or not
  # @see #get_latest_stable_version_details
  # @see #latest_stable_version_details
  #
  # @return [void]
  def fetch_gem_stable_version_data
    fetch_data("#{RubygemsApi::BASE_URL}/api/v1/versions/#{gem_name}.json", @default_options) do |http_response|
      latest_stable_version_details = get_latest_stable_version_details(http_response)
      downloads_count = latest_stable_version_details['downloads_count'] unless latest_stable_version_details.blank?
      @callback.call(downloads_count, http_response)
    end
  end

  # Method that downloads information about a specifc version of a gem and send the count to the callback
  # The count defers depending if we need to display total amount or not
  # @see #display_total
  #
  # @return [void]
  def fetch_specific_version_data
    fetch_data("#{RubygemsApi::BASE_URL}/api/v1/downloads/#{gem_name}-#{gem_version}.json", @default_options) do |http_response|
      downloads_count = http_response['version_downloads']
      downloads_count = http_response['total_downloads'] if display_total
      @callback.call(downloads_count, http_response)
    end
  end

  # Method that downloads information about the latest version and sends the count to the callback
  # The count defers depending if we need to display total amount or not
  # @see #display_total
  #
  # @return [void]
  def fetch_gem_data_without_version
    fetch_data("#{RubygemsApi::BASE_URL}/api/v1/gems/#{gem_name}.json", @default_options) do |http_response|
      downloads_count = http_response['version_downloads']
      downloads_count = http_response['downloads'] if display_total
      @callback.call(downloads_count, http_response)
    end
  end

  # Method that is used when a HTTP error happens for this service,
  # and this also makes sure that the success callback is called with nil values,
  # so further processing can happen
  # @see CoreApi#callback_error
  #
  # @return [void]
  def callback_error(error, options = {})
    super(error, options)
    @callback.call(nil, nil)
  end

  # Method that is executed after we receive an successful response.
  # This method willt try and parse the response as JSON, and if the
  # parsing fails will return  nil
  #
  # @param [String] response The response received after successful HTTP request
  # @return [Hash, nil] Returns the response parsed to JSON, and if the parsing fails returns nil
  def callback_before_success(response)
    parse_json(response)
  end
end

#requestRack::Request

Returns The request that Sinatra received.

Returns:

  • (Rack::Request)

    The request that Sinatra received


12
13
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
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
# File 'lib/rubygems_api.rb', line 12

class RubygemsApi < CoreApi
  # the base url to which the API will connect for fetching information about gems
  BASE_URL = 'https://rubygems.org'

  attr_reader :callback, :params, :request, :default_options

  # Method used to instantiate an instance of RubygemsApi class with the params received from URL
  #
  # @param [Rack::Request] request The request that Sinatra received
  # @param [Hash] params The params received from URL
  # @option params [String] :gem The name of the gem
  # @option params [String]:version The version of the gem
  # @option params [String] :type The type of display , if we want to display total downloads, this will have value 'total'
  # @param [Proc] callback The callback that is executed after info is fetched
  # @return [void]
  def initialize(request, params, callback)
    @request = request
    @params = params.stringify_keys
    @callback = callback
    @default_options = { 'callback' => @callback }
    fetch_downloads_data
  end

  # Method that checks if the call to rubygems.org needs to be authorized
  # and adds the authorization header if is needed
  # @return [Hash] The additional headers needed for request to rubygems.org
  #   * :Authorization [String] The authorization key that holds the API KEY for the request
  def fetch_additional_headers
    api_key = params.fetch('api_key', '')
    { 'Authorization' => api_key }
  end

  # Method that checks if the gem is valid , and if it is will fetch the infromation about the gem
  # and pass the callback to the method . If is not valid the callback will be called with nil value
  # @see #valid?
  # @see #fetch_dowloads_info
  #
  # @return [void]
  def fetch_downloads_data
    if valid?
      fetch_dowloads_info
    else
      @callback.call(nil, nil)
    end
  end

  # This method will decide what API method need to be called, depending if we want the latest stable version,
  # a specific version or the latest one and passs the callback to the method call
  # @see #fetch_gem_data_without_version
  # @see #gem_stable_version?
  # @see #fetch_specific_version_data
  # @see #fetch_gem_stable_version_data
  #
  # @return [void]
  def fetch_dowloads_info
    if gem_version.blank?
      fetch_gem_data_without_version
    elsif !gem_stable_version?
      fetch_specific_version_data
    elsif gem_stable_version?
      fetch_gem_stable_version_data
    end
  end

  # Method that is used to determine if the gem is valid by checking his name and version
  # THe name is required and the version need to checked if is stable or sintactically valid
  # @see  #gem_with_version?
  #
  # @return [Boolean] Returns true if the gem is valid
  def valid?
    gem_name.present? || gem_with_version?
  end

  # Returns the gem name from the params , otherwise nil
  #
  # @return [String, nil] Returns the gem name from the params , otherwise nil
  def gem_name
    @params.fetch('gem', nil)
  end

  # Returns the gem version from the params , otherwise nil
  #
  # @return [String, nil] Returns the gem version from the params , otherwise nil
  def gem_version
    @params.fetch('version', nil)
  end

  # Method that checks if the version is 'stable'
  #
  # @return [Boolean] Returns true if the version is 'stable'
  def gem_stable_version?
    gem_version.present? && gem_version == 'stable'
  end

  # Method that checks if the version of the gem is sintactically valid
  #
  # @return [Boolean] returns true if the version of the gem is sintactically valid
  def gem_valid_version?
    gem_version.present? && parse_gem_version(gem_version).present?
  end

  # Method that check if the gem name and gem version are present and the gem version is either 'stable'
  # or sintactically valid
  # @see #gem_stable_version?
  # @see #gem_valid_version?
  #
  # @return [Boolean] returns true if the name and version of gem are present and valid sintactically
  def gem_with_version?
    gem_name.present? && gem_version.present? && (gem_stable_version? || gem_valid_version?)
  end

  # Method that downloads all the versions of a gem, finds the latest stable version and sends the downloads count to the callback
  # The count defers depending if we need to display total amount or not
  # @see #get_latest_stable_version_details
  # @see #latest_stable_version_details
  #
  # @return [void]
  def fetch_gem_stable_version_data
    fetch_data("#{RubygemsApi::BASE_URL}/api/v1/versions/#{gem_name}.json", @default_options) do |http_response|
      latest_stable_version_details = get_latest_stable_version_details(http_response)
      downloads_count = latest_stable_version_details['downloads_count'] unless latest_stable_version_details.blank?
      @callback.call(downloads_count, http_response)
    end
  end

  # Method that downloads information about a specifc version of a gem and send the count to the callback
  # The count defers depending if we need to display total amount or not
  # @see #display_total
  #
  # @return [void]
  def fetch_specific_version_data
    fetch_data("#{RubygemsApi::BASE_URL}/api/v1/downloads/#{gem_name}-#{gem_version}.json", @default_options) do |http_response|
      downloads_count = http_response['version_downloads']
      downloads_count = http_response['total_downloads'] if display_total
      @callback.call(downloads_count, http_response)
    end
  end

  # Method that downloads information about the latest version and sends the count to the callback
  # The count defers depending if we need to display total amount or not
  # @see #display_total
  #
  # @return [void]
  def fetch_gem_data_without_version
    fetch_data("#{RubygemsApi::BASE_URL}/api/v1/gems/#{gem_name}.json", @default_options) do |http_response|
      downloads_count = http_response['version_downloads']
      downloads_count = http_response['downloads'] if display_total
      @callback.call(downloads_count, http_response)
    end
  end

  # Method that is used when a HTTP error happens for this service,
  # and this also makes sure that the success callback is called with nil values,
  # so further processing can happen
  # @see CoreApi#callback_error
  #
  # @return [void]
  def callback_error(error, options = {})
    super(error, options)
    @callback.call(nil, nil)
  end

  # Method that is executed after we receive an successful response.
  # This method willt try and parse the response as JSON, and if the
  # parsing fails will return  nil
  #
  # @param [String] response The response received after successful HTTP request
  # @return [Hash, nil] Returns the response parsed to JSON, and if the parsing fails returns nil
  def callback_before_success(response)
    parse_json(response)
  end
end

Instance Method Details

#callback_before_success(response) ⇒ Hash?

Method that is executed after we receive an successful response. This method willt try and parse the response as JSON, and if the parsing fails will return nil

Parameters:

  • response (String)

    The response received after successful HTTP request

Returns:

  • (Hash, nil)

    Returns the response parsed to JSON, and if the parsing fails returns nil


180
181
182
# File 'lib/rubygems_api.rb', line 180

def callback_before_success(response)
  parse_json(response)
end

#callback_error(error, options = {}) ⇒ void

This method returns an undefined value.

Method that is used when a HTTP error happens for this service, and this also makes sure that the success callback is called with nil values, so further processing can happen


169
170
171
172
# File 'lib/rubygems_api.rb', line 169

def callback_error(error, options = {})
  super(error, options)
  @callback.call(nil, nil)
end

#fetch_additional_headersHash

Method that checks if the call to rubygems.org needs to be authorized and adds the authorization header if is needed

Returns:

  • (Hash)

    The additional headers needed for request to rubygems.org

    • :Authorization [String] The authorization key that holds the API KEY for the request


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

def fetch_additional_headers
  api_key = params.fetch('api_key', '')
  { 'Authorization' => api_key }
end

#fetch_dowloads_infovoid

This method returns an undefined value.

This method will decide what API method need to be called, depending if we want the latest stable version, a specific version or the latest one and passs the callback to the method call


66
67
68
69
70
71
72
73
74
# File 'lib/rubygems_api.rb', line 66

def fetch_dowloads_info
  if gem_version.blank?
    fetch_gem_data_without_version
  elsif !gem_stable_version?
    fetch_specific_version_data
  elsif gem_stable_version?
    fetch_gem_stable_version_data
  end
end

#fetch_downloads_datavoid

This method returns an undefined value.

Method that checks if the gem is valid , and if it is will fetch the infromation about the gem and pass the callback to the method . If is not valid the callback will be called with nil value


50
51
52
53
54
55
56
# File 'lib/rubygems_api.rb', line 50

def fetch_downloads_data
  if valid?
    fetch_dowloads_info
  else
    @callback.call(nil, nil)
  end
end

#fetch_gem_data_without_versionvoid

This method returns an undefined value.

Method that downloads information about the latest version and sends the count to the callback The count defers depending if we need to display total amount or not

See Also:

  • Helper#display_total

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

def fetch_gem_data_without_version
  fetch_data("#{RubygemsApi::BASE_URL}/api/v1/gems/#{gem_name}.json", @default_options) do |http_response|
    downloads_count = http_response['version_downloads']
    downloads_count = http_response['downloads'] if display_total
    @callback.call(downloads_count, http_response)
  end
end

#fetch_gem_stable_version_datavoid

This method returns an undefined value.

Method that downloads all the versions of a gem, finds the latest stable version and sends the downloads count to the callback The count defers depending if we need to display total amount or not

See Also:

  • Helper#get_latest_stable_version_details
  • #latest_stable_version_details

129
130
131
132
133
134
135
# File 'lib/rubygems_api.rb', line 129

def fetch_gem_stable_version_data
  fetch_data("#{RubygemsApi::BASE_URL}/api/v1/versions/#{gem_name}.json", @default_options) do |http_response|
    latest_stable_version_details = get_latest_stable_version_details(http_response)
    downloads_count = latest_stable_version_details['downloads_count'] unless latest_stable_version_details.blank?
    @callback.call(downloads_count, http_response)
  end
end

#fetch_specific_version_datavoid

This method returns an undefined value.

Method that downloads information about a specifc version of a gem and send the count to the callback The count defers depending if we need to display total amount or not

See Also:

  • Helper#display_total

142
143
144
145
146
147
148
# File 'lib/rubygems_api.rb', line 142

def fetch_specific_version_data
  fetch_data("#{RubygemsApi::BASE_URL}/api/v1/downloads/#{gem_name}-#{gem_version}.json", @default_options) do |http_response|
    downloads_count = http_response['version_downloads']
    downloads_count = http_response['total_downloads'] if display_total
    @callback.call(downloads_count, http_response)
  end
end

#gem_nameString?

Returns the gem name from the params , otherwise nil

Returns:

  • (String, nil)

    Returns the gem name from the params , otherwise nil


88
89
90
# File 'lib/rubygems_api.rb', line 88

def gem_name
  @params.fetch('gem', nil)
end

#gem_stable_version?Boolean

Method that checks if the version is 'stable'

Returns:

  • (Boolean)

    Returns true if the version is 'stable'


102
103
104
# File 'lib/rubygems_api.rb', line 102

def gem_stable_version?
  gem_version.present? && gem_version == 'stable'
end

#gem_valid_version?Boolean

Method that checks if the version of the gem is sintactically valid

Returns:

  • (Boolean)

    returns true if the version of the gem is sintactically valid


109
110
111
# File 'lib/rubygems_api.rb', line 109

def gem_valid_version?
  gem_version.present? && parse_gem_version(gem_version).present?
end

#gem_versionString?

Returns the gem version from the params , otherwise nil

Returns:

  • (String, nil)

    Returns the gem version from the params , otherwise nil


95
96
97
# File 'lib/rubygems_api.rb', line 95

def gem_version
  @params.fetch('version', nil)
end

#gem_with_version?Boolean

Method that check if the gem name and gem version are present and the gem version is either 'stable' or sintactically valid

Returns:

  • (Boolean)

    returns true if the name and version of gem are present and valid sintactically

See Also:


119
120
121
# File 'lib/rubygems_api.rb', line 119

def gem_with_version?
  gem_name.present? && gem_version.present? && (gem_stable_version? || gem_valid_version?)
end

#valid?Boolean

Method that is used to determine if the gem is valid by checking his name and version THe name is required and the version need to checked if is stable or sintactically valid

Returns:

  • (Boolean)

    Returns true if the gem is valid

See Also:


81
82
83
# File 'lib/rubygems_api.rb', line 81

def valid?
  gem_name.present? || gem_with_version?
end