Class List Method List File List

# Module: WpUser::Existable

Included in:
  WpUser
Defined in:
  lib/common/models/wp_user/existable.rb

## Class Method Summary (collapse)

- + (String) display_name_from_body(body)

  The display_name.

- + (String) login_from_author_pattern(text)

  The login.

- + (String) login_from_body(body)

  The login.

## Instance Method Summary (collapse)

- - (Boolean) exists_from_response?(response, options = {})
- - (void) load_from_response(response) private

  Load the login and display_name from the response.

## Class Method Details

### + (`String`) display_name_from_body(body)

Note:

Some bodies are encoded in ASCII-8BIT, and Nokogiri doesn’t support it So it’s forced to UTF-8 when this encoding is detected

The display_name

Parameters:

- body (`String`)

Returns:

- (`String`) —

  The display_name

    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    # File 'lib/common/models/wp_user/existable.rb', line 64

    def self.display_name_from_body(body)
      if title_tag = body[%r{<title>([^<]+)</title>}i, 1]
        title_tag.force_encoding('UTF-8') if title_tag.encoding == Encoding::ASCII_8BIT
        title_tag = Nokogiri::HTML::DocumentFragment.parse(title_tag).to_s
        # &amp; are not decoded with Nokogiri
        title_tag.sub!('&amp;', '&')

        name = title_tag[%r{([^|«]+) }, 1]

        return name.strip if name
      end
    end

### + (`String`) login_from_author_pattern(text)

The login

Parameters:

- text (`String`)

Returns:

- (`String`) —

  The login

    39
    40
    41
    # File 'lib/common/models/wp_user/existable.rb', line 39

    def self.login_from_author_pattern(text)
      text[%r{/author/([^/\b]+)/?}i, 1]
    end

### + (`String`) login_from_body(body)

The login

Parameters:

- body (`String`)

Returns:

- (`String`) —

  The login

    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    # File 'lib/common/models/wp_user/existable.rb', line 46

    def self.login_from_body(body)
      # Feed URL with Permalinks
      login = WpUser::Existable.login_from_author_pattern(body)

      unless login
        # No Permalinks
        login = body[%r{<body class="archive author author-([^\s]+) author-(\d+)}i, 1]
      end

      login
    end

## Instance Method Details

### - (`Boolean`) exists_from_response?(response, options = {})

Parameters:

- response (`Typhoeus::Response`)
- options (`Hash`) (defaults to: `{}`)

Returns:

- (`Boolean`)

    10
    11
    12
    13
    14
    # File 'lib/common/models/wp_user/existable.rb', line 10

    def exists_from_response?(response, options = {})
      load_from_response(response)

      @login ? true : false
    end

### - (`void`) load_from_response(response) (private)

This method returns an undefined value.

Load the login and display_name from the response

Parameters:

- response (`Typhoeus::Response`)

    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    # File 'lib/common/models/wp_user/existable.rb', line 21

    def load_from_response(response)
      if response.code == 301 # login in location?
        location = response.headers_hash['Location']

        @login        = Existable.login_from_author_pattern(location)
        @display_name = Existable.display_name_from_body(
          Browser.get(location).body
        )
      elsif response.code == 200 # login in body?
        @login        = Existable.login_from_body(response.body)
        @display_name = Existable.display_name_from_body(response.body)
      end
    end