Sha256: ac663c6f9c285348438152080c97f6c8b1e16b5b8b12629fbf8d8c7c571863a3

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

require 'aws/api'
require 'aws/call_types/action_param'

module AWS

  ##
  # Amazon's Mechanical Turk
  #
  # http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html
  #
  # All requests are POST and always through HTTPS.
  # Instead of regions, this API supports normal and sandbox mode. Use the third
  # parameter of #initialize to specify sandbox mode.
  #
  # For a more fleshed out object API for interacting with MechanicalTurk, you should
  # give rturk a try here: https://github.com/mdp/rturk
  ##
  class MechanicalTurk < API
    endpoint "mechanicalturk"
    use_https true
    version "2011-10-01"

    def initialize(key, secret, sandbox = false)
      super(key, secret, sandbox ? "sandbox" : nil)
    end

    include CallTypes::ActionParam

    protected

    # Sign the Turk request according to SignatureVersion 0 rules.
    #
    # As this is coming from CallTypes::ActionParam we need to fix
    # the Action param first thing.
    def finish_and_sign_request(request)
      request.params["Operation"] = request.params.delete("Action")

      request.params.merge!({
        "Service" => "AWSMechanicalTurkRequester",
        "AWSAccessKeyId" => self.access_key,
        "Timestamp" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
        "Version" => self.version
      })

      request.params["Signature"] = Base64.encode64(sign_request(request.params.clone)).chomp

      request
    end

    def sign_request(params)
      to_sign = [params["Service"], params["Operation"], params["Timestamp"]].join ""
      OpenSSL::HMAC.digest("sha1", self.secret_key, to_sign)
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simple_aws-0.0.1d lib/aws/mechanical_turk.rb
simple_aws-0.0.1c lib/aws/mechanical_turk.rb
simple_aws-0.0.1b lib/aws/mechanical_turk.rb