Class: GoogleR
- Inherits:
-
Object
show all
- Defined in:
- lib/google_r.rb,
lib/google_r/version.rb,
lib/google_r/exceptions.rb
Defined Under Namespace
Classes: Calendar, Contact, Error, Event, Group, Token
Constant Summary
collapse
- VERSION =
"0.2.0"
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#calendars(params = {}) ⇒ Object
-
#connection(url) ⇒ Object
-
#contacts(params = {}) ⇒ Object
-
#create(object, params = {}) ⇒ Object
-
#delete(object, params = {}) ⇒ Object
-
#events(calendar, params = {}) ⇒ Object
-
#fetch(object, params = {}) ⇒ Object
-
#fetch_json(klass, url, path, params) ⇒ Object
-
#fetch_legacy_xml(klass, url, path, params = {}) ⇒ Object
-
#groups(params = {}) ⇒ Object
-
#initialize(oauth2_token) ⇒ GoogleR
constructor
A new instance of GoogleR.
-
#make_request(http_method, url, path, params, body, headers) ⇒ Object
-
#parse_legacy_xml_response(body, klass) ⇒ Object
-
#parse_response(response, object) ⇒ Object
-
#save(object, params = {}) ⇒ Object
-
#test_access ⇒ Object
-
#token ⇒ Object
-
#update(object, params = {}) ⇒ Object
Constructor Details
#initialize(oauth2_token) ⇒ GoogleR
Returns a new instance of GoogleR.
19
20
21
22
23
|
# File 'lib/google_r.rb', line 19
def initialize(oauth2_token)
@oauth2_token = oauth2_token
self.logger = Logger.new("/dev/null")
self.logger.formatter = Logger::Formatter.new
end
|
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
17
18
19
|
# File 'lib/google_r.rb', line 17
def logger
@logger
end
|
#oauth2_token ⇒ Object
Returns the value of attribute oauth2_token.
16
17
18
|
# File 'lib/google_r.rb', line 16
def oauth2_token
@oauth2_token
end
|
Instance Method Details
#calendars(params = {}) ⇒ Object
#connection(url) ⇒ Object
149
150
151
|
# File 'lib/google_r.rb', line 149
def connection(url)
Faraday.new(:url => url, :ssl => {:verify => false})
end
|
#create(object, params = {}) ⇒ Object
42
43
44
45
46
47
48
49
|
# File 'lib/google_r.rb', line 42
def create(object, params = {})
response = make_request(:post, object.class.url, object.path, params, object.to_google, object.class.)
if response.status == 200 || response.status == 201
parse_response(response, object)
else
raise GoogleR::Error.new(response.status, response.body)
end
end
|
#delete(object, params = {}) ⇒ Object
60
61
62
63
|
# File 'lib/google_r.rb', line 60
def delete(object, params = {})
response = make_request(:delete, object.class.url, object.path, params, nil, object.class.)
response.status
end
|
#events(calendar, params = {}) ⇒ Object
#fetch(object, params = {}) ⇒ Object
25
26
27
28
29
30
31
32
|
# File 'lib/google_r.rb', line 25
def fetch(object, params = {})
response = make_request(:get, object.class.url, object.path, params, nil, object.class.)
if response.status == 200
parse_response(response, object)
else
raise GoogleR::Error.new(response.status, response.body)
end
end
|
#fetch_json(klass, url, path, params) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/google_r.rb', line 85
def fetch_json(klass, url, path, params)
max_results = 500
params.merge!({"maxResults" => max_results})
elements = []
next_page_token = nil
begin
response = make_request(:get, url, path, params, nil, klass.)
if response.status == 200
parsed = Yajl::Parser.parse(response.body)
elements.concat(parsed["items"] || [])
next_page_token = parsed["nextPageToken"]
params.merge!({"pageToken" => next_page_token})
else
raise GoogleR::Error.new(response.status, response.body)
end
end while !next_page_token.nil?
elements
end
|
#fetch_legacy_xml(klass, url, path, params = {}) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/google_r.rb', line 108
def fetch_legacy_xml(klass, url, path, params = {})
current_count = 0
per_page = 500
results = []
start_index = 1
connection = connection(url)
begin
query_params = params.merge({
:"max-results" => per_page,
:"start-index" => start_index,
})
response = connection.get(path + "?" + Faraday::Utils.build_query(query_params)) do |req|
req.['Content-Type'] = 'application/atom+xml'
req.['Authorization'] = "OAuth #{oauth2_token}"
req.['GData-Version'] = '3.0'
end
if response.status == 200
case response.["Content-Type"]
when /xml/
entries = parse_legacy_xml_response(response.body, klass)
else
raise "Not implemented"
end
current_count = entries.size
next if current_count == 0
results += entries
start_index += current_count
else
raise GoogleR::Error.new(response.status, response.body)
end
end while current_count == per_page
results
end
|
#groups(params = {}) ⇒ Object
#make_request(http_method, url, path, params, body, headers) ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/google_r.rb', line 153
def make_request(http_method, url, path, params, body, )
params = Faraday::Utils.build_query(params)
path = path + "?" + params unless params == ""
response = connection(url).send(http_method, path) do |req|
req.['Authorization'] = "OAuth #{oauth2_token}"
.each do |, value|
req.[] = value
end
req.body = body
self.logger.debug "#{http_method} #{url}/#{path}"
end
end
|
#parse_legacy_xml_response(body, klass) ⇒ Object
143
144
145
146
147
|
# File 'lib/google_r.rb', line 143
def parse_legacy_xml_response(body, klass)
doc = Nokogiri::XML.parse(body)
doc.remove_namespaces!
klass.from_xml(doc)
end
|
#parse_response(response, object) ⇒ Object
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/google_r.rb', line 166
def parse_response(response, object)
case response.["Content-Type"]
when /json/
object.class.from_json(Yajl::Parser.parse(response.body), object)
when /xml/
doc = Nokogiri::XML.parse(response.body)
doc.remove_namespaces!
object.class.from_xml(doc.root, object)
else
raise "Cannot deserialize"
end
end
|
#save(object, params = {}) ⇒ Object
34
35
36
37
38
39
40
|
# File 'lib/google_r.rb', line 34
def save(object, params = {})
if object.new?
create(object, params)
else
update(object, params)
end
end
|
#test_access ⇒ Object
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/google_r.rb', line 191
def test_access
response = self.make_request(:get, "https://www.google.com", "/m8/feeds/contacts/default/full", {"max-results" => 0}, nil, {})
if response.status == 401
false
elsif response.status == 200
return true
else
raise GoogleR::Error.new(response.status, response.body)
end
end
|
#token ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/google_r.rb', line 179
def token
begin
token = GoogleR::Token.new(oauth2_token)
response = make_request(:post, GoogleR::Token.url, token.path, {:access_token => oauth2_token}, nil, GoogleR::Token.)
if response.status == 200
GoogleR::Token.from_json(Yajl::Parser.parse(response.body), oauth2_token)
else
nil
end
end
end
|
#update(object, params = {}) ⇒ Object
51
52
53
54
55
56
57
58
|
# File 'lib/google_r.rb', line 51
def update(object, params = {})
response = make_request(:patch, object.class.url, object.path, params, object.to_google, object.class.)
if response.status == 200
parse_response(response, object)
else
raise GoogleR::Error.new(response.status, response.body)
end
end
|