Class: TrimApi::Trim
- Inherits:
-
Object
- Object
- TrimApi::Trim
- Defined in:
- lib/trim-api/trim-api.rb
Instance Attribute Summary collapse
-
#password ⇒ Object
Returns the value of attribute password.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Trim
constructor
A new instance of Trim.
- #trim(original_url, options = {}) ⇒ Object
- #trim_claim(reference, options = {}) ⇒ Object
- #trim_destination(trimmed_url, options = {}) ⇒ Object
- #trim_reference(trimmed_url, options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Trim
Returns a new instance of Trim.
18 19 20 21 22 |
# File 'lib/trim-api/trim-api.rb', line 18 def initialize( = {}) @client = HTTPClient.new self.username = [:username] if [:username] self.password = [:password] if [:password] end |
Instance Attribute Details
#password ⇒ Object
Returns the value of attribute password.
16 17 18 |
# File 'lib/trim-api/trim-api.rb', line 16 def password @password end |
#username ⇒ Object
Returns the value of attribute username.
15 16 17 |
# File 'lib/trim-api/trim-api.rb', line 15 def username @username end |
Instance Method Details
#trim(original_url, options = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/trim-api/trim-api.rb', line 24 def trim(original_url, = {}) raise ArgumentError.new(":url to shorten is required") if original_url.nil? response = @client.get_content(create_url("trim_url", :url => original_url)) data = JSON.parse(response) unless data["status"]["result"] == "OK" raise TrimError.new(data["status"]["code"],data["status"]["message"]) else "http://tr.im/#{data['url']}" end end |
#trim_claim(reference, options = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/trim-api/trim-api.rb', line 59 def trim_claim(reference, = {}) raise ArgumentError.new(":url reference is required. Use trim_reference to obtain this.") if reference.nil? raise ArgumentError.new(":username and password are required") if (username.nil? || password.nil?) response = @client.get_content(create_url("trim_claim", .merge!({:reference => reference, :username => username, :password => password}))) data = JSON.parse(response) unless data["status"]["result"] == "OK" raise TrimError.new(data["status"]["code"],data["status"]["message"]) else "URL successfully claimed and added to your account" end end |
#trim_destination(trimmed_url, options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/trim-api/trim-api.rb', line 35 def trim_destination(trimmed_url, = {}) raise ArgumentError.new(":trimmed url is required") if trimmed_url.nil? raise ArgumentError.new(":username and password are required") if (username.nil? || password.nil?) response = @client.get_content(create_url("trim_destination",.merge!({:trimpath => trimmed_url, :username => username, :password => password}))) data = JSON.parse(response) unless data["status"]["result"] == "OK" raise TrimError.new(data["status"]["code"],data["status"]["message"]) else data["destination"] end end |
#trim_reference(trimmed_url, options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/trim-api/trim-api.rb', line 47 def trim_reference(trimmed_url, = {}) raise ArgumentError.new(":trimmed url is required") if trimmed_url.nil? raise ArgumentError.new(":username and password are required") if (username.nil? || password.nil?) response = @client.get_content(create_url("trim_reference", .merge!({:trimpath => trimmed_url, :username => username, :password => password}))) data = JSON.parse(response) unless data["status"]["result"] == "OK" raise TrimError.new(data["status"]["code"],data["status"]["message"]) else data["reference"] end end |