Sha256: fa4c85ed37e0d99e98113e10c97d8cced9049f92c340484eed34afaf6364521d

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

require 'logger'

class JohnStamos::Client
  attr_accessor :proxy

  def initialize(options={})
    default_options = { proxy: nil }
    options = default_options.merge(options)
    @proxy = options[:proxy]
  end

  def search_pins(search_text, options={})
    search_scraper = JohnStamos::PinSearch.new(self, search_text, options)
    search_scraper.execute!

    search_scraper.pins
  end

  def pin(pinterest_pin_id)
    JohnStamos::Pin.new(self, pinterest_pin_id)
  end

  def pinner(username)
    JohnStamos::Pinner.new(self, username)
  end

  def page_content(url)
    response = make_request(url)

    Nokogiri::HTML(response)
  end

  def json_content(url, params)
    response = make_json_request(url, params)

    JSON.parse(response)
  end

  private
    def make_request(url, params={}, accept_json=false)
      request_headers = build_request_headers(accept_json)

      response = pinterest_connection.get do |req|
        req.url url, params
        req.headers = request_headers
      end

      response.body
    end

    def make_json_request(url, params={})
      make_request(url, params, true)
    end

    def build_request_headers(accept_json=false)
      headers = {}

      if accept_json
        headers['Accept'] = 'application/json'
        headers['X-Requested-With'] = 'XMLHttpRequest'
      end
      headers['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36'

      headers
    end

    def pinterest_connection
      pinterest_url = 'http://pinterest.com'
      @pinterest_connection ||= @proxy.nil? ? Faraday.new(url: pinterest_url) : Faraday.new(url: pinterest_url, proxy: @proxy)
    end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
john_stamos-0.0.2 lib/john_stamos/client.rb