Class: OauthCli

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile = nil) ⇒ OauthCli

Returns a new instance of OauthCli.



17
18
19
20
# File 'lib/oauth_cli.rb', line 17

def initialize(profile = nil)
  @options = {}
  connect(profile)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.add_profile(name, values) ⇒ Object



108
109
110
111
# File 'lib/oauth_cli.rb', line 108

def self.add_profile(name, values)
  @profiles[name] = values
  name
end

.inializeObject

Static Setup Mehtods



91
92
93
# File 'lib/oauth_cli.rb', line 91

def self.inialize
  @profiles || {}
end

.load_profiles(cfg_file, tmp_file) ⇒ Object



95
96
97
98
99
# File 'lib/oauth_cli.rb', line 95

def self.load_profiles(cfg_file, tmp_file)
  @cfg_file = cfg_file #keep so we can save back to file
  @profiles = load(cfg_file)
  @templates = load(tmp_file)
end

.parse_args(args, opt = {}, last_arg = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/oauth_cli.rb', line 121

def self.parse_args(args, opt = {}, last_arg = nil)
  @profiles ||= {}
  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
  
  profile = opt.delete(:profile) || opt.delete(:p)

  if !@profiles[profile] && opt.any?
    profile ||= 'commandline'
    @profiles[profile] = opt
    save_profiles unless profile == 'commandline'
  end        
    
  if !@profiles[profile] && @default_profile
    profile = @default_profile
    say "Using default profile: #{profile}"
  end
  
  if profile && !@profiles[profile]
    say_error "Profile #{profile} not found"
    profile = nil
  end
  
  [method, uri, body, profile]
end

.profilesObject



113
114
115
# File 'lib/oauth_cli.rb', line 113

def self.profiles
  @profiles || {}
end

.save_profilesObject



101
102
103
104
105
106
# File 'lib/oauth_cli.rb', line 101

def self.save_profiles
  return unless @cfg_file
  File.open(@cfg_file, 'w') do |out|
     YAML.dump(@profiles, out)
  end
end

.templatesObject



117
118
119
# File 'lib/oauth_cli.rb', line 117

def self.templates
  @templates || {}
end

Instance Method Details

#access_request_urlObject



46
47
48
49
50
# File 'lib/oauth_cli.rb', line 46

def access_request_url
  @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!!
  url = "#{@options[:auth_url]}?oauth_token=#{@request_token.token}"
end

#access_token(verifier) ⇒ Object



52
53
54
55
# File 'lib/oauth_cli.rb', line 52

def access_token(verifier)
  request_url unless @request_token
  @access_token = @request_token.get_access_token({}, "oauth_verifier" => verifier)
end

#auth?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/oauth_cli.rb', line 42

def auth?
  @access_token
end

#connect(profile) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/oauth_cli.rb', line 22

def connect(profile)
  return false if !profile.is_a?(Hash) && !OauthCli.profiles[profile]
  @options = OauthCli.profiles[profile] || profile
  
  #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]

  connected?
end

#connected?Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/oauth_cli.rb', line 37

def connected?
  # TODO check if host available?
  @options[:consumer_key] && @options[:consumer_secret] && @options[:host]
end

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



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
# File 'lib/oauth_cli.rb', line 57

def request(method, uri, body = nil)
  unless %w(get post put delete).include? method.to_s
    say_message "Wrong HTTP Method: #{method}  - calling #{@options[:host]}#{uri}", 'RED'
    return
  end

  uri  = ask_prompt "request uri" if !uri
  body = ask_prompt "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_message "Status: #{header.code} #{header.message}  - calling #{@options[:host]}#{uri}", 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