Class: Glass::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/glass/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/glass/client.rb', line 18

def initialize(opts)
  setup_google_api_keys
  initialize_google_client
  self. = opts[:google_account]

  self.access_token = opts[:access_token] || .try(:token)
  self.refresh_token = opts[:refresh_token] || .try(:refresh_token)
  self.has_expired_token = opts[:has_expired_token] || .has_expired_token?

  setup_with_our_access_tokens
  setup_with_user_access_token
  self
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



6
7
8
# File 'lib/glass/client.rb', line 6

def access_token
  @access_token
end

#api_keysObject

Returns the value of attribute api_keys.



6
7
8
# File 'lib/glass/client.rb', line 6

def api_keys
  @api_keys
end

#callback_urlObject



44
45
46
47
48
49
50
# File 'lib/glass/client.rb', line 44

def callback_url
  if ::Rails.env.production?
    ::Rails.application.routes.url_helpers.glass_notifications_callback_url(protocol: 'https')
  else
    ::Glass::DEVELOPMENT_PROXY_URL + ::Glass.dev_callback_url + "/glass/notifications"
  end
end

#contentObject

Returns the value of attribute content.



6
7
8
# File 'lib/glass/client.rb', line 6

def content
  @content
end

#google_accountObject

Returns the value of attribute google_account.



6
7
8
# File 'lib/glass/client.rb', line 6

def 
  @google_account
end

#google_clientObject

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_tokenObject

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_apiObject

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_typeObject

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_tokenObject

Returns the value of attribute refresh_token.



6
7
8
# File 'lib/glass/client.rb', line 6

def refresh_token
  @refresh_token
end

#timeline_itemObject

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



118
119
120
# File 'lib/glass/client.rb', line 118

def timeline_list
  @timeline_list
end

Class Method Details

.create(timeline_item, opts = {}) ⇒ Object



12
13
14
15
16
# File 'lib/glass/client.rb', line 12

def self.create(timeline_item, opts={})
  client = new(opts.merge({google_account: timeline_item.}))
  client.set_timeline_item(timeline_item)
  client
end

Instance Method Details

#cached_list(opts = {as_hash: true}) ⇒ Object



123
124
125
126
# File 'lib/glass/client.rb', line 123

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



159
160
161
162
163
# File 'lib/glass/client.rb', line 159

def delete(options={})
  deleting_content = { api_method: mirror_api.timeline.delete,
                       parameters: options }
  google_client.execute(deleting_content)
end

#get(id) ⇒ Object



94
95
96
# File 'lib/glass/client.rb', line 94

def get(id)
  self.google_client.execute(get_timeline_item_parameters(id))
end

#get_location(id = 'latest') ⇒ Object



61
62
63
# File 'lib/glass/client.rb', line 61

def get_location(id='latest')
  response_hash(self.google_client.execute(get_location_parameters(id)).response)
end

#get_location_parameters(id) ⇒ Object



65
66
67
68
69
# File 'lib/glass/client.rb', line 65

def get_location_parameters(id)
  { api_method: self.mirror_api.locations.get,
    parameters: { "id" => id}
  }
end

#get_timeline_item(id) ⇒ Object



32
33
34
# File 'lib/glass/client.rb', line 32

def get_timeline_item(id)
  response_hash(self.google_client.execute(get_timeline_item_parameters(id)).response)
end

#get_timeline_item_parameters(id) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/glass/client.rb', line 36

def get_timeline_item_parameters(id)
  { api_method: self.mirror_api.timeline.get,
    parameters: {
      "id" => id
    }
  }
end

#insert(options = {}) ⇒ Object



98
99
100
# File 'lib/glass/client.rb', line 98

def insert(options={})
  google_client.execute(rest_action(options, "insert"))
end

#json_content(options, api_method = "insert") ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/glass/client.rb', line 71

def json_content(options, api_method="insert")
  if c = options[:content]
    data = c.is_a?(String) ? {text: c} : c
  else
    data = self.timeline_item.to_json.merge(options)
  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

developers.google.com/glass/v1/reference/timeline/list



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/glass/client.rb', line 135

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



102
103
104
105
106
107
# File 'lib/glass/client.rb', line 102

def patch(options={})
  glass_item_id = options.delete(:glass_item_id)
  patch_action = rest_action(options, "patch").merge(parameters: {id: glass_item_id})
  puts patch_action
  google_client.execute(patch_action)
end

#response_hash(google_response) ⇒ Object



165
166
167
# File 'lib/glass/client.rb', line 165

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.



88
89
90
91
92
# File 'lib/glass/client.rb', line 88

def rest_action(options, action="insert")
  body_object = json_content(options, action)
  inserting_content = { api_method: mirror_api.timeline.send(action),
                        body_object: body_object}
end

#set_timeline_item(timeline_object) ⇒ Object



56
57
58
59
# File 'lib/glass/client.rb', line 56

def set_timeline_item(timeline_object)
  self.timeline_item = timeline_object
  self
end

#text_content(text, api_method = "insert") ⇒ Object



81
82
83
# File 'lib/glass/client.rb', line 81

def text_content(text, api_method="insert")
  mirror_api.timeline.send(api_method).request_schema.new({text: text})
end

#update(timeline_item, options = {}) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/glass/client.rb', line 109

def update(timeline_item, options={})
  glass_item_id = options.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