Sha256: ce93f0ddd6d2169f1b7dd23525bd1c7007e52eb3400f4120c81e6006543e519a

Contents?: true

Size: 814 Bytes

Versions: 4

Compression:

Stored size: 814 Bytes

Contents

class HttpPage < UnderOs::Page

  def initialize
    @search = first('#search-form input')
    @result = first('img#result')
    @locker = Locker.new

    first('#search-form button').on(:tap) { search }
  end

  def search
    @search.blur
    @locker.show

    UnderOs::HTTP.get search_url do |response|
      if image_url = parse_first_image_url(response)
        @result.load image_url do
          @locker.hide
        end
      else
        @locker.hide
      end
    end
  end

  def search_url
    query = @search.value
    query = 'puppy' if query.empty?
    query = String.new(query).url_encode

    "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=#{query}"
  end

  def parse_first_image_url(response)
    response.json["responseData"]["results"][0]["url"] rescue nil # ftw!
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
under-os-1.4.0 app/pages/http_page.rb
under-os-1.3.0 app/pages/http_page.rb
under-os-1.2.1 app/pages/http_page.rb
under-os-1.2.0 app/pages/http_page.rb