Class: ProximityBeacon::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/proximity_beacon/client.rb,
lib/proximity_beacon/client/beacons.rb,
lib/proximity_beacon/client/request.rb,
lib/proximity_beacon/client/projects.rb,
lib/proximity_beacon/client/attachments.rb

Defined Under Namespace

Classes: Attachments, Beacons, Projects, Request, RequestError

Constant Summary collapse

PROXIMITY_BEACON_ROOT =
"https://proximitybeacon.googleapis.com/v1beta1/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials = nil) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/proximity_beacon/client.rb', line 8

def initialize(credentials = nil)
  if credentials.nil?
    # if no credentials are provided, we try to load them from file or
    # initiate a CLI based oauth flow
    self.credentials = Credentials.from_file || OAuth.new.get_credentials
    if self.credentials.expired? && self.credentials.refresh_token
      self.credentials = OAuth.new.refresh_credentials(self.credentials)
    end
    self.credentials.save
  else
    self.credentials = credentials
  end
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



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

def credentials
  @credentials
end

Instance Method Details

#attachmentsObject



36
37
38
# File 'lib/proximity_beacon/client.rb', line 36

def attachments
  Attachments.new(credentials)
end

#beaconsObject



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

def beacons
  Beacons.new(credentials)
end

#eidparamsObject



22
23
24
25
26
# File 'lib/proximity_beacon/client.rb', line 22

def eidparams
  uri = URI(PROXIMITY_BEACON_ROOT + "eidparams")
  response = Request.get(uri, credentials)
  return JSON.parse(response.body)
end

#getforobserved(eids, api_key = ENV["GOOGLE_API_KEY"]) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/proximity_beacon/client.rb', line 40

def getforobserved(eids, api_key = ENV["GOOGLE_API_KEY"])
  uri = URI("#{PROXIMITY_BEACON_ROOT}beaconinfo:getforobserved?key=#{api_key}")
  response = Request.post(uri) {|request|
    observations = Array(eids).map {|eid|
      {advertisedId: {type: "EDDYSTONE_EID", id: base64_eid(eid)}}
    }
    request.body = {
      observations: observations,
      namespacedTypes: "*",
    }.to_json
    request.add_field "Content-Type", "application/json"
  }
  JSON.parse(response.body)
end

#projectsObject



28
29
30
# File 'lib/proximity_beacon/client.rb', line 28

def projects
  Projects.new(credentials)
end