Class: CasualHelper::Api

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

Overview

CasualHelper::Api makes it easier to use the Casual Cloud REST API

Usage

api = CasualHelper::Api.new 'http://localhost:9393'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_server) ⇒ Api

Returns a new instance of Api.



34
35
36
# File 'lib/casual_helper.rb', line 34

def initialize(api_server)
  @api_server = api_server
end

Instance Attribute Details

#api_serverObject (readonly)

Returns the value of attribute api_server.



38
39
40
# File 'lib/casual_helper.rb', line 38

def api_server
  @api_server
end

Class Method Details

.validate_env_variables(env_vars) ⇒ Object

return true when all environment variables are set



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

def self.validate_env_variables(env_vars)
  p "Enivonrment variables at runtime"
  env_vars.all? {|env_var|
    if ENV.has_key? env_var
      p "  #{env_var}=#{ENV[env_var]}"
      true
    else
      p "  #{env_var} is not set! The application will not start."
      false
    end
  }
end

Instance Method Details

#add_comment_tap(tap_id, comment_string) ⇒ Object



163
164
165
166
# File 'lib/casual_helper.rb', line 163

def add_comment_tap(tap_id, comment_string)
  return if are_invalid tap_id, comment_string
  post "#{@api_server.to_s}/v0/taps/#{tap_id}/update", { :feedback => { :comments => [comment_string] } }
end

#add_drop(tap_id, options) ⇒ Object



115
116
117
118
# File 'lib/casual_helper.rb', line 115

def add_drop(tap_id, options)
  return if are_invalid tap_id, options
  post "#{@api_server.to_s}/v0/taps/#{tap_id.to_s}/drops", options
end

#add_tap(options) ⇒ Object



110
111
112
113
# File 'lib/casual_helper.rb', line 110

def add_tap(options)
  return if are_invalid options
  post "#{@api_server.to_s}/v0/taps", options
end

#auth_user(auth) ⇒ Object

authenticates the user with the API and returns their Casual profile



121
122
123
124
125
126
127
128
129
# File 'lib/casual_helper.rb', line 121

def auth_user(auth)
  return if are_invalid auth
  # note - differences from 'post': (1) no .to_json (2) return response hash
  response_json = RestClient.post "#{@api_server.to_s}/v0/users/authenticate", auth, JSON_CONTENT
  response = JSON.parse(response_json)
  response['response']
rescue => e
  log_error url, e
end

#get_config(id) ⇒ Object

Returns hash with attributes passed in and readable_id.

Returns:

  • hash with attributes passed in and readable_id



169
170
171
172
# File 'lib/casual_helper.rb', line 169

def get_config(id)
  return if are_invalid id
  get "#{@api_server.to_s}/v0/configs/#{id}"
end

#get_drop(tap_id, drop_id) ⇒ Object

Description

Fetch the drop with the specified IDs

Example:

tap = api.get_tap('0123456789abcdef01234567', 5) #=> {"id" => 5, "tap_id" => "7654321fedcba9876543210", ...}

Returns nil on error.

Parameters:

  • tap_id

    string containing the mongo ID of an existing tap

  • drop_id

    string containing the mongo ID of an existing drop



104
105
106
107
108
# File 'lib/casual_helper.rb', line 104

def get_drop(tap_id, drop_id)
  #return if are_invalid_mongo_key tap_id
  return if are_invalid drop_id
  get "#{@api_server.to_s}/v0/taps/#{tap_id}/drops/#{drop_id.to_i}"
end

#get_tap(tap_id) ⇒ Object

Description

Fetch the tap with the specified ID

will be returned as a hash

Example:

tap = api.get_tap('0123456789abcdef01234567') #=> {"id" => "0123456789abcdef01234567", ...}

Returns nil on error.

Parameters:

  • tap_id

    If valid mongo ID string for an existing tap, the tap



85
86
87
88
# File 'lib/casual_helper.rb', line 85

def get_tap(tap_id)
  #return if are_invalid_mongo_key tap_id
  get "#{@api_server.to_s}/v0/taps/#{tap_id}"
end

#get_tapsObject

Description

Fetch all recent taps from the Casual Cloud API server

Example:

drops = api.get_taps #=> [{"id" => "0123456789abcdef01234567", ...}, ...]

Returns nil on error.

TODO - add limit parameter



67
68
69
# File 'lib/casual_helper.rb', line 67

def get_taps
  get "#{@api_server.to_s}/v0/taps"
end

#get_user(id) ⇒ Object



131
132
133
134
# File 'lib/casual_helper.rb', line 131

def get_user(id)
  #return if are_invalid_mongo_key id
  get "#{@api_server.to_s}/v0/users/#{id}"
end

#lookup_alias(element_alias) ⇒ Object



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

def lookup_alias(element_alias)
  return if are_invalid element_alias
  get "#{@api_server.to_s}/v0/aliases/#{element_alias}"
end

#put_config(id, config) ⇒ Object

Parameters:

  • config

    is a hash



175
176
177
178
# File 'lib/casual_helper.rb', line 175

def put_config(id, config)
  return if are_invalid id, config
  put "#{@api_server.to_s}/v0/configs/#{id}", config
end

#signup_email(email_address) ⇒ Object

add email address to signup list returns true if successful



138
139
140
141
# File 'lib/casual_helper.rb', line 138

def (email_address)
  return if are_invalid email
  post "#{@api_server.to_s}/v0/register", { :address => email_address.to_s }
end

#update_drop_feedback(tap_id, drop_id, update) ⇒ Object



143
144
145
146
# File 'lib/casual_helper.rb', line 143

def update_drop_feedback(tap_id, drop_id, update)
  return if are_invalid tap_id, drop_id, update
  post "#{@api_server.to_s}/v0/taps/#{tap_id}/drops/#{drop_id}/update", update
end

#update_location(drop_uri, longitude, latitude) ⇒ Object



153
154
155
156
# File 'lib/casual_helper.rb', line 153

def update_location(drop_uri, longitude, latitude)
  return if are_invalid drop_uri, longitude, latitude
  put drop_uri, { :location => [longitude, latitude] }
end

#update_user(user_id, body) ⇒ Object



158
159
160
161
# File 'lib/casual_helper.rb', line 158

def update_user(user_id, body)
  return if are_invalid user_id, body
  put "#{@api_server.to_s}/v0/users/#{user_id}", body
end