# encoding: utf-8
# frozen_string_literal: true

#
# WARNING: Do not edit by hand, this file was generated by Heroics:
#
#   https://github.com/interagent/heroics
#

require 'heroics'
require 'uri'

module ExampleClient
  # Get a Client configured to use HTTP Basic or header-based authentication.
  #
  # @param api_key [String] The API key to use when connecting.
  # @param options [Hash<Symbol,String>] Optionally, custom settings
  #   to use with the client.  Allowed options are `default_headers`,
  #   `cache`, `user` and `url`.
  # @return [Client] A client configured to use the API with HTTP Basic
  #   or header-based authentication.
  def self.connect(api_key, options=nil)
    options = custom_options(options)
    uri = URI.parse(options[:url])

    if options[:user]
      uri.user = URI.encode_www_form_component options[:user]
    end

    if api_key
      uri.user ||= 'user'
      uri.password = api_key
    end

    client = Heroics.client_from_schema(SCHEMA, uri.to_s, options)
    Client.new(client)
  end

  # Get a Client configured to use OAuth authentication.
  #
  # @param oauth_token [String] The OAuth token to use with the API.
  # @param options [Hash<Symbol,String>] Optionally, custom settings
  #   to use with the client.  Allowed options are `default_headers`,
  #   `cache` and `url`.
  # @return [Client] A client configured to use the API with OAuth
  #   authentication.
  def self.connect_oauth(oauth_token, options=nil)
    options = custom_options(options)
    url = options[:url]
    client = Heroics.oauth_client_from_schema(oauth_token, SCHEMA, url, options)
    Client.new(client)
  end

  # Get a Client configured to use Token authentication.
  #
  # @param token [String] The token to use with the API.
  # @param options [Hash<Symbol,String>] Optionally, custom settings
  #   to use with the client.  Allowed options are `default_headers`,
  #   `cache` and `url`.
  # @return [Client] A client configured to use the API with OAuth
  #   authentication.
  def self.connect_token(token, options=nil)
    options = custom_options(options)
    url = options[:url]
    client = Heroics.token_client_from_schema(token, SCHEMA, url, options)
    Client.new(client)
  end

  # Get customized options.
  def self.custom_options(options)
    return default_options if options.nil?

    final_options = default_options
    if options[:default_headers]
      final_options[:default_headers].merge!(options[:default_headers])
    end
    final_options[:cache] = options[:cache] || Moneta.new(:File, dir: "/Users/mgauger/.heroics/example")
    final_options[:url] = options[:url] if options[:url]
    final_options[:user] = options[:user] if options[:user]
    final_options
  end

  # Get the default options.
  def self.default_options
    default_headers = {"Accept"=>"application/vnd.example+json; version=1"}
    {
      default_headers: default_headers,
      url:             "https://example.com"
    }
  end

  private_class_method :default_options, :custom_options

  # Sample schema for use in tests.
  class Client
    def initialize(client)
      @client = client
    end

    # A sample resource to use in tests.
    #
    # @return [Resource]
    def resource
      @resource_resource ||= Resource.new(@client)
    end

    # Another sample resource to use in tests.
    #
    # @return [AnotherResource]
    def another_resource
      @another_resource_resource ||= AnotherResource.new(@client)
    end

    # Underscores the importance of supporting underscored resources
    #
    # @return [UnderscoredResource]
    def underscored_resource
      @underscored_resource_resource ||= UnderscoredResource.new(@client)
    end
  end

  private

  # A sample resource to use in tests.
  class Resource
    def initialize(client)
      @client = client
    end

    # Show all sample resources
    def list()
      @client.resource.list()
    end

    # Show a sample resource
    #
    # @param resource_uuid_field: A sample UUID field
    def info(resource_uuid_field)
      @client.resource.info(resource_uuid_field)
    end

    # Show a sample resource
    #
    # @param resource_uuid_field_or_resource_email_field: A sample UUID field or A sample email address field
    def identify_resource(resource_uuid_field_or_resource_email_field)
      @client.resource.identify_resource(resource_uuid_field_or_resource_email_field)
    end

    # Create a sample resource
    #
    # @param body: the object to pass as the request payload
    def create(body = {})
      @client.resource.create(body)
    end

    # Submit a sample resource as form data
    #
    # @param body: the object to pass as the request payload
    def submit(body = {})
      @client.resource.submit(body)
    end

    # Update a sample resource
    #
    # @param resource_uuid_field: A sample UUID field
    # @param body: the object to pass as the request payload
    def update(resource_uuid_field, body = {})
      @client.resource.update(resource_uuid_field, body)
    end

    # Delete an existing sample resource at specific time
    #
    # @param resource_date_field: A sample date field
    def delete(resource_date_field)
      @client.resource.delete(resource_date_field)
    end
  end

  # Another sample resource to use in tests.
  class AnotherResource
    def initialize(client)
      @client = client
    end

    # Show all sample resources
    def list()
      @client.another_resource.list()
    end
  end

  # Underscores the importance of supporting underscored resources
  class UnderscoredResource
    def initialize(client)
      @client = client
    end

    # Show all underscored resources
    def list()
      @client.underscored_resource.list()
    end
  end

  SCHEMA = Heroics::Schema.new(MultiJson.load(<<-'HEROICS_SCHEMA'))
{
  "description":"Sample schema for use in tests.",
  "definitions":{
    "resource":{
      "description":"A sample resource to use in tests.",
      "id":"schema/resource",
      "$schema":"http://json-schema.org/draft-04/hyper-schema",
      "title":"Sample resource title",
      "type":[
        "object"
      ],
      "definitions":{
        "date_field":{
          "description":"A sample date field",
          "example":"2013-10-19 22:10:29Z",
          "format":"date-time",
          "readOnly":true,
          "type":[
            "string"
          ]
        },
        "string_field":{
          "description":"A sample string field",
          "example":"Sample text.",
          "readOnly":true,
          "type":[
            "string"
          ]
        },
        "boolean_field":{
          "description":"A sample boolean field",
          "example":true,
          "type":[
            "boolean"
          ]
        },
        "uuid_field":{
          "description":"A sample UUID field",
          "example":"44724831-bf66-4bc2-865f-e2c4c2b14c78",
          "format":"uuid",
          "readOnly":true,
          "type":[
            "string"
          ]
        },
        "email_field":{
          "description":"A sample email address field",
          "example":"username@example.com",
          "format":"email",
          "readOnly":true,
          "type":[
            "string"
          ]
        },
        "identity":{
          "oneOf":[
            {
              "$ref":"#/definitions/resource/definitions/uuid_field"
            },
            {
              "$ref":"#/definitions/resource/definitions/email_field"
            }
          ]
        }
      },
      "properties":{
        "date_field":{
          "$ref":"#/definitions/resource/definitions/date_field"
        },
        "string_field":{
          "$ref":"#/definitions/resource/definitions/string_field"
        },
        "boolean_field":{
          "$ref":"#/definitions/resource/definitions/boolean_field"
        },
        "uuid_field":{
          "$ref":"#/definitions/resource/definitions/uuid_field"
        },
        "email_field":{
          "$ref":"#/definitions/resource/definitions/email_field"
        }
      },
      "links":[
        {
          "description":"Show all sample resources",
          "href":"/resource",
          "method":"GET",
          "rel":"instances",
          "title":"List"
        },
        {
          "description":"Show a sample resource",
          "href":"/resource/{(%23%2Fdefinitions%2Fresource%2Fdefinitions%2Fuuid_field)}",
          "method":"GET",
          "rel":"self",
          "title":"Info"
        },
        {
          "description":"Show a sample resource",
          "href":"/resource/{(%23%2Fdefinitions%2Fresource%2Fdefinitions%2Fidentity)}",
          "method":"GET",
          "rel":"self",
          "title":"Identify resource"
        },
        {
          "description":"Create a sample resource",
          "href":"/resource",
          "method":"POST",
          "rel":"create",
          "title":"Create",
          "schema":{
            "properties":{
              "date_field":{
                "$ref":"#/definitions/resource/definitions/date_field"
              },
              "string_field":{
                "$ref":"#/definitions/resource/definitions/string_field"
              },
              "boolean_field":{
                "$ref":"#/definitions/resource/definitions/boolean_field"
              },
              "uuid_field":{
                "$ref":"#/definitions/resource/definitions/uuid_field"
              },
              "email_field":{
                "$ref":"#/definitions/resource/definitions/email_field"
              }
            }
          }
        },
        {
          "description":"Submit a sample resource as form data",
          "encType":"application/x-www-form-urlencoded",
          "href":"/resource",
          "method":"POST",
          "rel":"submit",
          "title":"Submit",
          "schema":{
            "properties":{
              "date_field":{
                "$ref":"#/definitions/resource/definitions/date_field"
              },
              "string_field":{
                "$ref":"#/definitions/resource/definitions/string_field"
              },
              "boolean_field":{
                "$ref":"#/definitions/resource/definitions/boolean_field"
              },
              "uuid_field":{
                "$ref":"#/definitions/resource/definitions/uuid_field"
              },
              "email_field":{
                "$ref":"#/definitions/resource/definitions/email_field"
              }
            }
          }
        },
        {
          "description":"Update a sample resource",
          "href":"/resource/{(%23%2Fdefinitions%2Fresource%2Fdefinitions%2Fuuid_field)}",
          "method":"PATCH",
          "rel":"update",
          "title":"Update",
          "schema":{
            "properties":{
              "date_field":{
                "$ref":"#/definitions/resource/definitions/date_field"
              },
              "string_field":{
                "$ref":"#/definitions/resource/definitions/string_field"
              },
              "boolean_field":{
                "$ref":"#/definitions/resource/definitions/boolean_field"
              },
              "uuid_field":{
                "$ref":"#/definitions/resource/definitions/uuid_field"
              },
              "email_field":{
                "$ref":"#/definitions/resource/definitions/email_field"
              }
            }
          }
        },
        {
          "description":"Delete an existing sample resource at specific time",
          "href":"/resource/{(%23%2Fdefinitions%2Fresource%2Fdefinitions%2Fdate_field)}",
          "method":"DELETE",
          "rel":"destroy",
          "title":"Delete"
        }
      ]
    },
    "another-resource":{
      "description":"Another sample resource to use in tests.",
      "id":"schema/another-resource",
      "$schema":"http://json-schema.org/draft-04/hyper-schema",
      "title":"Another sample resource title",
      "type":[
        "object"
      ],
      "definitions":{},
      "properties":{},
      "links":[
        {
          "description":"Show all sample resources",
          "href":"/another-resource",
          "method":"GET",
          "rel":"instances",
          "title":"List"
        }
      ]
    },
    "underscored_resource":{
      "description":"Underscores the importance of supporting underscored resources",
      "id":"schema/underscored_resource",
      "$schema":"http://json-schema.org/draft-04/hyper-schema",
      "title":"Another underscored resource to use in tests",
      "type":[
        "object"
      ],
      "definitions":{},
      "properties":{},
      "links":[
        {
          "description":"Show all underscored resources",
          "href":"/underscored_resource",
          "method":"GET",
          "rel":"instances",
          "title":"List"
        }
      ]
    }
  },
  "properties":{
    "resource":{
      "$ref":"#/definitions/resource"
    },
    "another-resource":{
      "$ref":"#/definitions/another-resource"
    },
    "underscored_resource":{
      "$ref":"#/definitions/underscored_resource"
    }
  }
}

HEROICS_SCHEMA
end
