Class: GyazoSpector::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gyazo_spector/client.rb

Overview

concreate implemtation of Client

Constant Summary collapse

CONTENT_TYPE =
'image/png'.freeze
DEFAULT_SITE =
'http://gyazo.com'.freeze
DEFAULT_ENDPOINT =
'/'.freeze
DEFAULT_POLTERGEIST_OPTIONS =
{
  js_errors: false,
  timeout: 1000,
  debug: false
}.freeze
DEFAULT_SELECTOR =
:css

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gyazo_spector/client.rb', line 18

def initialize(opts = {})
  @site = opts.delete(:site) || DEFAULT_SITE
  @endpoint = opts.delete(:endpoint) || DEFAULT_ENDPOINT
  Capybara.default_selector = opts.delete(:selector) || DEFAULT_SELECTOR

  Capybara.register_driver :poltergeist do |app|
    Capybara::Poltergeist::Driver.new(
      app, DEFAULT_POLTERGEIST_OPTIONS.merge(opts)
    )
  end
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



16
17
18
# File 'lib/gyazo_spector/client.rb', line 16

def endpoint
  @endpoint
end

#imagedataObject (readonly)

Returns the value of attribute imagedata.



16
17
18
# File 'lib/gyazo_spector/client.rb', line 16

def imagedata
  @imagedata
end

#sessionObject (readonly)

Returns the value of attribute session.



16
17
18
# File 'lib/gyazo_spector/client.rb', line 16

def session
  @session
end

#siteObject (readonly)

Returns the value of attribute site.



16
17
18
# File 'lib/gyazo_spector/client.rb', line 16

def site
  @site
end

Instance Method Details

#capture(url, options = {}) {|session| ... } ⇒ Object

Yields:



34
35
36
37
38
39
# File 'lib/gyazo_spector/client.rb', line 34

def capture(url, options = {})
  session.visit(url)
  yield(session) if block_given?
  @imagedata = Base64.decode64(session.driver.render_base64(:png, options))
  self
end

#upload!Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/gyazo_spector/client.rb', line 41

def upload!
  Faraday.new(site) do |client|
    client.request :multipart
    client.request :url_encoded
    client.adapter Faraday.default_adapter
  end.post(
    endpoint,
    imagedata: Faraday::UploadIO.new(StringIO.new(imagedata), CONTENT_TYPE)
  ).body
end