Class: GoogleR

Inherits:
Object
  • 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

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

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#oauth2_tokenObject (readonly)

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



80
81
82
83
# File 'lib/google_r.rb', line 80

def calendars(params = {})
  jsons = fetch_json(GoogleR::Calendar, GoogleR::Calendar.url, GoogleR::Calendar.path, params)
  jsons.map { |e| GoogleR::Calendar.from_json(e) }.flatten
end

#connection(url) ⇒ Object



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

def connection(url)
  Faraday.new(:url => url, :ssl => {:verify => false})
end

#contacts(params = {}) ⇒ Object



65
66
67
# File 'lib/google_r.rb', line 65

def contacts(params = {})
  fetch_legacy_xml(GoogleR::Contact, GoogleR::Contact.url, GoogleR::Contact.path, params)
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.api_headers)
  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.api_headers)
  response.status
end

#events(calendar, params = {}) ⇒ Object



73
74
75
76
77
78
# File 'lib/google_r.rb', line 73

def events(calendar, params = {})
  calendar_google_id = calendar.google_id.split("/").last
  jsons = fetch_json(GoogleR::Event, GoogleR::Event.url, GoogleR::Event.path(calendar_google_id), params)
  event = GoogleR::Event.new(calendar)
  jsons.map { |e| GoogleR::Event.from_json(e, event) }.flatten
end

#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.api_headers)
  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.api_headers)
    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.headers['Content-Type'] = 'application/atom+xml'
      req.headers['Authorization'] = "OAuth #{oauth2_token}"
      req.headers['GData-Version'] = '3.0'
    end
    if response.status == 200
      case response.headers["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



69
70
71
# File 'lib/google_r.rb', line 69

def groups(params = {})
  fetch_legacy_xml(GoogleR::Group, GoogleR::Group.url, GoogleR::Group.path, params)
end

#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, headers)
  params = Faraday::Utils.build_query(params)
  path = path + "?" + params unless params == ""
  response = connection(url).send(http_method, path) do |req|
    req.headers['Authorization'] = "OAuth #{oauth2_token}"
    headers.each do |header, value|
      req.headers[header] = 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.headers["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_accessObject



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

#tokenObject



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.api_headers)
    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.api_headers)
  if response.status == 200
    parse_response(response, object)
  else
    raise GoogleR::Error.new(response.status, response.body)
  end
end