Class: OauthCli
- Inherits:
-
Object
- Object
- OauthCli
- Defined in:
- lib/oauth_cli.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ OauthCli
constructor
A new instance of OauthCli.
- #request(method, uri, body = nil) ⇒ Object
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 = if [: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_url, :auth_url].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
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/oauth_cli.rb', line 14 def @options end |
Class Method Details
.parse_args(args, opt = {}, last_arg = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/oauth_cli.rb', line 38 def self.parse_args(args, opt = {}, last_arg = nil) method, uri, body = args.clone.delete_if do |kv| next opt[$1.to_sym] = $2 if kv =~ /-?-([^=]+)=(.+)$/ #catches --param=value next opt[last_arg] = kv if last_arg && !opt[last_arg] #catches value next last_arg = $1.to_sym if kv =~ /^-?-(.+)$/ #catches --param false end [method, uri, body, opt, (opt.delete(:profile) || opt.delete(:p))] end |
Instance Method Details
#request(method, uri, body = nil) ⇒ Object
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 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/oauth_cli.rb', line 48 def request(method, uri, body = nil) if method =~ /auth/ @request_token = @consumer.get_request_token({}, "oauth_callback" => "oob") @options[:auth_url] ||= "#{@options[:host].gsub('api.', 'www.').gsub('v1/', '')}/mobile/authorize" #That's for Qype only!! color = 'YELLOW' say " <%= color('# To authorize, go to ...', #{color}) %>" say " <%= '# ' + color('#{@options[:auth_url]}?oauth_token=#{@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}) %>" say " <%= color(' token: #{@access_token.token}', #{color}) %>" say " <%= color(' token_secret: #{@access_token.secret}', #{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.} - 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 |