Class: ScreenshotLayer::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/screenshot_capture.rb

Instance Method Summary collapse

Constructor Details

#initialize(access_key, secret_keyword) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/screenshot_capture.rb', line 16

def initialize(access_key, secret_keyword)

  if access_key.nil?
    raise ScreenshotLayer::MissingArgumentException.new 'access_key'
  end

  if secret_keyword.nil?
    raise ScreenshotLayer::MissingArgumentException.new 'secret_keyword'
  end

  @access_key = access_key
  @secret_keyword = secret_keyword

end

Instance Method Details

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/screenshot_capture.rb', line 32

def capture(url, options = {})

  if url.nil?
    raise ScreenshotLayer::MissingArgumentException.new 'url'
    return
  end

  # Create a shallow copy so we don't manipulate the original reference
  query = options.dup

  # Generate the SecretKey for the request
  md5 = Digest::MD5.new
  md5.update url + @secret_keyword
  secret_key = md5.hexdigest

  # Populate the Query
  query.access_key = @access_key
  query.secret_key = secret_key
  query.url = URI.escape(url)

  # We then create the Request
  req = CaptureRequest.new(query)

  #  We create a Hash of the request so we can send it via HTTP
  req_dto = req.to_dh

  begin

    # We make the actual request
    res = self.class.get('/capture', req_dto)

    # We ensure that we tap the response so we can use the results
    res.inspect

    # If we have an export option passed in, we save it to local file system
    if options.filename.nil?

      # We just return the parsed binary response
      return res.parsed_response

    else

      begin

        # Ensure the path exists before we write
        FileUtils.mkdir_p(File.dirname(options.filename))

        File.open(options.filename, 'a+') do |file|

          file.write(res.body)

          result = {
              success: true,
              info: ScreenshotLayer::CaptureResponse::INFO_MESSAGE_SUCCESS_FILENAME,
              file_name: options.filename
          }
          return result

        end

      end
    end


  rescue => e
    puts e.inspect
    return

  ensure
    # Clean Up

  end
end