class Decidim::ComparativeStats::ApiFetcher

Class used to fetch and validate Decidim API calls

Attributes

client[W]
endpoint[R]
errors[R]

Public Class Methods

new(endpoint) click to toggle source
# File lib/decidim/comparative_stats/api_fetcher.rb, line 10
def initialize(endpoint)
  @errors = []
  @queries = {}
  @endpoint = endpoint
end

Public Instance Methods

client() click to toggle source
# File lib/decidim/comparative_stats/api_fetcher.rb, line 20
def client
  @client ||= Graphlient::Client.new(endpoint,
                                     http: CachedHTTPAdapter,
                                     http_options: {
                                       read_timeout: 20,
                                       write_timeout: 30
                                     })
end
error() click to toggle source

returns the last error

# File lib/decidim/comparative_stats/api_fetcher.rb, line 57
def error
  @errors.last
end
name_and_version() click to toggle source

When creating name and version are fetched from the api Update action should allow the user to change the name but not the version

# File lib/decidim/comparative_stats/api_fetcher.rb, line 31
def name_and_version
  @name_and_version ||= fetch_name_and_version.data.decidim
end
query(tag) click to toggle source

Queries the GraphQL api using one of files in lib/decidim/comparative_stats/queries

# File lib/decidim/comparative_stats/api_fetcher.rb, line 36
def query(tag)
  @queries[tag] ||= client.query versioned_query(tag)
rescue Faraday::Error
  @errors << "Not a valid Decidim API URL"
rescue Graphlient::Errors::Error => e
  @errors << e.message
end
valid?(min_version = MIN_API_VERSION) click to toggle source

Checks if is a valid Decidim API URL

# File lib/decidim/comparative_stats/api_fetcher.rb, line 45
def valid?(min_version = MIN_API_VERSION)
  response = fetch_name_and_version
  return false unless response

  if Gem::Version.new(min_version) > Gem::Version.new(response.data.decidim.version)
    @errors << "Decidim version detect (#{response.data.decidim.version}) should be at least #{min_version}"
    return false
  end
  true
end

Private Instance Methods

method_missing(name) click to toggle source

Syntactic sugar to query graphql queries: i.e:

query "name_and_version" => fetch_name_and_version
Calls superclass method
# File lib/decidim/comparative_stats/api_fetcher.rb, line 79
def method_missing(name)
  _, key = name.to_s.split("fetch_")
  if key
    query key
  else
    super
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/decidim/comparative_stats/api_fetcher.rb, line 88
def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.start_with?("fetch_") || super
end
versioned_query(tag) click to toggle source
# File lib/decidim/comparative_stats/api_fetcher.rb, line 63
def versioned_query(tag)
  file = File.join(__dir__, "queries", "#{tag}.graphql")
  raise NameError unless File.exist?(file)

  unless tag == "name_and_version"
    if Gem::Version.new(name_and_version.version) < Gem::Version.new("0.23")
      alt_file = File.join(__dir__, "queries", "v022", "#{tag}.graphql")
      file = alt_file if File.exist?(alt_file)
    end
  end
  File.open(file).read
end