Class: InstaPush

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

Defined Under Namespace

Classes: AuthFailedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ InstaPush

Returns a new instance of InstaPush.



31
32
33
34
35
# File 'lib/instapush.rb', line 31

def initialize(username, password)
  @username = username
  @password = password
  @api_url = "https://www.instapaper.com/api/"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym) ⇒ Object

Raises:

  • (NoMethodError)


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

def method_missing(sym)
  raise NoMethodError unless sym.to_s.include? '_url'
  api_url + sym.to_s.chomp('_url')
end

Instance Attribute Details

#api_urlObject (readonly)

Returns the value of attribute api_url.



29
30
31
# File 'lib/instapush.rb', line 29

def api_url
  @api_url
end

#passwordObject (readonly)

Returns the value of attribute password.



29
30
31
# File 'lib/instapush.rb', line 29

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



29
30
31
# File 'lib/instapush.rb', line 29

def username
  @username
end

Class Method Details

.authenticate(username, password = nil) ⇒ Object

Raises:



23
24
25
26
# File 'lib/instapush.rb', line 23

def authenticate(username, password = nil)
  conn = connect(username, password)
  raise AuthFailedError unless conn.authenticate
end

.connect(username, password = nil, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/instapush.rb', line 9

def connect(username, password = nil, &block)
  conn = new username, password

  if block_given?
    if block.arity == 1
      block.call(conn)
    else
      conn.instance_eval(&block)
    end
  else
    conn
  end
end

Instance Method Details

#add(url, opts = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/instapush.rb', line 46

def add(url, opts = {})
  opts.merge! :url      => url,
              :username => username,
              :password => password

  RestClient.post add_url, opts
rescue RestClient::RequestFailed
  errors << $!
  false
end

#authenticateObject



37
38
39
40
41
42
43
44
# File 'lib/instapush.rb', line 37

def authenticate
  RestClient.post authenticate_url,
                  :username => username,
                  :password => password
rescue RestClient::RequestFailed
  errors << $!
  false
end

#errorsObject



57
58
59
# File 'lib/instapush.rb', line 57

def errors
  @errors ||= []
end