from restkit import Resource

from hoppy import api_key

class Deploy(Resource):
    def __init__(self, use_ssl=False):
        self.api_key = api_key
        super(Deploy, self).__init__(self.host, follow_redirect=True)

    def check_configuration(self):
        if not self.api_key:
            raise HoptoadError('API Key cannot be blank')

    def request(self, *args, **kwargs):
        response = super(Deploy, self).request(
                api_key=self.api_key, *args, **kwargs)
        return response.body_string()

    def base_uri(self, use_ssl=False):
        base = 'http://hoptoadapp.com/deploys.txt'
        base = base.replace('http://', 'https://') if use_ssl else base
        return base

    def deploy(self, env, **kwargs):
        """ Optional parameters accepted by Hoptoad are:
        scm_revision
        scm_repository
        local_username
        """
        params = {}
        params['deploy[rails_env]'] = env
        for key, value in kwargs:
            params['deploy[%s]' % key] = value
        return self.post(**params)

from hoppy.api import HoptoadResource

class Deploy(HoptoadResource):
    def __init__(self, use_ssl=False):
        from hoppy import api_key
        self.api_key = api_key
        super(Deploy, self).__init__(use_ssl)

    def check_configuration(self):
        if not self.api_key:
            raise HoptoadError('API Key cannot be blank')

    def request(self, *args, **kwargs):
        response = super(Deploy, self).request(
                api_key=self.api_key, *args, **kwargs)
        return response

    def base_uri(self, use_ssl=False):
        base = 'http://hoptoadapp.com/deploys.txt'
        base = base.replace('http://', 'https://') if use_ssl else base
        return base

    def deploy(self, env, **kwargs):
        """ Optional parameters accepted by Hoptoad are:
        scm_revision
        scm_repository
        local_username
        """
        params = {}
        params['deploy[rails_env]'] = env
        for key, value in kwargs.iteritems():
            params['deploy[%s]' % key] = value
        return self.post(**params)

Test Deploy resource after reworking.
Test Deploy resource after reworking.

- from restkit import Resource
+ from hoppy.api import HoptoadResource
  
- from hoppy import api_key
- 
- class Deploy(Resource):
+ class Deploy(HoptoadResource):
      def __init__(self, use_ssl=False):
+         from hoppy import api_key
          self.api_key = api_key
-         super(Deploy, self).__init__(self.host, follow_redirect=True)
+         super(Deploy, self).__init__(use_ssl)
  
      def check_configuration(self):
          if not self.api_key:
              raise HoptoadError('API Key cannot be blank')
  
      def request(self, *args, **kwargs):
          response = super(Deploy, self).request(
                  api_key=self.api_key, *args, **kwargs)
-         return response.body_string()
+         return response
  
      def base_uri(self, use_ssl=False):
          base = 'http://hoptoadapp.com/deploys.txt'
          base = base.replace('http://', 'https://') if use_ssl else base
          return base
  
      def deploy(self, env, **kwargs):
          """ Optional parameters accepted by Hoptoad are:
          scm_revision
          scm_repository
          local_username
          """
          params = {}
          params['deploy[rails_env]'] = env
-         for key, value in kwargs:
+         for key, value in kwargs.iteritems():
              params['deploy[%s]' % key] = value
          return self.post(**params)
  
Test Deploy resource after reworking.
## Code Before:
from restkit import Resource

from hoppy import api_key

class Deploy(Resource):
    def __init__(self, use_ssl=False):
        self.api_key = api_key
        super(Deploy, self).__init__(self.host, follow_redirect=True)

    def check_configuration(self):
        if not self.api_key:
            raise HoptoadError('API Key cannot be blank')

    def request(self, *args, **kwargs):
        response = super(Deploy, self).request(
                api_key=self.api_key, *args, **kwargs)
        return response.body_string()

    def base_uri(self, use_ssl=False):
        base = 'http://hoptoadapp.com/deploys.txt'
        base = base.replace('http://', 'https://') if use_ssl else base
        return base

    def deploy(self, env, **kwargs):
        """ Optional parameters accepted by Hoptoad are:
        scm_revision
        scm_repository
        local_username
        """
        params = {}
        params['deploy[rails_env]'] = env
        for key, value in kwargs:
            params['deploy[%s]' % key] = value
        return self.post(**params)

## Instruction:
Test Deploy resource after reworking.
## Code After:
from hoppy.api import HoptoadResource

class Deploy(HoptoadResource):
    def __init__(self, use_ssl=False):
        from hoppy import api_key
        self.api_key = api_key
        super(Deploy, self).__init__(use_ssl)

    def check_configuration(self):
        if not self.api_key:
            raise HoptoadError('API Key cannot be blank')

    def request(self, *args, **kwargs):
        response = super(Deploy, self).request(
                api_key=self.api_key, *args, **kwargs)
        return response

    def base_uri(self, use_ssl=False):
        base = 'http://hoptoadapp.com/deploys.txt'
        base = base.replace('http://', 'https://') if use_ssl else base
        return base

    def deploy(self, env, **kwargs):
        """ Optional parameters accepted by Hoptoad are:
        scm_revision
        scm_repository
        local_username
        """
        params = {}
        params['deploy[rails_env]'] = env
        for key, value in kwargs.iteritems():
            params['deploy[%s]' % key] = value
        return self.post(**params)
