-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,028 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ tmp | |
*.o | ||
*.a | ||
mkmf.log | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'net/instagram/config' | ||
require 'net/instagram/models' | ||
|
||
module Net | ||
module Instagram | ||
extend Config | ||
include Errors | ||
include Models | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Net | ||
module Instagram | ||
module Api | ||
class Configuration | ||
attr_accessor :client_id | ||
|
||
def initialize | ||
@client_id = ENV['INSTAGRAM_CLIENT_ID'] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'net/instagram/errors/response_error' | ||
require 'net/instagram/errors/unknown_user' | ||
require 'active_support' | ||
require 'active_support/core_ext' | ||
|
||
module Net | ||
module Instagram | ||
module Api | ||
class Request | ||
def initialize(attrs = {}) | ||
@host = 'api.instagram.com' | ||
@path = attrs.fetch :path, "/v1/#{attrs[:endpoint]}" | ||
@query = attrs[:params] if attrs[:params] | ||
@method = attrs.fetch :method, :get | ||
end | ||
|
||
def run | ||
case response = run_http_request | ||
when Net::HTTPOK | ||
JSON(response.body)['data'] | ||
else | ||
raise Errors::ResponseError, response | ||
end | ||
end | ||
|
||
private | ||
def run_http_request | ||
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| | ||
http.request http_request | ||
end | ||
end | ||
|
||
def http_request | ||
http_class = "Net::HTTP::#{@method.capitalize}".constantize | ||
@http_request ||= http_class.new(uri.request_uri) | ||
end | ||
|
||
def uri | ||
@uri ||= URI::HTTPS.build host: @host, path: @path, query: query | ||
end | ||
|
||
def query | ||
{}.tap do |query| | ||
query.merge! @query if @query | ||
query.merge! client_id: Net::Instagram.configuration.client_id | ||
end.to_param | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'net/instagram/api/configuration' | ||
|
||
module Net | ||
module Instagram | ||
module Config | ||
def configure | ||
yield configuration if block_given? | ||
end | ||
|
||
def configuration | ||
@configuration ||= Api::Configuration.new | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require 'net/instagram/errors/response_error' | ||
require 'net/instagram/errors/private_user' | ||
require 'net/instagram/errors/unknown_user' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Net | ||
module Instagram | ||
module Errors | ||
class PrivateUser < StandardError | ||
def message | ||
'Private user' | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Net | ||
module Instagram | ||
module Errors | ||
class ResponseError < StandardError | ||
attr_reader :response | ||
|
||
def initialize(response = {}) | ||
@response = response | ||
super response | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Net | ||
module Instagram | ||
module Errors | ||
class UnknownUser < StandardError | ||
def message | ||
'Unknown user' | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'net/instagram/models/user' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'net/instagram/api/request' | ||
require 'net/instagram/errors' | ||
|
||
module Net | ||
module Instagram | ||
module Models | ||
class User | ||
attr_reader :username, :follower_count | ||
|
||
def initialize(attrs = {}) | ||
@username = attrs['username'] | ||
@follower_count = attrs['counts']['followed_by'] | ||
end | ||
|
||
# Returns the existing Instagram user matching the provided attributes or | ||
# nil when the user is not found. | ||
# | ||
# @return [Net::Instagram::Models::User] when the user is found. | ||
# @return [nil] when the user is not found or has a private account. | ||
# @param [Hash] params the attributes to find a user by. | ||
# @option params [String] :username The Instagram user’s username | ||
# (case-insensitive). | ||
def self.find_by(params = {}) | ||
find_by! params | ||
rescue Errors::PrivateUser, Errors::UnknownUser | ||
nil | ||
end | ||
|
||
# Returns the existing Instagram user matching the provided attributes or | ||
# nil when the user is not found, and raises an error when the user account is private. | ||
# | ||
# @return [Net::Instagram::Models::User] the Instagram user. | ||
# @param [Hash] params the attributes to find a user by. | ||
# @option params [String] :username The Instagram user’s username | ||
# (case-insensitive). | ||
# @raise [Net::Errors::PrivateUser] if the user account is private. | ||
def self.find_by!(params = {}) | ||
find_by_username! params[:username] | ||
end | ||
|
||
private | ||
|
||
def self.find_by_username!(username) | ||
request = Api::Request.new endpoint: "users/search", params: {q: username} | ||
users = Array.wrap request.run | ||
if user = users.find{|u| u['username'].casecmp(username).zero?} | ||
find_by_id! user['id'] | ||
else | ||
raise Errors::UnknownUser | ||
end | ||
end | ||
|
||
def self.find_by_id!(id) | ||
request = Api::Request.new endpoint: "users/#{id}" | ||
new request.run | ||
rescue Errors::ResponseError => error | ||
case error.response | ||
when Net::HTTPBadRequest then raise Errors::PrivateUser | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Net | ||
VERSION = "0.1.0" | ||
VERSION = "0.1.1" | ||
end |
Oops, something went wrong.