Sha256: 503ade18d28554351e0750209882026f0f1cae5525662ee07ea024a316e88942

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

module Svelte
  # Dynamically generates a client to consume a Swagger API
  class Service
    class << self
      # Generate a Service via URL or JSON.
      # @param url [String] full URL of the Swagger API spec
      # @param json [String] full Swagger API spec as a String
      # @param module_name [String] constant name where Svelte will
      #   build the functionality on top of
      # @return [Module] A newly created `Module` with the
      #   module hierarchy and methods to consume the Swagger API
      #   The new module will be built on top of `Svelte::Service` and will
      #   have `module_name` as its constant name, therefore it can also be
      #   accessed via `Svelte::Service::<module_name>`. For example, if
      #   `module_name` is `Comments`, the new module will be built in
      #   `Svelte::Service::Comments`
      # @note Either `url` or `json` need to be provided. `url` will take
      #   precedence over `json`
      def create(url: nil, json: nil, module_name:, options: {})
        json = get_json(url: url) if url
        SwaggerBuilder.new(raw_hash: JSON.parse(json.to_s),
                           module_name: module_name,
                           options: options).make_resource
      end

      private

      def get_json(url:)
        Faraday.get(url).body
      rescue Faraday::ClientError => e
        raise HTTPError.new(
          message: "Could not get API json from #{url}",
          parent: e
        )
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
svelte-0.1.5 lib/svelte/service.rb
svelte-0.1.4 lib/svelte/service.rb
svelte-0.1.3 lib/svelte/service.rb