Class: Crazylegs::SignedURL

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

Overview

Used to create OAuth-signed URLs that you can then request via other means.

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

url

String containing the URL (without parameters) to request

method

The HTTP Request method that will be made, as a String

logger

a logger where you’d like to see diagnostics, if omitted will try $logger and then create one

Raises:

  • (ArgumentError)


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

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 || Logger.new(STDOUT)

  @oauth_params = {
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_version' => '1.0',
  }
  @oauth_params['oauth_consumer_key'] = credentials.consumer_key
  @oauth_params['oauth_token'] = credentials.access_token.token if credentials.access_token
  @params = {}
  @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



41
42
43
# File 'lib/crazylegs/url.rb', line 41

def logger
  @logger
end

Class Method Details

.encode_parts(url) ⇒ Object

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



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

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

.url_encode(string) ⇒ Object

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



34
35
36
37
38
# File 'lib/crazylegs/url.rb', line 34

def self.url_encode(string)
  string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
    '%' + $1.unpack('H2' * $1.size).join('%').upcase
  end.gsub(' ', '%20')
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)


80
81
82
83
84
85
86
87
88
89
# File 'lib/crazylegs/url.rb', line 80

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 timestamp:: the timestamp to use; defaults to ‘now’ and generally is only visible for testing nonce the nonce to use; defaults to a reasonable value and generally is only visible for testing

Returns a String that is the entire encoded OAuth-compliant URL ready to be requested. Note that part of the OAuth signing process is to include the HTTP request method; if you request this url using a method other than the one you passed to the constructor, it will not work.



108
109
110
111
112
113
114
# File 'lib/crazylegs/url.rb', line 108

def full_url(timestamp=nil,nonce=nil)
  query_string_params,oauth_params = get_query_and_oauth_parameters(timestamp,nonce)

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

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

Gets the full URL, signed and ready to be requested using the Authorization header style. As such, all of the parameters needed for OAuth are not part of the url returned here, instead you get the url and the headers needed to make the full request

timestamp:: the timestamp to use; defaults to ‘now’ and generally is only visible for testing nonce the nonce to use; defaults to a reasonable value and generally is only visible for testing

Returns an array of size two:

0

the url to request, as a String

1

the headers, as a Hash of String to String, to use with the request; without using these headers, the request will surely fail.



128
129
130
131
132
133
134
135
136
# File 'lib/crazylegs/url.rb', line 128

def full_url_using_headers(timestamp=nil,nonce=nil)
  @logger.debug("Getting full_url for header-based request of #{@url}")
  query_string_params,oauth_params = get_query_and_oauth_parameters(timestamp,nonce)
  assembled_url = assemble_url(query_string_params)
  oauth_headers = {
    'Authorization' => 'OAuth ' + oauth_params.to_a.sort.map { |param| "#{param[0]}=\"#{param[1]}\"" }.join(',')
  }
  return [assembled_url,oauth_headers]
end

#params=(params_hash) ⇒ Object

Sets all request parameters to those in the hash.

params_hash

hash of all the parameters you want to add; will replace existing parameters

Raises:

  • (ArgumentError)


94
95
96
97
98
99
# File 'lib/crazylegs/url.rb', line 94

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