Class: Ully::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token) ⇒ Client



18
19
20
# File 'lib/ully.rb', line 18

def initialize(access_token)
    self.class.default_params :access_token => access_token
end

Class Method Details

.pretty_response(response, format) ⇒ Object

Pretty responses in terminal



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ully.rb', line 23

def self.pretty_response(response, format)
    json = JSON.parse(response.body)
    json_response = json["response"]
    if format == "json"
        JSON.pretty_generate(json_response)
    elsif format == "yaml"
        json_response.to_yaml
    else
        json_response
    end
end

Instance Method Details

#collections(format = false) ⇒ Object

Show collections



99
100
101
102
# File 'lib/ully.rb', line 99

def collections(format=false)
    response = self.class.get("/collections", :query => {:fields => "name,slug,urls,public"})
    self.class.pretty_response(response, format)
end

#collections_by_username(username, format = false) ⇒ Object

Show collections by username



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

def collections_by_username(username, format=false)
    response = self.class.get("/collections/of/"+username, :query => {:fields => "name,slug,urls,public"})
    self.class.pretty_response(response, format)
end

#create_collections(name, slug, public_collection = true, format = false) ⇒ Object

Create collections



111
112
113
114
# File 'lib/ully.rb', line 111

def create_collections(name, slug, public_collection=true, format=false)
    response = self.class.post("/collections", :body => {:name => name, :slug => slug, :public => public_collection})
    self.class.pretty_response(response, format)
end

#create_urls(collection_slug, url, title = "", description = "", format = false) ⇒ Object

Create urls



129
130
131
132
# File 'lib/ully.rb', line 129

def create_urls(collection_slug, url, title="", description="", format=false)
    response = self.class.post("/collections/"+collection_slug+"/urls", :body => {:title => title, :url => url, :description => description})
    self.class.pretty_response(response, format)
end

#delete_collections(collection_slug, format = false) ⇒ Object

Delete collections



123
124
125
126
# File 'lib/ully.rb', line 123

def delete_collections(collection_slug, format=false)
    response = self.class.delete("/collections/"+collection_slug)
    self.class.pretty_response(response, format)
end

#delete_me(format = false) ⇒ Object

Delete profile info



93
94
95
96
# File 'lib/ully.rb', line 93

def delete_me(format=false)
    response = self.class.delete("/me")
    self.class.pretty_response(response, format)
end

#delete_urls(collection_slug, url_id, format = false) ⇒ Object

Delete urls



141
142
143
144
# File 'lib/ully.rb', line 141

def delete_urls(collection_slug, url_id, format=false)
    response = self.class.delete("/collections/"+collection_slug+"/urls/"+url_id)
    self.class.pretty_response(response, format)
end

#logged_in?Boolean

Check if user is logged



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ully.rb', line 49

def logged_in?
    if File.exist?("ullyConfig.yml")
        config = YAML.load_file("ullyConfig.yml")
        if config.has_key?("access_token") && config.has_key?("role")
            true
        else
            false
        end
    else
        false
    end
end

#login(email, password) ⇒ Object

Login in user account



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ully.rb', line 36

def (email, password)
    response = self.class.post("/forgot/access_token", :body => {:email => email, :password => password})
    json_response = JSON.parse(response.body)
    config = json_response["response"]
    if config.has_key?("access_token") && config.has_key?("role")
        File.open("ullyConfig.yml", "w"){ |f| f << config.to_yaml }
        puts "Logged successfully!"
    else
        puts "Login failed. Try again!"
    end
end

#me(format = false) ⇒ Object

Show profile info



81
82
83
84
# File 'lib/ully.rb', line 81

def me(format=false)
    response = self.class.get("/me")
    self.class.pretty_response(response, format)
end

#stats(format = false) ⇒ Object

Stats of Ully



63
64
65
66
# File 'lib/ully.rb', line 63

def stats(format=false)
    response = self.class.get("/stats")
    self.class.pretty_response(response, format)
end

#stats_by_username(username, format = false) ⇒ Object

Stats of Ully



69
70
71
72
# File 'lib/ully.rb', line 69

def stats_by_username(username, format=false)
    response = self.class.get("/stats/"+username)
    self.class.pretty_response(response, format)
end

#status(format = false) ⇒ Object

Status of Ully API



75
76
77
78
# File 'lib/ully.rb', line 75

def status(format=false)
    response = self.class.get("/status")
    self.class.pretty_response(response, format)
end

#update_collections(collection_slug, name = "", slug = "", public_collection = true, format = false) ⇒ Object

Update collections



117
118
119
120
# File 'lib/ully.rb', line 117

def update_collections(collection_slug, name="", slug="", public_collection=true, format=false)
    response = self.class.put("/collections/"+collection_slug, :body => {:name => name, :slug => slug, :public => public_collection})
    self.class.pretty_response(response, format)
end

#update_me(current_password, name = "", email = "", username = "", password = "", format = false) ⇒ Object

Update profile info



87
88
89
90
# File 'lib/ully.rb', line 87

def update_me(current_password, name="", email="", username="", password="", format=false)
    response = self.class.put("/me", :body => {:name => name, :email => email, :username => username, :currentpassword => current_password, :password => password})
    self.class.pretty_response(response, format)
end

#update_urls(collection_slug, url_id, url, title = "", description = "", format = false) ⇒ Object

Update urls



135
136
137
138
# File 'lib/ully.rb', line 135

def update_urls(collection_slug, url_id, url, title="", description="", format=false)
    response = self.class.put("/collections/"+collection_slug+"/urls/"+url_id, :body => {:title => title, :url => url, :description => description})
    self.class.pretty_response(response, format)
end