Class: Gliffy::SignedURL

Inherits:
Object
  • Object
show all
Defined in:
lib/gliffy/url.rb

Overview

Handles signing and assembling the URL

Constant Summary collapse

READ_ONLY_PARAMS =
{
  'oauth_consumer_key' => true,
  'oauth_token' => true,
  'oauth_signature_method' => true,
  'oauth_version' => true,
  'oauth_nonce' => true,
  'oauth_timestamp' => true,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials, url, method, logger = nil) ⇒ SignedURL

Create a new SignedURL

credentails

The credentials available when signing the request (required).

url

The URL (without parameters) to request (required)

method

The HTTP Request method that will be made (required)

Raises:

  • (ArgumentError)


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
# File 'lib/gliffy/url.rb', line 47

def initialize(credentials,url,method,logger=nil)
  raise ArgumentError.new("credentials is required") if credentials.nil?
  raise ArgumentError.new("url is required") if url.nil?
  raise ArgumentError.new("method is required") if method.nil?

  @credentials = credentials

  @logger = logger || Logger.new(STDOUT)
  @logger.level = Logger::INFO

  @params = {
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_version' => '1.0',
  }
  @params['oauth_consumer_key'] = credentials.consumer_key
  @params['oauth_token'] = credentials.access_token.token if credentials.access_token
  @consumer_secret = credentials.consumer_secret
  if credentials.access_token
    @access_secret = credentials.access_token.secret 
  else
    @access_secret = nil
  end
  @method = method.upcase
  @url = url
end

Instance Attribute Details

#loggerObject

Modify the logger



40
41
42
# File 'lib/gliffy/url.rb', line 40

def logger
  @logger
end

Class Method Details

.encode(string) ⇒ Object

Ruby’s CGI::encode doesn’t encode spaces correctly



33
34
35
36
37
# File 'lib/gliffy/url.rb', line 33

def self.encode(string)
  string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
    '%' + $1.unpack('H2' * $1.size).join('%').upcase
  end.gsub(' ', '%20')
end

.encodeParts(url) ⇒ Object

Encodes each part of this url, accounting for some of the weirdness we are dealing with



21
22
23
24
25
26
27
28
29
30
# File 'lib/gliffy/url.rb', line 21

def self.encodeParts(url)
  parts = url.split(/\//).map do |part|
    if part =~ /^\$/
      part
    else
      encode(part)
    end
  end
  parts.join('/')
end

Instance Method Details

#[]=(param, value) ⇒ Object

Sets a request parameter

param

the name of the parameter, as a string or symbol

value

the value of the parameter, unencoded

Raises:

  • (ArgumentError)


78
79
80
81
82
83
84
85
86
87
# File 'lib/gliffy/url.rb', line 78

def []=(param,value)
  raise ArgumentError.new("param may not be nil") if param.nil?
  param = param.to_s
  raise ArgumentError.new("You may not override #{param}") if READ_ONLY_PARAMS[param]
  if value.nil? 
    @params.delete(param)
  else
    @params[param] = value.to_s
  end
end

#full_url(timestamp = nil, nonce = nil) ⇒ Object

Gets the full URL, signed and ready to be requested



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/gliffy/url.rb', line 98

def full_url(timestamp=nil,nonce=nil)

  @logger.debug("Getting full_url of #{@url}")
  @logger.debug("OAuth Part 1 : #{@method}")

  escaped_url = SignedURL::encode(@url)
  to_sign = @method + "&" + escaped_url + "&"

  @logger.debug("OAuth Part 2 (raw) : #{@url}")
  @logger.debug("OAuth Part 2 (esc) : #{escaped_url}")

  timestamp=Time.now.to_i if timestamp.nil?
  nonce=@credentials.nonce if nonce.nil?

  param_part,url_params = handle_params(timestamp,nonce)
  escaped_params = SignedURL::encode(param_part)
  @logger.debug("OAuth Part 3 (raw) : #{param_part}")
  @logger.debug("OAuth Part 3 (esc) : #{escaped_params}")

  to_sign += escaped_params

  signature = get_signature(to_sign)

  url_params['oauth_signature'] = SignedURL::encode(signature)

  assembled_url = assemble_url(url_params)
  @logger.debug("Full URL is " + assembled_url)
  return assembled_url
end

#params=(params_hash) ⇒ Object

Sets all request parameters to those in the hash.

Raises:

  • (ArgumentError)


90
91
92
93
94
95
# File 'lib/gliffy/url.rb', line 90

def params=(params_hash)
  raise ArgumentError.new('you may not set params to nil') if params_hash.nil?
  params_hash.each do |k,v|
    self[k]=v
  end
end