Class: OauthCli

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ OauthCli

Returns a new instance of OauthCli.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/oauth_cli.rb', line 16

def initialize(options = {})
  @options = options

  if options[:consumer_key].to_s.empty?
    color = 'YELLOW'
    say " <%= color('# -------------------------------------------------------------------------', #{color}) %>"
    say " <%= color('# no consumer_key provided, please create a profile or call the script with:', #{color}) %>"
    say " <%= color('# oauthc --consumer_key=<consumer_key> --consumer_secret=<consumer_secret>', #{color}) %>"
    say " <%= color('# -------------------------------------------------------------------------', #{color}) %>"
    #please get a key here: #{options[:host]}/api_consumers"
    return
  end
  
  #add http if missing
  [:host, :reg_host, :auth_host].each do |key|
    @options[key] = "http://#{@options[key]}" unless @options[key] =~ /^http/
  end
  
  @consumer     = OAuth::Consumer.new(@options[:consumer_key], @options[:consumer_secret], :site => @options[:host])
  @access_token = OAuth::AccessToken.new(@consumer, @options[:token], @options[:token_secret]) if @options[:token]
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/oauth_cli.rb', line 14

def options
  @options
end

Instance Method Details

#request(method, uri, body = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/oauth_cli.rb', line 38

def request(method, uri, body = nil)
  if method =~ /auth/
    @request_token = @consumer.get_request_token({}, "oauth_callback" => "oob")
    @options[:auth_host] ||= "#{@options[:host].gsub('api.', 'www.').gsub('v1/', '')}/mobile/authorize?oauth_token=" #That's for Qype only!!
    color = 'YELLOW'
    say " <%= color('# To authorize, go to ...', #{color}) %>"
    say " <%= '#   ' + color('#{@options[:auth_host]}#{@request_token.token}', BOLD, UNDERLINE) %>\n"
    say " <%= color('#  ... and enter given token verifier:', #{color}) %>"
    verifier = ask " |-- verifier >> "

    begin
      @access_token = @request_token.get_access_token({}, "oauth_verifier" => verifier)

      @options[:token] = @access_token.token
      @options[:token_secret] = @access_token.secret

      color = 'GREEN'
      say " <%= color('# -------------------------------------------------------------------------', #{color}) %>"
      say " <%= color('# Authorization SUCCESSFUL', BOLD, #{color}) %>"
      say " <%= color('# -------------------------------------------------------------------------', #{color}) %>"
    rescue
      color = 'RED'
      say " <%= color('# -------------------------------------------------------------------------', #{color}) %>"
      say " <%= color('# Authorization FAILED', BOLD, #{color}) %>"
      say " <%= color('# -------------------------------------------------------------------------', #{color}) %>"
    end
    return
  end

  unless %w(get post put delete).include? method.to_s
    color = 'RED'
    say " <%= color('# -------------------------------------------------------------------------', #{color}) %>"
    say " <%= color('# Wrong HTTP Method: #{method}  - calling #{@options[:host]}#{uri}', BOLD, #{color}) %>"
    say " <%= color('# -------------------------------------------------------------------------', #{color}) %>"
    return
  end

  uri  = ask " |-- request uri >> " if !uri
  body = ask " |-- request body >> " if !body && (method == "post" || method == "put")
  
  url = @options[:host] + uri

  @options[:mime_type]    ||= (url =~ /\.json/) ? "application/json" : "application/xml"
  @options[:content_type] ||= (url =~ /\.json/) ? "application/json" : "application/xml"

  response = @consumer.request(method, url, @access_token, {}, body, { 'Accept' => @options[:mime_type], 'Content-Type' => @options[:content_type] })

  header = response.header

  color = (response.code.to_i < 400) ? 'GREEN' : 'RED'
  say " <%= color('# -------------------------------------------------------------------------', #{color}) %>"
  say " <%= color('# Status: #{header.code} #{header.message}  - calling #{@options[:host]}#{uri}', BOLD, #{color}) %>"
  say " <%= color('# -------------------------------------------------------------------------', #{color}) %>"

  body = response.body
  if header.content_type =~ /json/
    body = JSON.parse(body)
    ap(body) rescue say(JSON.pretty_generate(body))
    return
  end

  body = " <%= color('# #{$1.gsub("'", "")} ', RED) %>" if body =~ /<pre>(.+)<\/pre>/
  say body
end