Class: Mmf::Client

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

Constant Summary collapse

ROOT_URI =
"https://oauth2-api.mapmyapi.com/"
VAR =
/%\{(.*?)\}/
API_MAP =
{
  # user resources
  me:                 { method: :get,  endpoint: 'v7.0/user/self' },
  deactivate:         { method: :post, endpoint: 'v7.0/user_deactivation', },
  user:               { method: :get,  endpoint: 'v7.0/user/%{user_id}', defaults: { user_id: :me } },
  create_user:        { method: :post, endpoint: 'v7.0/user' },
  update_user:        { method: :put,  endpoint: 'v7.0/user/%{user_id}', defaults: { user_id: :me } },
  user_photo:         { method: :get,  endpoint: 'v7.0/user_profile_photo/%{user_id}', defaults: { user_id: :me } },
  user_stats:         { method: :get,  endpoint: 'v7.0/user_stats/%{user_id}', defaults: { user_id: :me } },

  achievement:        { method: :get,  endpoint: 'v7.0/acievement/%{achievement_id}' },
  achievements:       { method: :get,  endpoint: 'v7.0/user_acievement', required: [:user], defaults: { user: :me } },

  # friend resources
  friends:            { method: :get,    endpoint: 'v7.0/user', required: [:friends_with] },
  suggested_friends:  { method: :get,    endpoint: 'v7.0/user', required: [:suggested_friends_for, :suggested_friends_source] },
  add_friend:         { method: :post,   endpoint: 'v7.0/frendship' },
  remove_friend:      { method: :delete, endpoint: 'v7.0/friendship/%{friendship_id}' },
  approve_friend:     { method: :put,    endpoint: 'v7.0/friendship/%{friendship_id}' },
  friend_requests:    { method: :get,    endpoint: 'v7.0/friendship', required: [:to_user], defaults: { status: 'pending' } },

  # shared resources
  activity_types:     { method: :get,  endpoint: 'v7.0/activity_type' },
  activity_type:      { method: :get,  endpoint: 'v7.0/activity_type/%{activity_type_id}' },
  privacy_options:    { method: :get,  endpoint: 'v7.0/privacy_option' },
  privacy_option:     { method: :get,  endpoint: 'v7.0/privacy_option/%{privacy_option_id}' },

  # workout resources
  add_workout:        { method: :post, endpoint: 'v7.0/workout', required: [:activity_type, :name, :start_datetime, :start_locale_timezone] },
  workouts:           { method: :get,  endpoint: 'v7.0/workout', required: [:user], defaults: { user: :me } },
  workout:            { method: :get,  endpoint: 'v7.0/workout/%{workout_id}' },

  # course resources
  search_courses:     { method: :get,  endpoint: 'v7.0/course' },
  course:             { method: :get,  endpoint: 'v7.0/course/%{course_id}' },
  # course_leaderboard: { method: :get,  endpoint: 'api/0.1/course_leaderboard/%{course_id}', required: [:activity_type_id] },
  # course_history:     { method: :get,  endpoint: '/api/0.1/course_history/%{course_id}_%{user_id}' },
  # course_map:         { method: :get,  endpoint: 'api/0.1/course_map/%{course_id}' },

  # route resources
  route:              { method: :get,    endpoint: 'v7.0/route/%{route_id}' },
  routes:             { method: :get,    endpoint: 'v7.0/route', required: [:user], defaults: { user: :me } },
  nearby_routes:      { method: :get,    endpoint: 'v7.0/route', required: [:close_to_location, :minimum_distance, :maximum_distance] },
  add_route:          { method: :post,   endpoint: 'v7.0/route' },
  update_route:       { method: :put,    endpoint: 'v7.0/route/%{route_id}' },
  remove_route:       { method: :delete, endpoint: 'v7.0/route/%{route_id}' },
  bookmarks:          { method: :get,    endpoint: 'v7.0/route_bookmark', required: [:user], defaults: { user: :me } },
  add_bookmark:       { method: :post,   endpoint: 'v7.0/route_bookmark/%{route_id}' },
  remove_bookmark:    { method: :delete, endpoint: 'v7.0/route_bookmark/%{route_bookmark_id}' }

}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:

  • _self (Mmf::Client)

    the object that the method was called on



64
65
66
67
68
69
# File 'lib/mmf.rb', line 64

def initialize
  @client_key, @client_secret, @access_token = '','',''
  yield self
  client  = OAuth2::Client.new(client_key, client_secret)
  @client = OAuth2::AccessToken.new(client, access_token)
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



62
63
64
# File 'lib/mmf.rb', line 62

def access_token
  @access_token
end

#client_keyObject

Returns the value of attribute client_key.



62
63
64
# File 'lib/mmf.rb', line 62

def client_key
  @client_key
end

#client_secretObject

Returns the value of attribute client_secret.



62
63
64
# File 'lib/mmf.rb', line 62

def client_secret
  @client_secret
end

Instance Method Details

#apiObject



81
82
83
84
85
86
87
88
# File 'lib/mmf.rb', line 81

def api
  API_MAP.any? do |name, details|
    vars = url_params(details[:endpoint])
    context = Hash[vars.zip vars.map {|v| ":#{v}"}]
    endpoint = interpolate(details[:endpoint], context)
    puts "client.#{name}".ljust(30) + "=> [#{details[:method]}]".ljust(12) + endpoint
  end
end

#user_idObject



77
78
79
# File 'lib/mmf.rb', line 77

def user_id
  @user_id ||= me['id']
end