Module: OauthTwitter::Helper

Included in:
OauthTwitter
Defined in:
lib/oauth_twitter/helper.rb

Constant Summary collapse

HOST =

Twitter API root url

"https://api.twitter.com"

Instance Method Summary collapse

Instance Method Details

#send_request(method, path, original_query, oauth = true, options = {}) ⇒ Object

Package data and send oauth request

oauth = true : true - include oauth token

false - do not include oauth token
[false, {hash}] - include additional params

options = {} : :detailed => fasle - return false if request failed,

otherwise return just data



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/oauth_twitter/helper.rb', line 24

def send_request(method, path, original_query, oauth=true, options={})
  # generate signing key
  signing_key_array = [ Config.consumer_secret ]

  # generate oauth params
  if !!oauth === oauth # boolean
    oauth_params = generate_oauth_params(oauth)
    signing_key_array.push((oauth ? self.oauth_token_secret : '' ))
  else
    oauth_params = generate_oauth_params(oauth[0], oauth[1])
    signing_key_array.push((oauth[0] ? self.oauth_token_secret : '' ))
  end

  # filter out value == nil
  query = original_query.select {|key, value| !value.nil?}

  # generate base string
  base_array = [ method.to_s.upcase, URI.encode_www_form_component(HOST + path), URI.encode_www_form_component(URI.encode_www_form((query ? oauth_params.merge(query) : oauth_params).sort)) ]

  # generate signature
  oauth_params[:oauth_signature] = sign(base_array, signing_key_array)

  # generate HTTP request
  uri = URI.parse(HOST + path)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  if method.to_s.upcase === 'GET'
    uri.query = URI.encode_www_form(query) if query
    request = Net::HTTP::Get.new(uri.request_uri)
  elsif method.to_s.upcase === 'POST'
    request = Net::HTTP::Post.new(uri.request_uri)
    request.set_form_data(query) if query
  end
  request["Authorization"] = generate_oauth_header(oauth_params)

  # retrive response
  return parse_response(https.request(request), options)
end