Class: Pinterest::Client

Inherits:
Object
  • Object
show all
Includes:
Endpoints::Authentication, Endpoints::Boards, Endpoints::Pins, Endpoints::Users
Defined in:
lib/pinterest/client.rb

Overview

A Pinterest API client.

Constant Summary collapse

API_URL =

The Pinterest API Root URL.

"https://api.pinterest.com".freeze
API_VERSION =

The Pinterest API version.

"v1".freeze
SCOPES =

The allowed authorization scopes.

["read_public", "write_public", "read_relationships", "write_relationships"].freeze
DEFAULT_LIMIT =

The maximum number of results to return by default.

50

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Endpoints::Boards

#board, #boards, #create_board, #delete_board, #edit_board, #follow_board, #following_boards, #search_my_boards, #suggested_boards, #unfollow_board

Methods included from Endpoints::Pins

#board_pins, #create_pin, #delete_pin, #edit_pin, #likes, #pin, #pins, #search_my_pins

Methods included from Endpoints::Users

#follow_interest, #follow_user, #followers, #following_users, #interests, #me, #unfollow_interest, #unfollow_user, #user

Methods included from Endpoints::Authentication

#authorization_state, #authorization_url, #fetch_access_token, #verify_access_token

Constructor Details

#initialize(access_token: nil, client_id: nil, client_secret: nil, verbose: false, &connection_setup) ⇒ Client

Creates a new client.

Parameters:

  • access_token (String) (defaults to: nil)

    The access token.

  • client_id (String) (defaults to: nil)

    The client id.

  • client_secret (String) (defaults to: nil)

    The client secret.

  • verbose (Boolean) (defaults to: false)

    If log requests.

  • connection_setup (Proc)

    Additional code to execute on the connection object.



41
42
43
44
45
46
47
48
# File 'lib/pinterest/client.rb', line 41

def initialize(access_token: nil, client_id: nil, client_secret: nil, verbose: false, &connection_setup)
  @client_id = client_id
  @client_secret = client_secret
  @access_token = access_token
  @verbose = verbose

  ensure_connection(connection_setup)
end

Instance Attribute Details

#access_tokenString

Returns The access token.

Returns:

  • (String)

    The access token.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/pinterest/client.rb', line 19

class Client
  # The Pinterest API Root URL.
  API_URL = "https://api.pinterest.com".freeze

  # The Pinterest API version.
  API_VERSION = "v1".freeze

  # The allowed authorization scopes.
  SCOPES = ["read_public", "write_public", "read_relationships", "write_relationships"].freeze

  # The maximum number of results to return by default.
  DEFAULT_LIMIT = 50

  attr_accessor :client_id, :client_secret, :access_token, :verbose, :connection

  # Creates a new client.
  #
  # @param access_token [String] The access token.
  # @param client_id [String] The client id.
  # @param client_secret [String] The client secret.
  # @param verbose [Boolean] If log requests.
  # @param connection_setup [Proc] Additional code to execute on the connection object.
  def initialize(access_token: nil, client_id: nil, client_secret: nil, verbose: false, &connection_setup)
    @client_id = client_id
    @client_secret = client_secret
    @access_token = access_token
    @verbose = verbose

    ensure_connection(connection_setup)
  end

  include Pinterest::Endpoints::Authentication
  include Pinterest::Endpoints::Users
  include Pinterest::Endpoints::Pins
  include Pinterest::Endpoints::Boards

  private

  # :nodoc:
  def ensure_connection(setup = nil)
    setup ||= ->(c) { default_connection_setup(c) }
    @connection ||= Faraday.new(url: ::Pinterest::Client::API_URL, &setup)
  end

  # :nodoc:
  def ensure_array(subject, default = [])
    subject = (subject ? [subject] : [default]).flatten unless subject.is_a?(Array)
    subject
  end

  # :nodoc:
  def ensure_param(param, error = nil)
    valid = param && !param.to_s.strip.empty?
    raise(ArgumentError, error) if error && !valid
    valid
  end

  # :nodoc:
  def default_connection_setup(c)
    c.request(:multipart)
    c.request(:url_encoded)
    c.response(:safe_oj)
    c.response(:logger) if verbose

    c.use(FaradayMiddleware::FollowRedirects)
    c.adapter(Faraday.default_adapter)
  end

  # :nodoc:
  def cleanup_params(params)
    params.reject { |_, v| !ensure_param(v) }
  end

  # :nodoc:
  # rubocop:disable Metrics/ParameterLists
  def perform_network_request(method: "GET", url: "/", query: {}, body: {}, headers: {}, authenticated: true, pagination: false, **args, &additional)
    response = connection.send(method.downcase) do |request|
      # Setup URL and headers
      setup_headers(request, url, headers, query, authenticated)

      # Handle pagination
      handle_pagination(request, args) if pagination

      # Add the body
      request.body = body

      # Run user callback
      yield(request) if additional
    end

    # Perform the call
    raise(::Pinterest::Errors.create(response)) unless response.success?
    response
  rescue Faraday::ParsingError => e
    handle_network_error(e)
  end

  # :nodoc:
  def handle_network_error(e)
    code = e.response.status
    message = /<h1>(.+?)<\/h1>/mi.match(e.response.body)

    raise(::Pinterest::Errors.class_for_code(code).new(code, message ? message[1] : "Invalid response from the server.", e.response))
  end

  # :nodoc:
  def handle_pagination(request, args)
    limit = args[:limit].to_i
    limit = DEFAULT_LIMIT if limit < 1

    request.params[:cursor] = args[:cursor] if args[:cursor]
    request.params[:limit] = limit
  end

  # :nodoc:
  def setup_headers(request, url, headers, query, authenticated)
    request.url(url)
    request.headers["Authorization"] = "Bearer #{access_token}" if authenticated
    request.headers.merge!(headers)
    request.params.merge!(query)
  end

  # :nodoc:
  def versioned_url(url)
    "/#{::Pinterest::Client::API_VERSION}/#{url.gsub(/^\//, "")}"
  end
end

#client_idObject

Returns The client id.

Returns:

  • The client id.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/pinterest/client.rb', line 19

class Client
  # The Pinterest API Root URL.
  API_URL = "https://api.pinterest.com".freeze

  # The Pinterest API version.
  API_VERSION = "v1".freeze

  # The allowed authorization scopes.
  SCOPES = ["read_public", "write_public", "read_relationships", "write_relationships"].freeze

  # The maximum number of results to return by default.
  DEFAULT_LIMIT = 50

  attr_accessor :client_id, :client_secret, :access_token, :verbose, :connection

  # Creates a new client.
  #
  # @param access_token [String] The access token.
  # @param client_id [String] The client id.
  # @param client_secret [String] The client secret.
  # @param verbose [Boolean] If log requests.
  # @param connection_setup [Proc] Additional code to execute on the connection object.
  def initialize(access_token: nil, client_id: nil, client_secret: nil, verbose: false, &connection_setup)
    @client_id = client_id
    @client_secret = client_secret
    @access_token = access_token
    @verbose = verbose

    ensure_connection(connection_setup)
  end

  include Pinterest::Endpoints::Authentication
  include Pinterest::Endpoints::Users
  include Pinterest::Endpoints::Pins
  include Pinterest::Endpoints::Boards

  private

  # :nodoc:
  def ensure_connection(setup = nil)
    setup ||= ->(c) { default_connection_setup(c) }
    @connection ||= Faraday.new(url: ::Pinterest::Client::API_URL, &setup)
  end

  # :nodoc:
  def ensure_array(subject, default = [])
    subject = (subject ? [subject] : [default]).flatten unless subject.is_a?(Array)
    subject
  end

  # :nodoc:
  def ensure_param(param, error = nil)
    valid = param && !param.to_s.strip.empty?
    raise(ArgumentError, error) if error && !valid
    valid
  end

  # :nodoc:
  def default_connection_setup(c)
    c.request(:multipart)
    c.request(:url_encoded)
    c.response(:safe_oj)
    c.response(:logger) if verbose

    c.use(FaradayMiddleware::FollowRedirects)
    c.adapter(Faraday.default_adapter)
  end

  # :nodoc:
  def cleanup_params(params)
    params.reject { |_, v| !ensure_param(v) }
  end

  # :nodoc:
  # rubocop:disable Metrics/ParameterLists
  def perform_network_request(method: "GET", url: "/", query: {}, body: {}, headers: {}, authenticated: true, pagination: false, **args, &additional)
    response = connection.send(method.downcase) do |request|
      # Setup URL and headers
      setup_headers(request, url, headers, query, authenticated)

      # Handle pagination
      handle_pagination(request, args) if pagination

      # Add the body
      request.body = body

      # Run user callback
      yield(request) if additional
    end

    # Perform the call
    raise(::Pinterest::Errors.create(response)) unless response.success?
    response
  rescue Faraday::ParsingError => e
    handle_network_error(e)
  end

  # :nodoc:
  def handle_network_error(e)
    code = e.response.status
    message = /<h1>(.+?)<\/h1>/mi.match(e.response.body)

    raise(::Pinterest::Errors.class_for_code(code).new(code, message ? message[1] : "Invalid response from the server.", e.response))
  end

  # :nodoc:
  def handle_pagination(request, args)
    limit = args[:limit].to_i
    limit = DEFAULT_LIMIT if limit < 1

    request.params[:cursor] = args[:cursor] if args[:cursor]
    request.params[:limit] = limit
  end

  # :nodoc:
  def setup_headers(request, url, headers, query, authenticated)
    request.url(url)
    request.headers["Authorization"] = "Bearer #{access_token}" if authenticated
    request.headers.merge!(headers)
    request.params.merge!(query)
  end

  # :nodoc:
  def versioned_url(url)
    "/#{::Pinterest::Client::API_VERSION}/#{url.gsub(/^\//, "")}"
  end
end

#client_secretObject

Returns The client secret.

Returns:

  • The client secret.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/pinterest/client.rb', line 19

class Client
  # The Pinterest API Root URL.
  API_URL = "https://api.pinterest.com".freeze

  # The Pinterest API version.
  API_VERSION = "v1".freeze

  # The allowed authorization scopes.
  SCOPES = ["read_public", "write_public", "read_relationships", "write_relationships"].freeze

  # The maximum number of results to return by default.
  DEFAULT_LIMIT = 50

  attr_accessor :client_id, :client_secret, :access_token, :verbose, :connection

  # Creates a new client.
  #
  # @param access_token [String] The access token.
  # @param client_id [String] The client id.
  # @param client_secret [String] The client secret.
  # @param verbose [Boolean] If log requests.
  # @param connection_setup [Proc] Additional code to execute on the connection object.
  def initialize(access_token: nil, client_id: nil, client_secret: nil, verbose: false, &connection_setup)
    @client_id = client_id
    @client_secret = client_secret
    @access_token = access_token
    @verbose = verbose

    ensure_connection(connection_setup)
  end

  include Pinterest::Endpoints::Authentication
  include Pinterest::Endpoints::Users
  include Pinterest::Endpoints::Pins
  include Pinterest::Endpoints::Boards

  private

  # :nodoc:
  def ensure_connection(setup = nil)
    setup ||= ->(c) { default_connection_setup(c) }
    @connection ||= Faraday.new(url: ::Pinterest::Client::API_URL, &setup)
  end

  # :nodoc:
  def ensure_array(subject, default = [])
    subject = (subject ? [subject] : [default]).flatten unless subject.is_a?(Array)
    subject
  end

  # :nodoc:
  def ensure_param(param, error = nil)
    valid = param && !param.to_s.strip.empty?
    raise(ArgumentError, error) if error && !valid
    valid
  end

  # :nodoc:
  def default_connection_setup(c)
    c.request(:multipart)
    c.request(:url_encoded)
    c.response(:safe_oj)
    c.response(:logger) if verbose

    c.use(FaradayMiddleware::FollowRedirects)
    c.adapter(Faraday.default_adapter)
  end

  # :nodoc:
  def cleanup_params(params)
    params.reject { |_, v| !ensure_param(v) }
  end

  # :nodoc:
  # rubocop:disable Metrics/ParameterLists
  def perform_network_request(method: "GET", url: "/", query: {}, body: {}, headers: {}, authenticated: true, pagination: false, **args, &additional)
    response = connection.send(method.downcase) do |request|
      # Setup URL and headers
      setup_headers(request, url, headers, query, authenticated)

      # Handle pagination
      handle_pagination(request, args) if pagination

      # Add the body
      request.body = body

      # Run user callback
      yield(request) if additional
    end

    # Perform the call
    raise(::Pinterest::Errors.create(response)) unless response.success?
    response
  rescue Faraday::ParsingError => e
    handle_network_error(e)
  end

  # :nodoc:
  def handle_network_error(e)
    code = e.response.status
    message = /<h1>(.+?)<\/h1>/mi.match(e.response.body)

    raise(::Pinterest::Errors.class_for_code(code).new(code, message ? message[1] : "Invalid response from the server.", e.response))
  end

  # :nodoc:
  def handle_pagination(request, args)
    limit = args[:limit].to_i
    limit = DEFAULT_LIMIT if limit < 1

    request.params[:cursor] = args[:cursor] if args[:cursor]
    request.params[:limit] = limit
  end

  # :nodoc:
  def setup_headers(request, url, headers, query, authenticated)
    request.url(url)
    request.headers["Authorization"] = "Bearer #{access_token}" if authenticated
    request.headers.merge!(headers)
    request.params.merge!(query)
  end

  # :nodoc:
  def versioned_url(url)
    "/#{::Pinterest::Client::API_VERSION}/#{url.gsub(/^\//, "")}"
  end
end

#connectionObject

Returns the value of attribute connection.



32
33
34
# File 'lib/pinterest/client.rb', line 32

def connection
  @connection
end

#connection_setupObject (readonly)

Returns Additional code to execute on the connection object.

Returns:

  • Additional code to execute on the connection object.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/pinterest/client.rb', line 19

class Client
  # The Pinterest API Root URL.
  API_URL = "https://api.pinterest.com".freeze

  # The Pinterest API version.
  API_VERSION = "v1".freeze

  # The allowed authorization scopes.
  SCOPES = ["read_public", "write_public", "read_relationships", "write_relationships"].freeze

  # The maximum number of results to return by default.
  DEFAULT_LIMIT = 50

  attr_accessor :client_id, :client_secret, :access_token, :verbose, :connection

  # Creates a new client.
  #
  # @param access_token [String] The access token.
  # @param client_id [String] The client id.
  # @param client_secret [String] The client secret.
  # @param verbose [Boolean] If log requests.
  # @param connection_setup [Proc] Additional code to execute on the connection object.
  def initialize(access_token: nil, client_id: nil, client_secret: nil, verbose: false, &connection_setup)
    @client_id = client_id
    @client_secret = client_secret
    @access_token = access_token
    @verbose = verbose

    ensure_connection(connection_setup)
  end

  include Pinterest::Endpoints::Authentication
  include Pinterest::Endpoints::Users
  include Pinterest::Endpoints::Pins
  include Pinterest::Endpoints::Boards

  private

  # :nodoc:
  def ensure_connection(setup = nil)
    setup ||= ->(c) { default_connection_setup(c) }
    @connection ||= Faraday.new(url: ::Pinterest::Client::API_URL, &setup)
  end

  # :nodoc:
  def ensure_array(subject, default = [])
    subject = (subject ? [subject] : [default]).flatten unless subject.is_a?(Array)
    subject
  end

  # :nodoc:
  def ensure_param(param, error = nil)
    valid = param && !param.to_s.strip.empty?
    raise(ArgumentError, error) if error && !valid
    valid
  end

  # :nodoc:
  def default_connection_setup(c)
    c.request(:multipart)
    c.request(:url_encoded)
    c.response(:safe_oj)
    c.response(:logger) if verbose

    c.use(FaradayMiddleware::FollowRedirects)
    c.adapter(Faraday.default_adapter)
  end

  # :nodoc:
  def cleanup_params(params)
    params.reject { |_, v| !ensure_param(v) }
  end

  # :nodoc:
  # rubocop:disable Metrics/ParameterLists
  def perform_network_request(method: "GET", url: "/", query: {}, body: {}, headers: {}, authenticated: true, pagination: false, **args, &additional)
    response = connection.send(method.downcase) do |request|
      # Setup URL and headers
      setup_headers(request, url, headers, query, authenticated)

      # Handle pagination
      handle_pagination(request, args) if pagination

      # Add the body
      request.body = body

      # Run user callback
      yield(request) if additional
    end

    # Perform the call
    raise(::Pinterest::Errors.create(response)) unless response.success?
    response
  rescue Faraday::ParsingError => e
    handle_network_error(e)
  end

  # :nodoc:
  def handle_network_error(e)
    code = e.response.status
    message = /<h1>(.+?)<\/h1>/mi.match(e.response.body)

    raise(::Pinterest::Errors.class_for_code(code).new(code, message ? message[1] : "Invalid response from the server.", e.response))
  end

  # :nodoc:
  def handle_pagination(request, args)
    limit = args[:limit].to_i
    limit = DEFAULT_LIMIT if limit < 1

    request.params[:cursor] = args[:cursor] if args[:cursor]
    request.params[:limit] = limit
  end

  # :nodoc:
  def setup_headers(request, url, headers, query, authenticated)
    request.url(url)
    request.headers["Authorization"] = "Bearer #{access_token}" if authenticated
    request.headers.merge!(headers)
    request.params.merge!(query)
  end

  # :nodoc:
  def versioned_url(url)
    "/#{::Pinterest::Client::API_VERSION}/#{url.gsub(/^\//, "")}"
  end
end

#verboseObject

Returns the value of attribute verbose.



32
33
34
# File 'lib/pinterest/client.rb', line 32

def verbose
  @verbose
end