Class: Glass::Client
- Inherits:
-
Object
- Object
- Glass::Client
- Defined in:
- lib/glass/client.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#api_keys ⇒ Object
Returns the value of attribute api_keys.
-
#content ⇒ Object
Returns the value of attribute content.
-
#google_account ⇒ Object
Returns the value of attribute google_account.
-
#google_client ⇒ Object
Returns the value of attribute google_client.
-
#has_expired_token ⇒ Object
Returns the value of attribute has_expired_token.
-
#mirror_api ⇒ Object
Returns the value of attribute mirror_api.
-
#mirror_content_type ⇒ Object
Returns the value of attribute mirror_content_type.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#timeline_item ⇒ Object
Returns the value of attribute timeline_item.
-
#timeline_list(opts = {as_hash: true}) ⇒ Object
deprecated: please use cached_list instead.
Class Method Summary collapse
Instance Method Summary collapse
- #cached_list(opts = {as_hash: true}) ⇒ Object
- #delete(options = {}) ⇒ Object
- #get(id) ⇒ Object
- #get_timeline_item(id) ⇒ Object
- #get_timeline_item_parameters(id) ⇒ Object
-
#initialize(opts) ⇒ Client
constructor
A new instance of Client.
- #insert(options = {}) ⇒ Object
- #json_content(options, api_method = "insert") ⇒ Object
-
#list(opts = {as_hash: true}) ⇒ Object
this method is pretty much extracted directly from the mirror API code samples in ruby.
- #patch(options = {}) ⇒ Object
- #response_hash(google_response) ⇒ Object
-
#rest_action(options, action = "insert") ⇒ Object
optional parameter is merged into the content hash before sending.
- #set_timeline_item(timeline_object) ⇒ Object
- #text_content(text, api_method = "insert") ⇒ Object
- #update(timeline_item, options = {}) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 |
# File 'lib/glass/client.rb', line 17 def initialize(opts) setup_google_api_keys initialize_google_client self.google_account = opts[:google_account] setup_with_our_access_tokens setup_with_user_access_token self end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
6 7 8 |
# File 'lib/glass/client.rb', line 6 def access_token @access_token end |
#api_keys ⇒ Object
Returns the value of attribute api_keys.
6 7 8 |
# File 'lib/glass/client.rb', line 6 def api_keys @api_keys end |
#content ⇒ Object
Returns the value of attribute content.
6 7 8 |
# File 'lib/glass/client.rb', line 6 def content @content end |
#google_account ⇒ Object
Returns the value of attribute google_account.
6 7 8 |
# File 'lib/glass/client.rb', line 6 def google_account @google_account end |
#google_client ⇒ Object
Returns the value of attribute google_client.
6 7 8 |
# File 'lib/glass/client.rb', line 6 def google_client @google_client end |
#has_expired_token ⇒ Object
Returns the value of attribute has_expired_token.
6 7 8 |
# File 'lib/glass/client.rb', line 6 def has_expired_token @has_expired_token end |
#mirror_api ⇒ Object
Returns the value of attribute mirror_api.
6 7 8 |
# File 'lib/glass/client.rb', line 6 def mirror_api @mirror_api end |
#mirror_content_type ⇒ Object
Returns the value of attribute mirror_content_type.
6 7 8 |
# File 'lib/glass/client.rb', line 6 def mirror_content_type @mirror_content_type end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
6 7 8 |
# File 'lib/glass/client.rb', line 6 def refresh_token @refresh_token end |
#timeline_item ⇒ Object
Returns the value of attribute timeline_item.
6 7 8 |
# File 'lib/glass/client.rb', line 6 def timeline_item @timeline_item end |
#timeline_list(opts = {as_hash: true}) ⇒ Object
deprecated: please use cached_list instead
90 91 92 |
# File 'lib/glass/client.rb', line 90 def timeline_list @timeline_list end |
Class Method Details
.create(timeline_item, opts = {}) ⇒ Object
11 12 13 14 15 |
# File 'lib/glass/client.rb', line 11 def self.create(timeline_item, opts={}) client = new(opts.merge({google_account: timeline_item.google_account})) client.set_timeline_item(timeline_item) client end |
Instance Method Details
#cached_list(opts = {as_hash: true}) ⇒ Object
95 96 97 98 |
# File 'lib/glass/client.rb', line 95 def cached_list(opts={as_hash: true}) retval = @timeline_list.nil? ? self.list(opts) : @timeline_list opts[:as_hash] ? retval.map(&:to_hash).map(&:with_indifferent_access) : retval end |
#delete(options = {}) ⇒ Object
131 132 133 134 135 |
# File 'lib/glass/client.rb', line 131 def delete(={}) deleting_content = { api_method: mirror_api.timeline.delete, parameters: } google_client.execute(deleting_content) end |
#get(id) ⇒ Object
66 67 68 |
# File 'lib/glass/client.rb', line 66 def get(id) self.google_client.execute(get_timeline_item_parameters(id)) end |
#get_timeline_item(id) ⇒ Object
26 27 28 |
# File 'lib/glass/client.rb', line 26 def get_timeline_item(id) response_hash(self.google_client.execute(get_timeline_item_parameters(id)).response) end |
#get_timeline_item_parameters(id) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/glass/client.rb', line 30 def get_timeline_item_parameters(id) { api_method: self.mirror_api.timeline.get, parameters: { "id" => id } } end |
#insert(options = {}) ⇒ Object
70 71 72 |
# File 'lib/glass/client.rb', line 70 def insert(={}) google_client.execute(rest_action(, "insert")) end |
#json_content(options, api_method = "insert") ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/glass/client.rb', line 43 def json_content(, api_method="insert") if c = [:content] data = c.is_a?(String) ? {text: c} : c else data = self.timeline_item.to_json.merge() end data = format_hash_properly(data) mirror_api.timeline.send(api_method).request_schema.new(data) end |
#list(opts = {as_hash: true}) ⇒ Object
this method is pretty much extracted directly from the mirror API code samples in ruby
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/glass/client.rb', line 107 def list(opts={as_hash: true}) page_token = nil parameters = {} self.timeline_list = [] begin parameters = {} parameters['pageToken'] = page_token if page_token.present? api_result = google_client.execute(api_method: mirror_api.timeline.list, parameters: parameters) if api_result.success? timeline_items = api_result.data page_token = nil if timeline_items.items.empty? if timeline_items.items.any? @timeline_list.concat(timeline_items.items) page_token = timeline_items.next_page_token end else puts "An error occurred: #{result.data['error']['message']}" page_token = nil end end while page_token.to_s != '' timeline_list(opts) end |
#patch(options = {}) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/glass/client.rb', line 74 def patch(={}) glass_item_id = .delete(:glass_item_id) patch_action = rest_action(, "patch").merge(parameters: {id: glass_item_id}) puts patch_action google_client.execute(patch_action) end |
#response_hash(google_response) ⇒ Object
137 138 139 |
# File 'lib/glass/client.rb', line 137 def response_hash(google_response) JSON.parse(google_response.body).with_indifferent_access end |
#rest_action(options, action = "insert") ⇒ Object
optional parameter is merged into the content hash before sending. good for specifying more application specific stuff like speakableText parameters.
60 61 62 63 64 |
# File 'lib/glass/client.rb', line 60 def rest_action(, action="insert") body_object = json_content(, action) inserting_content = { api_method: mirror_api.timeline.send(action), body_object: body_object} end |
#set_timeline_item(timeline_object) ⇒ Object
38 39 40 41 |
# File 'lib/glass/client.rb', line 38 def set_timeline_item(timeline_object) self.timeline_item = timeline_object self end |
#text_content(text, api_method = "insert") ⇒ Object
53 54 55 |
# File 'lib/glass/client.rb', line 53 def text_content(text, api_method="insert") mirror_api.timeline.send(api_method).request_schema.new({text: text}) end |
#update(timeline_item, options = {}) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/glass/client.rb', line 81 def update(timeline_item, ={}) glass_item_id = .delete(:glass_item_id) update_content = { api_method: mirror_api.timeline.update, body_object: timeline_item, parameters: {id: glass_item_id}} google_client.execute update_content end |