Class: Instapi

Inherits:
Object
  • Object
show all
Defined in:
lib/instapi.rb,
lib/instapi/version.rb

Defined Under Namespace

Classes: LoginError

Constant Summary collapse

BASE_URL =
"https://www.instapaper.com"
VERSION =
"1.4.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Instapi

Returns a new instance of Instapi.



11
12
13
# File 'lib/instapi.rb', line 11

def initialize(username, password)
  @username, @password = username, password
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



9
10
11
# File 'lib/instapi.rb', line 9

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



9
10
11
# File 'lib/instapi.rb', line 9

def username
  @username
end

Class Method Details

.text(url) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/instapi.rb', line 52

def self.text(url)
  @_client ||= HTTPClient.new
  doc = Nokogiri::HTML(@_client.get_content("#{BASE_URL}/text?u=#{url}"))
  {
    :title => doc.css('title').first.text,
    :text => doc.css('#story').first.inner_html
  }
end

Instance Method Details

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



28
29
30
# File 'lib/instapi.rb', line 28

def add(url, options = {})
  get('/api/add', {:url => url, :username => username, :password => password}.merge(options))
end

#archive(id) ⇒ Object



40
41
42
# File 'lib/instapi.rb', line 40

def archive(id)
  get("/skip/#{id}")
end

#clientObject

Raises:



15
16
17
18
19
20
21
# File 'lib/instapi.rb', line 15

def client
  return @client if @client
  @client = HTTPClient.new
  @client.post("#{BASE_URL}/user/login", :username => username, :password => password)
  raise LoginError if @client.cookies.empty?
  @client
end

#delete(id) ⇒ Object



44
45
46
# File 'lib/instapi.rb', line 44

def delete(id)
  get("/delete/#{id}")
end

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



48
49
50
# File 'lib/instapi.rb', line 48

def get(path, params = {})
  client.get_content(BASE_URL + path, params)
end

#login!Object



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

def login!
  client
  self
end

#unreadObject



32
33
34
35
36
37
38
# File 'lib/instapi.rb', line 32

def unread
  Nokogiri::HTML(get("/u")).css('.tableViewCell').map do |elem|
    id = elem[:id].scan(/tableViewCell(\d+)/)[0][0]
    link = elem.css('.tableViewCellTitleLink').first
    {:id => id, :title => link.text, :url => link[:href]}
  end
end