Class: InstapaperFull::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



8
9
10
# File 'lib/instapaper_full.rb', line 8

def initialize(options={})
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/instapaper_full.rb', line 7

def options
  @options
end

Instance Method Details

#authenticate(username, password) ⇒ Object



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

def authenticate(username,password)
  @options.delete(:oauth_token)
  @options.delete(:oauth_token_secret)
  result = connection.post 'oauth/access_token' do |r| 
    r.body = { :x_auth_username => username, :x_auth_password => password, :x_auth_mode => "client_auth" }
  end
  if result.status == 200
    data = CGI.parse(result.body)
    if data.has_key? 'oauth_token_secret'
      @options[:oauth_token] = data['oauth_token'][0]
      @options[:oauth_token_secret] = data['oauth_token_secret'][0]
      verify_credentials.each { |k,v|
        @options[k.to_sym] = v if k != 'type'
      }
    end
    return true
  else
    return false
  end
end

#authenticated?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/instapaper_full.rb', line 39

def authenticated?
  @options.has_key? :oauth_token and @options.has_key? :oauth_token_secret
end

#bookmarks_add(options = nil) ⇒ Object



85
86
87
# File 'lib/instapaper_full.rb', line 85

def bookmarks_add(options=nil)
  call('bookmarks/add',options)
end

#bookmarks_archive(options = nil) ⇒ Object



101
102
103
# File 'lib/instapaper_full.rb', line 101

def bookmarks_archive(options=nil)
  call('bookmarks/archive', options)
end

#bookmarks_delete(options = nil) ⇒ Object



89
90
91
# File 'lib/instapaper_full.rb', line 89

def bookmarks_delete(options=nil)
  call('bookmarks/delete', options)
end

#bookmarks_get_text(options = nil) ⇒ Object



113
114
115
# File 'lib/instapaper_full.rb', line 113

def bookmarks_get_text(options=nil)
  call('bookmarks/get_text', options)
end

#bookmarks_list(options = nil) ⇒ Object



77
78
79
# File 'lib/instapaper_full.rb', line 77

def bookmarks_list(options=nil)
  call('bookmarks/list', options)[2..-1] # slice off the 'meta' and 'user' from the front of the array
end

#bookmarks_move(options = nil) ⇒ Object



109
110
111
# File 'lib/instapaper_full.rb', line 109

def bookmarks_move(options=nil)
  call('bookmarks/move', options)
end

#bookmarks_star(options = nil) ⇒ Object



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

def bookmarks_star(options=nil)
  call('bookmarks/star', options)
end

#bookmarks_unarchive(options = nil) ⇒ Object



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

def bookmarks_unarchive(options=nil)
  call('bookmarks/unarchive', options)
end

#bookmarks_unstar(options = nil) ⇒ Object



97
98
99
# File 'lib/instapaper_full.rb', line 97

def bookmarks_unstar(options=nil)
  call('bookmarks/unstar', options)
end

#bookmarks_update_read_progress(options = nil) ⇒ Object



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

def bookmarks_update_read_progress(options=nil)
  call('bookmarks/update_read_progress', options)
end

#call(method, body = nil) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/instapaper_full.rb', line 64

def call(method, body = nil)
  result = connection.post(method) do |r|
    if body
      r.body = body
    end
  end
  return result.body
end

#connectionObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/instapaper_full.rb', line 12

def connection
  options = {
    :proxy => @options[:proxy],
    :ssl => {:verify => false},
    :url => "https://www.instapaper.com/api/1/"
  }
  oauth_options = { 
    :consumer_key => @options[:consumer_key],
    :consumer_secret => @options[:consumer_secret]
  }

  if authenticated?
    oauth_options[:token] = @options[:oauth_token]
    oauth_options[:token_secret] = @options[:oauth_token_secret]
  end
  
  Faraday.new(options) do |builder|
    builder.use Faraday::Request::OAuth, oauth_options
    builder.use Faraday::Request::UrlEncoded
    builder.use Faraday::Response::Logger 
    builder.adapter Faraday.default_adapter
    if authenticated?
      builder.use Faraday::Response::ParseJson
    end
  end
end

#folders_add(options = nil) ⇒ Object



121
122
123
# File 'lib/instapaper_full.rb', line 121

def folders_add(options=nil)
  call('folders/add', options)
end

#folders_delete(options = nil) ⇒ Object



125
126
127
# File 'lib/instapaper_full.rb', line 125

def folders_delete(options=nil)
  call('folders/delete', options)
end

#folders_listObject



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

def folders_list
  call('folders/list')
end

#folders_set_order(options = nil) ⇒ Object



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

def folders_set_order(options=nil)
  call('folders/set_order', options)
end

#verify_credentialsObject



73
74
75
# File 'lib/instapaper_full.rb', line 73

def verify_credentials
  call('account/verify_credentials')[0]
end