Skip to content

Commit 1b8361e

Browse files
author
Stan Hu
committed
Improve API translation
1 parent 6650421 commit 1b8361e

5 files changed

Lines changed: 23 additions & 18 deletions

File tree

lib/ghee.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def self.access_token(token, api_url = nil)
7373

7474
def self.private_token(token, api_url = nil)
7575
options = { :private_token => token }
76+
options[:enable_url_escape] = false
7677
options[:api_url] = api_url if api_url
7778
Ghee.new options
7879
end

lib/ghee/api/gitlab/issues.rb

Whitespace-only changes.

lib/ghee/api/gitlab/repos.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ class Proxy < ::Ghee::ResourceProxy
2727
def repos(login, name = nil)
2828
repo = name.nil? ? '' : "#{login}%2F#{name}"
2929
path_prefix = "./projects/#{repo}"
30-
proxy = Proxy.new(connection, path_prefix, { escape_path: false } )
30+
proxy = Proxy.new(connection, path_prefix, self)
3131
proxy.repo_name = repo
32-
translate_data(proxy)
32+
proxy
3333
end
3434

3535
def translate_data(data)
36-
puts data.inspect
3736
data['full_name'] = data['path_with_namespace']
3837
data['private'] = !data['public']
3938
data['ssh_url'] = data['ssh_url_to_repo']
@@ -55,7 +54,6 @@ def translate_data(data)
5554
trees_url updated_at url watchers watchers_count)
5655
data.select { |key| allowed_keys.include? key }
5756
end
58-
5957
end
6058
end
6159
end

lib/ghee/connection.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Ghee
66
class Connection < Faraday::Connection
7-
attr_reader :hash
7+
attr_reader :hash, :enable_url_escape
88

99
def parallel_connection(adapter=:typhoeus)
1010
conn = self.class.new @hash
@@ -22,6 +22,7 @@ def parallel_connection(adapter=:typhoeus)
2222
# :basic_auth => {:user_name => "octocat", :password => "secret"}
2323
def initialize(hash={})
2424
@hash = hash
25+
@enable_url_escape = hash.fetch(:enable_url_escape, true)
2526
private_token = hash[:private_token] if hash.has_key?:private_token
2627
access_token = hash[:access_token] if hash.has_key?:access_token
2728
basic_auth = hash[:basic_auth] if hash.has_key?:basic_auth

lib/ghee/resource_proxy.rb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ResourceProxy
1313
include Ghee::CUD
1414

1515
# Make connection and path_prefix readable
16-
attr_reader :connection, :path_prefix, :params, :id
16+
attr_reader :connection, :path_prefix, :api_translator, :params, :id
1717

1818
# Expose pagination data
1919
attr_reader :current_page, :total, :pagination
@@ -30,15 +30,14 @@ def self.accept_header(header)
3030
# connection - Ghee::Connection object
3131
# path_prefix - String
3232
#
33-
def initialize(connection, path_prefix, params = {}, &block)
33+
def initialize(connection, path_prefix, api_translator = nil, params = {}, &block)
3434
if !params.is_a?Hash
3535
@id = params
3636
params = {}
3737
end
3838

39-
escape_path = params.fetch(:escape_path, true)
40-
@connection, @path_prefix, @params = connection, path_prefix, params
41-
@path_prefix = URI.escape(@path_prefix) if escape_path
39+
@connection, @path_prefix, @api_translator, @params = connection, path_prefix, api_translator, params
40+
@path_prefix = URI.escape(@path_prefix) if connection.enable_url_escape
4241
@block = block if block
4342
subject if block
4443
end
@@ -68,18 +67,24 @@ def raw
6867
# Returns json
6968
#
7069
def subject
71-
@subject ||= connection.get(path_prefix) do |req|
72-
req.params.merge!params
70+
@subject ||= connection.get(path_prefix) do |req|
71+
req.params.merge!params
7372
req.headers["Accept"] = accept_type if self.respond_to? :accept_type
7473
@block.call(req)if @block
75-
end.body
74+
end
75+
76+
if @api_translator
77+
@api_translator.translate_data(@subject.body)
78+
else
79+
@subject.body
80+
end
7681
end
7782

7883
# Paginate is a helper method to handle
7984
# request pagination to the github api
8085
#
8186
# options - Hash containing pagination params
82-
# eg;
87+
# eg;
8388
# :per_page => 100, :page => 1
8489
#
8590
# Returns self
@@ -100,7 +105,7 @@ def paginate(options)
100105

101106
parse_link_header response.headers.delete("link")
102107

103-
return self
108+
return self
104109
end
105110

106111
def all
@@ -125,7 +130,7 @@ def all_parallel
125130
end
126131
end
127132
end
128-
requests.inject([]) do |results, page|
133+
requests.inject([]) do |results, page|
129134
results.concat(page.body)
130135
end
131136
end
@@ -138,11 +143,11 @@ def all_parallel
138143
end
139144

140145
def build_prefix(first_argument, endpoint)
141-
(!first_argument.is_a?(Hash) && !first_argument.nil?) ?
146+
(!first_argument.is_a?(Hash) && !first_argument.nil?) ?
142147
File.join(path_prefix, "/#{endpoint}/#{first_argument}") : File.join(path_prefix, "/#{endpoint}")
143148
end
144149

145-
private
150+
private
146151

147152
def pagination_data(header)
148153
parse_link_header header

0 commit comments

Comments
 (0)