Class: Jiveapps::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, password, host = Jiveapps::WEBHOST) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
# File 'lib/jiveapps/client.rb', line 13

def initialize(user, password, host=Jiveapps::WEBHOST)
  @user     = user
  @password = password
  @host     = host
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



11
12
13
# File 'lib/jiveapps/client.rb', line 11

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



11
12
13
# File 'lib/jiveapps/client.rb', line 11

def password
  @password
end

#userObject (readonly)

Returns the value of attribute user.



11
12
13
# File 'lib/jiveapps/client.rb', line 11

def user
  @user
end

Class Method Details

.gem_version_stringObject



7
8
9
# File 'lib/jiveapps/client.rb', line 7

def self.gem_version_string
  "jiveapps-gem/#{Jiveapps::VERSION}"
end

Instance Method Details

#_escape(value) ⇒ Object

:nodoc:



219
220
221
222
# File 'lib/jiveapps/client.rb', line 219

def _escape(value)  # :nodoc:
  escaped = URI.escape(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
  escaped.gsub('.', '%2E') # not covered by the previous URI.escape
end

#add_collaborator(app_name, username) ⇒ Object

Invite a person by username to collaborate on the app.



138
139
140
# File 'lib/jiveapps/client.rb', line 138

def add_collaborator(app_name, username)
  post("/apps/#{escape(app_name)}/collaborators", {:collaborator => {:username => username}})
end

#add_key(key) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/jiveapps/client.rb', line 90

def add_key(key)
  item = post("/ssh_keys", {:ssh_key => {:key => key}})

  if item.class == Hash && item['ssh_key']
    item['ssh_key']
  else
    nil
  end
end

#add_oauth_service(app_name, name, key, secret) ⇒ Object



116
117
118
# File 'lib/jiveapps/client.rb', line 116

def add_oauth_service(app_name, name, key, secret)
  post("/apps/#{escape(app_name)}/oauth_services", {:oauth_service => {:name => name, :key => key, :secret => secret}})
end

#create(name) ⇒ Object



58
59
60
# File 'lib/jiveapps/client.rb', line 58

def create(name)
  post_app("/apps", {:app => {:name => name}})
end

#delete(uri, extra_headers = {}) ⇒ Object

:nodoc:



169
170
171
# File 'lib/jiveapps/client.rb', line 169

def delete(uri, extra_headers={})    # :nodoc:
  process(:delete, uri, extra_headers)
end

#delete_app(name) ⇒ Object



62
63
64
# File 'lib/jiveapps/client.rb', line 62

def delete_app(name)
  delete("/apps/#{escape(name)}").to_s
end

#escape(value) ⇒ Object

:nodoc:



209
210
211
212
213
214
215
216
217
# File 'lib/jiveapps/client.rb', line 209

def escape(value) # :nodoc:
  ### Ugly hack - nginx/passenger unescapes the name before it gets
  ### to rails, causing routes to fail. double encode in production
  if Jiveapps::MODE == 'production'
    _escape(_escape(value))
  else
    _escape(value)
  end
end

#extract_warning(response) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
# File 'lib/jiveapps/client.rb', line 233

def extract_warning(response)
  return unless response
  if response.headers[:x_jiveapps_warning] && @warning_callback
    warning = response.headers[:x_jiveapps_warning]
    @displayed_warnings ||= {}
    unless @displayed_warnings[warning]
      @warning_callback.call(warning)
      @displayed_warnings[warning] = true
    end
  end
end

#get(uri, extra_headers = {}) ⇒ Object

:nodoc:



157
158
159
# File 'lib/jiveapps/client.rb', line 157

def get(uri, extra_headers={})    # :nodoc:
  process(:get, uri, extra_headers)
end

#git_hostObject



245
246
247
# File 'lib/jiveapps/client.rb', line 245

def git_host
  host.gsub(/^(http|https):\/\//, '')
end

#info(name) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/jiveapps/client.rb', line 31

def info(name)
  begin
    item = get("/apps/#{escape(name)}?extended=true")
    item.class == Hash && item['app'] ? item['app'] : item
  rescue RestClient::ResourceNotFound
    nil
  end
end

#install(name) ⇒ Object



70
71
72
# File 'lib/jiveapps/client.rb', line 70

def install(name)
  post_app("/apps/#{escape(name)}/install", {})
end

#jiveapps_headersObject

:nodoc:



198
199
200
201
202
203
204
205
206
207
# File 'lib/jiveapps/client.rb', line 198

def jiveapps_headers   # :nodoc:
  {
    'X-Jiveapps-API-Version' => '1',
    'User-Agent'             => self.class.gem_version_string,
    'X-Ruby-Version'         => RUBY_VERSION,
    'X-Ruby-Platform'        => RUBY_PLATFORM,
    'Accept'                 => 'application/json',
    'Content-Type'           => 'application/json'
  }
end

#keysObject

SSH Keys



80
81
82
83
84
85
86
87
88
# File 'lib/jiveapps/client.rb', line 80

def keys
  ssh_keys = get('/ssh_keys')

  if ssh_keys.class == Array
    ssh_keys.map { |item| item['ssh_key'] }
  else
    return []
  end
end

#listObject

Apps



21
22
23
24
25
26
27
28
29
# File 'lib/jiveapps/client.rb', line 21

def list
  apps = get('/apps')

  if apps.class == Array
    apps.map { |item| item['app'] }
  else
    return apps
  end
end

#list_collaborators(app_name) ⇒ Object

Get a list of collaborators on the app, returns an array of hashes each with :email



127
128
129
130
131
132
133
134
135
# File 'lib/jiveapps/client.rb', line 127

def list_collaborators(app_name)
  collaborators = get("/apps/#{escape(app_name)}/collaborators")

  if collaborators.class == Array
    collaborators.map { |item| {:username => item['collaborator']['user']['username']} }
  else
    return []
  end
end

#list_oauth_services(app_name) ⇒ Object

OAuth Services



106
107
108
109
110
111
112
113
114
# File 'lib/jiveapps/client.rb', line 106

def list_oauth_services(app_name)
  services = get("/apps/#{escape(app_name)}/oauth_services")

  if services.class == Array
    services.map { |item| item['oauth_service'] }
  else
    return []
  end
end

#livedev(name, mode) ⇒ Object



74
75
76
# File 'lib/jiveapps/client.rb', line 74

def livedev(name, mode)
  post_app("/apps/#{escape(name)}/livedev?mode=#{mode}", {})
end

#on_warning(&blk) ⇒ Object



153
154
155
# File 'lib/jiveapps/client.rb', line 153

def on_warning(&blk)
  @warning_callback = blk
end

#parse_response(response) ⇒ Object



187
188
189
190
191
192
193
194
195
196
# File 'lib/jiveapps/client.rb', line 187

def parse_response(response)
  return nil if response == 'null' || response.strip.length == 0
  response_text = response.strip

  if response_text =~ /^\{|^\[/
    return ActiveSupport::JSON.decode(response_text)
  else
    return response_text
  end
end

#post(uri, object, extra_headers = {}) ⇒ Object

:nodoc:



161
162
163
# File 'lib/jiveapps/client.rb', line 161

def post(uri, object, extra_headers={})    # :nodoc:
  process(:post, uri, extra_headers, ActiveSupport::JSON.encode(object))
end

#post_app(path, obj) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jiveapps/client.rb', line 40

def post_app(path, obj)
  begin
    item = post(path, obj)
    if item.class == Hash && item['app']
      item['app']
    else
      item
    end
  rescue => e
    if e.response.body =~ /^\{/ # assume this is JSON if it starts with "{"
      errors = ActiveSupport::JSON.decode(e.response.body)
      return errors
    else
      nil
    end
  end
end

#process(method, uri, extra_headers = {}, payload = nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/jiveapps/client.rb', line 173

def process(method, uri, extra_headers={}, payload=nil)
  headers  = jiveapps_headers.merge(extra_headers)
  args     = [method, payload, headers].compact

  begin
    response = resource(uri).send(*args)
    extract_warning(response)
    parse_response(response.to_s)
  rescue RestClient::BadRequest => e
    puts e.response.body
    return nil
  end
end

#put(uri, object, extra_headers = {}) ⇒ Object

:nodoc:



165
166
167
# File 'lib/jiveapps/client.rb', line 165

def put(uri, object, extra_headers={})    # :nodoc:
  process(:put, uri, extra_headers, ActiveSupport::JSON.encode(object))
end

#register(name) ⇒ Object



66
67
68
# File 'lib/jiveapps/client.rb', line 66

def register(name)
  post_app("/apps/#{escape(name)}/register", {})
end

#remove_collaborator(app_name, username) ⇒ Object

Remove a collaborator.



143
144
145
# File 'lib/jiveapps/client.rb', line 143

def remove_collaborator(app_name, username)
  delete("/apps/#{escape(app_name)}/collaborators/#{escape(username)}").to_s
end

#remove_key(name) ⇒ Object



100
101
102
# File 'lib/jiveapps/client.rb', line 100

def remove_key(name)
  delete("/ssh_keys/#{escape(name)}").to_s
end

#remove_oauth_service(app_name, name) ⇒ Object



120
121
122
# File 'lib/jiveapps/client.rb', line 120

def remove_oauth_service(app_name, name)
  delete("/apps/#{escape(app_name)}/oauth_services/#{escape(name)}")
end

#resource(uri) ⇒ Object



224
225
226
227
228
229
230
231
# File 'lib/jiveapps/client.rb', line 224

def resource(uri)
  RestClient.proxy = ENV['HTTP_PROXY'] || ENV['http_proxy']
  if uri =~ /^http?/
    RestClient::Resource.new(uri, user, password)
  else
    RestClient::Resource.new(host, user, password)[uri]
  end
end

#versionObject

General



149
150
151
# File 'lib/jiveapps/client.rb', line 149

def version
  get("/gem_version").to_s
end