Class: InstapaperFull::API

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

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



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

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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#authenticate(username, password) ⇒ Object



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

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)


37
38
39
# File 'lib/instapaper_full.rb', line 37

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

#bookmarks_add(params = {}) ⇒ Object



91
92
93
# File 'lib/instapaper_full.rb', line 91

def bookmarks_add(params = {})
  call('bookmarks/add', params)
end

#bookmarks_archive(params = {}) ⇒ Object



107
108
109
# File 'lib/instapaper_full.rb', line 107

def bookmarks_archive(params = {})
  call('bookmarks/archive', params)
end

#bookmarks_delete(params = {}) ⇒ Object



95
96
97
# File 'lib/instapaper_full.rb', line 95

def bookmarks_delete(params = {})
  call('bookmarks/delete', params)
end

#bookmarks_get_text(params = {}) ⇒ Object



119
120
121
# File 'lib/instapaper_full.rb', line 119

def bookmarks_get_text(params = {})
  call('bookmarks/get_text', params)
end

#bookmarks_list(params = {}) ⇒ Object



83
84
85
# File 'lib/instapaper_full.rb', line 83

def bookmarks_list(params = {})
  call('bookmarks/list', params)
end

#bookmarks_move(params = {}) ⇒ Object



115
116
117
# File 'lib/instapaper_full.rb', line 115

def bookmarks_move(params = {})
  call('bookmarks/move', params)
end

#bookmarks_star(params = {}) ⇒ Object



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

def bookmarks_star(params = {})
  call('bookmarks/star', params)
end

#bookmarks_unarchive(params = {}) ⇒ Object



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

def bookmarks_unarchive(params = {})
  call('bookmarks/unarchive', params)
end

#bookmarks_unstar(params = {}) ⇒ Object



103
104
105
# File 'lib/instapaper_full.rb', line 103

def bookmarks_unstar(params = {})
  call('bookmarks/unstar', params)
end

#bookmarks_update_read_progress(params = {}) ⇒ Object



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

def bookmarks_update_read_progress(params = {})
  call('bookmarks/update_read_progress', params)
end

#call(method, params = {}, connection_options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/instapaper_full.rb', line 62

def call(method, params = {}, connection_options = {})
  result = connection(connection_options).post(method) do |r|
    r.body = params unless params.empty?
  end

  if result.headers['content-type'] == 'application/json'
    JSON.parse(result.body).tap do |d|
      if error = d.find { |e| e['type'] == 'error' }
        raise InstapaperFull::API::Error.new(error['error_code'], error['message'])
      end
    end
  else
    raise InstapaperFull::API::Error.new(-1, result.body) if result.status != 200
    result.body
  end
end

#connection(options = {}) ⇒ Object



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

def connection(options = {})
  options.merge!({
    :proxy => @options[:proxy],
    :ssl => {:verify => false},
    :url => "https://www.instapaper.com/api/1/"
  })

  oauth_params = {
    :consumer_key => @options[:consumer_key],
    :consumer_secret => @options[:consumer_secret]
  }

  if authenticated?
    oauth_params[:token] = @options[:oauth_token]
    oauth_params[:token_secret] = @options[:oauth_token_secret]
  end

  Faraday.new(options) do |builder|
    builder.use Faraday::Request::OAuth, oauth_params
    builder.use Faraday::Request::UrlEncoded
    builder.adapter Faraday.default_adapter
  end
end

#folders_add(params = {}) ⇒ Object



127
128
129
# File 'lib/instapaper_full.rb', line 127

def folders_add(params = {})
  call('folders/add', params)
end

#folders_delete(params = {}) ⇒ Object



131
132
133
# File 'lib/instapaper_full.rb', line 131

def folders_delete(params = {})
  call('folders/delete', params)
end

#folders_listObject



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

def folders_list
  call('folders/list')
end

#folders_set_order(params = {}) ⇒ Object



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

def folders_set_order(params = {})
  call('folders/set_order', params)
end

#verify_credentialsObject



79
80
81
# File 'lib/instapaper_full.rb', line 79

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