Class: KenaiTools::KenaiClient

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

Constant Summary collapse

DEFAULT_HOST =
'https://kenai.com/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = nil, opts = {}) ⇒ KenaiClient

Returns a new instance of KenaiClient.



15
16
17
18
19
# File 'lib/kenai_tools/kenai_client.rb', line 15

def initialize(host = nil, opts = {})
  RestClient.log = opts[:log] if opts[:log]
  @host = host || DEFAULT_HOST
  @opts = opts
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



13
14
15
# File 'lib/kenai_tools/kenai_client.rb', line 13

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



13
14
15
# File 'lib/kenai_tools/kenai_client.rb', line 13

def password
  @password
end

#userObject (readonly)

Returns the value of attribute user.



13
14
15
# File 'lib/kenai_tools/kenai_client.rb', line 13

def user
  @user
end

Instance Method Details

#api_client(fragment = '') ⇒ Object Also known as: []



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/kenai_tools/kenai_client.rb', line 161

def api_client(fragment='')
  params = {:headers => {:accept => 'application/json'}}
  if @auth
    params[:user] = @user
    params[:password] = @password
  end
  params.merge!(@opts)

  if fragment =~ %r{^https://}
    RestClient::Resource.new(fragment, params)
  else
    RestClient::Resource.new(@host, params)['api'][fragment]
  end
end

#authenticate(user, password) ⇒ Object

check credentials using the login/authenticate method; if successful, cache the credentials for future calls



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

def authenticate(user, password)
  @user = user
  @password = password
  begin
    client = self['login/authenticate']
    client["?username=#{@user}&password=#{@password}"].get
    @auth = true
  rescue RestClient::Unauthorized, RestClient::RequestFailed
    @auth = false
    @user = @password = nil
  end

  return @auth
end

#authenticated?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/kenai_tools/kenai_client.rb', line 38

def authenticated?
  @auth
end

#create_or_update_wiki_image(proj_name, opts) ⇒ Object

opts has the following keys :image_data = raw image data, required only if creating a new image :content_type = image_data content-type :filename = filename for multipart

:comments = optional comments for the image throws IOError unless create or update was successful



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/kenai_tools/kenai_client.rb', line 103

def create_or_update_wiki_image(proj_name, opts)
  req_params = {}
  if data = opts[:image_data]
    content_type = opts[:content_type]
    filename = opts[:filename]
    req_params["image[uploaded_data]"] = UploadIO.new(StringIO.new(data), content_type, filename)
  end
  if comments = opts[:comments]
    req_params["image[comments]"] = comments
  end
  return false if req_params.empty?

  self["projects/#{proj_name}/features/wiki/images/#{filename}"].put(req_params)
end

#create_project_feature(proj_name, feature_json) ⇒ Object



66
67
68
# File 'lib/kenai_tools/kenai_client.rb', line 66

def create_project_feature(proj_name, feature_json)
  self["projects/#{proj_name}/features"].post(feature_json, :content_type => :json, :accept => :json)
end

#delete_project_feature(proj_name, feature_name) ⇒ Object



70
71
72
# File 'lib/kenai_tools/kenai_client.rb', line 70

def delete_project_feature(proj_name, feature_name)
  project_feature_client(proj_name, feature_name).delete
end

#edit_wiki_page(proj_name, page_name) ⇒ Object

edit a single wiki page – yields the current page contents, and saves them back if the result of the block is different



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/kenai_tools/kenai_client.rb', line 132

def edit_wiki_page(proj_name, page_name)
  # fetch current page contents
  page = wiki_page_client(proj_name, page_name)
  begin
    page_data = JSON.parse(page.get)
    current_src = page_data['text']
  rescue RestClient::ResourceNotFound
    page_data = {}
    current_src = ''
  end

  new_src = yield(current_src)

  changed = !(new_src.nil? || new_src == current_src)

  if changed
    new_data = {
      'page' => {
        'text' => new_src,
        'description' => 'edited with kenai-client',
        'number' => page_data['number']
      }
    }
    page.put(JSON.dump(new_data), :content_type => 'application/json')
  end

  return changed
end

#my_projectsObject



80
81
82
# File 'lib/kenai_tools/kenai_client.rb', line 80

def my_projects
  fetch_all('projects/mine', 'projects')
end

#project(proj_name) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/kenai_tools/kenai_client.rb', line 42

def project(proj_name)
  begin
    JSON.parse(self["projects/#{proj_name}"].get)
  rescue RestClient::ResourceNotFound
    nil
  end
end

#project_feature(proj_name, feature_name) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/kenai_tools/kenai_client.rb', line 58

def project_feature(proj_name, feature_name)
  begin
    JSON.parse(project_feature_client(proj_name, feature_name).get)
  rescue RestClient::ResourceNotFound
    nil
  end
end

#project_features(proj_name) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/kenai_tools/kenai_client.rb', line 50

def project_features(proj_name)
  begin
    fetch_all("projects/#{proj_name}/features", 'features')
  rescue RestClient::ResourceNotFound
    nil
  end
end

#projects(params = {}) ⇒ Object

collect all project hashes (scope may be :all, or all projects, or :mine, for projects in which the current user has some role)



76
77
78
# File 'lib/kenai_tools/kenai_client.rb', line 76

def projects(params = {})
  fetch_all('projects', 'projects', params)
end

#wiki_image_data(image) ⇒ Object

get the wiki raw image data for an image



92
93
94
# File 'lib/kenai_tools/kenai_client.rb', line 92

def wiki_image_data(image)
  RestClient.get(image['image_url'], :accept => image['image_content_type'])
end

#wiki_images(project, on_page = nil) ⇒ Object

get wiki images for a project



85
86
87
88
89
# File 'lib/kenai_tools/kenai_client.rb', line 85

def wiki_images(project, on_page = nil)
  opts = {}
  opts[:page] = on_page if on_page
  fetch_all("projects/#{project}/features/wiki/images", 'images', opts)
end

#wiki_page(proj_name, page_name) ⇒ Object



125
126
127
128
# File 'lib/kenai_tools/kenai_client.rb', line 125

def wiki_page(proj_name, page_name)
  page = wiki_page_client(proj_name, page_name)
  JSON.parse(page.get)
end

#wiki_pages(project, on_page = nil) ⇒ Object

get wiki pages for a project



119
120
121
122
123
# File 'lib/kenai_tools/kenai_client.rb', line 119

def wiki_pages(project, on_page = nil)
  opts = {}
  opts[:page] = on_page if on_page
  fetch_all("projects/#{project}/features/wiki/pages", 'pages', opts)
end