Class: Myrando::MyRando

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

Defined Under Namespace

Classes: ApiError, InvalidParameterError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MyRando

Returns a new instance of MyRando.



33
34
35
36
37
38
39
40
# File 'lib/myrando.rb', line 33

def initialize(options = {})
  username = options[:username].to_uri if options [:username].kind_of?(String)
  password = options[:password].to_uri if options [:password].kind_of?(String)
  raise InvalidParameterError, "MyRando must :username attribute" if username.nil?
  raise InvalidParameterError, "MyRando must :password attribute" if password.nil?
  @connection = RandoConnection.new()
  @user_info = (username, password)
end

Instance Attribute Details

#user_infoObject

Returns the value of attribute user_info.



31
32
33
# File 'lib/myrando.rb', line 31

def 
  @user_info
end

Instance Method Details

#get_account_statusObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/myrando.rb', line 61

def ()
  resp = @connection.get("#{RANDO_API_URL}/account.json?auth_token=#{@user_info[:token]}")
  if resp.code == 200
    begin
      json = MultiJson.decode(resp.body)
      raise ApiError, "Rando returned an error: #{json['error']}" if json.has_key?('error')
      return json
    rescue MultiJson::DecodeError
      raise ApiError, "Rando returned an error:\nStatus: #{resp.code}\nBody: #{resp.body}"
    end
  else
    raise ApiError, "Rando returned an error:\nStatus: #{resp.code}\nBody: #{resp.body}"
  end
end

#get_photos(page = 1) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/myrando.rb', line 42

def get_photos(page=1)
  resp = @connection.get("#{RANDO_API_URL}/users/#{@user_info[:user_id]}/deliveries/received.json?page=#{page}&auth_token=#{@user_info[:token]}")
  if resp.code == 200
    begin
      json = MultiJson.decode(resp.body)
      raise ApiError, "Rando returned an error: #{json['error']}" if not json.kind_of?(Array) and json.has_key?('error')
      randos = []
      json.each do |r|
        randos << r
      end
      randos
    rescue MultiJson::DecodeError
      raise ApiError, "Rando returned an error:\nStatus: #{resp.code}\nBody: #{resp.body}"
    end
  else
    raise ApiError, "Rando returned an error:\nStatus: #{resp.code}\nBody: #{resp.body}"
  end
end