Module: OAuth2::UrlHelper

Included in:
Grant::Base
Defined in:
lib/oauth2/helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.http_basic_encode(username, password) ⇒ Object



31
32
33
34
# File 'lib/oauth2/helper.rb', line 31

def http_basic_encode(username, password)
  encoded_data = ["#{username}:#{password}"].pack("m0")
  "Basic #{encoded_data}"
end

Instance Method Details

#build_url(uri, opts = {}) ⇒ Object

convenience method to build response URI



7
8
9
10
11
12
13
14
15
16
# File 'lib/oauth2/helper.rb', line 7

def build_url(uri, opts={})
  path     = opts[:path] || ''
  query    = opts[:params] || {}
  fragment = opts[:fragment] || {}
  url = Addressable::URI.parse uri
  url.path = path
  url.query_values = query unless query.empty?
  url.fragment = Addressable::URI.form_encode(fragment) unless fragment.empty?
  url.to_s
end

#generate_timestampObject

:nodoc:



27
28
29
# File 'lib/oauth2/helper.rb', line 27

def generate_timestamp #:nodoc:
  Time.now.to_i.to_s
end

#generate_urlsafe_key(size = 48) ⇒ Object Also known as: generate_nonce

generates a random key of up to size bytes. The value returned is Base64 encoded with non-word characters removed.



20
21
22
23
24
# File 'lib/oauth2/helper.rb', line 20

def generate_urlsafe_key(size=48)
  seed = Time.now.to_i
  size = size - seed.to_s.length
  Base64.encode64("#{ OpenSSL::Random.random_bytes(size) }#{ seed }").gsub(/\W/, '')
end

#to_query(params) ⇒ Object

converts a hash to a URI query string



39
40
41
42
43
44
# File 'lib/oauth2/helper.rb', line 39

def to_query(params)
  unless params.is_a?(Hash)
    raise "Expected Hash but got #{params.class.name}"
  end
  Addressable::URI.form_encode(params)
end