Sha256: 84e01c298c21539cd2861a2e8a98fb42f93efe22bc430d36cfc8fa434ed41fd7

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require_relative 'response'
require_relative 'error'

module Marvel
  module Request
    include Marvel::Response

    def get(path, options = {})
      request(:get, path, options)
    end

    private

    def request(method, path, options = {})
      etag = options.delete(:etag)
      response = connection.send(method) do |request|
        request.url(path, options.merge(auth))
        request.headers['If-None-Match'] = etag if etag
      end
      prepare(response)
    end

    def prepare(response)
      case response.status
      when 200 then Marvel::Response.create(response.body)
      when 304 then Marvel::Response::Error.new(
                      { 'code' => 304, 'status' => 'Not Modified' })
      else          Marvel::Response::Error.new(response.body)
      end
    end

    def auth(nonce = timestamp)
      { ts: nonce, apikey: api_key, hash: hash(nonce) }
    end

    def hash(nonce)
      Digest::MD5.hexdigest(nonce + private_key + api_key)
    end

    def timestamp
      Time.now.to_s
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
marvel_api-0.2.1 lib/marvel/request.rb