Class: Me2day::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/me2day/client.rb

Defined Under Namespace

Classes: InternalServerError, UnauthenticatedError

Constant Summary collapse

DEFAULT_FORMAT =
'json'
@@apis =
%w(
  accept_friendship_request
  create_comment
  create_post
  delete_comment
  delete_post
  friendship
  get_bands
  get_comments
  get_friends
  get_friendship_requests
  get_latests 
  get_metoos
  get_person
  get_posts
  get_settings
  metoo
  noop
  track_comments
)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



40
41
42
43
44
45
46
# File 'lib/me2day/client.rb', line 40

def initialize(options={})
  @app_key = options[:app_key]
  @user_id = options[:user_id]
  @user_key = options[:user_key]
  @auth = { :uid => @user_id, :ukey => u_key(@user_key) }
  self.class.headers 'me2_application_key' => @app_key
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *args, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/me2day/client.rb', line 48

def method_missing(method_sym, *args, &block)
  if @@apis.include?(method_sym.to_s)
    id_part = args.first.is_a?(String) ? "/#{args.first}" : ''
    options = args.last.is_a?(Hash) ? args.last : {}
    format = options ? (options.delete(:format) || DEFAULT_FORMAT) : DEFAULT_FORMAT
    options.merge!(@auth)
    resp = self.class.get("/#{method_sym}#{id_part}.#{format}", :query => options)
    case resp.response.code.to_s
      when "200"
        resp.parsed_response
      when "401"
        raise UnauthenticatedError, resp.parsed_response.to_s  
      when "500"
        raise InternalServerError, resp.parsed_response.to_s
      else
        raise "Unknown Error", resp.inspect
      end       
  else    
    super
  end
end

Class Method Details

.get_auth_url(options = {}) ⇒ Object

attr_accessor :app_key



33
34
35
36
37
# File 'lib/me2day/client.rb', line 33

def get_auth_url(options={})
  raise "app_key is required" unless options[:app_key]
  self.headers 'me2_application_key' => options[:app_key]
  @auth_url ||= get("/get_auth_url.json")["url"]
end

Instance Method Details

#get(path, options = {}) ⇒ Object

General helper methods (deprecated)



72
# File 'lib/me2day/client.rb', line 72

def get(path, options={}); self.class.get(path, options).parsed_response end

#post(path, options = {}) ⇒ Object



73
# File 'lib/me2day/client.rb', line 73

def post(path, options={}); self.class.post(path, options).parsed_response end