Module: MegaphoneClient

Defined in:
lib/megaphone_client.rb,
lib/megaphone_client/episode.rb,
lib/megaphone_client/podcast.rb,
lib/megaphone_client/episode_collection.rb,
lib/megaphone_client/podcast_collection.rb

Overview

Author:

  • Jay Arella

Defined Under Namespace

Classes: ConnectionError, Episode, EpisodeCollection, Podcast, PodcastCollection

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_base_urlObject

Returns the value of attribute api_base_url.


12
13
14
# File 'lib/megaphone_client.rb', line 12

def api_base_url
  @api_base_url
end

.network_idObject

Returns the value of attribute network_id.


12
13
14
# File 'lib/megaphone_client.rb', line 12

def network_id
  @network_id
end

.organization_idObject

Returns the value of attribute organization_id.


12
13
14
# File 'lib/megaphone_client.rb', line 12

def organization_id
  @organization_id
end

.tokenObject

Returns the value of attribute token.


12
13
14
# File 'lib/megaphone_client.rb', line 12

def token
  @token
end

Class Method Details

.connection(options = {}) ⇒ Object

Note:

This is a generalized REST method that is used by both the Episode and Podcast class. If it's successful, it returns a struct representing the data. If it fails, it raises a ConnectionError.

Examples:

Get a list of podcasts with #connection

megaphone.connection({
  method: :get,
  url: "https://cms.megaphone.fm/api/podcasts"
})
#=> Array of structs representing podcasts

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :method (Symbol)

    Request method

  • :params (Hash)

    Request params in a hash

  • :payload (String)

    If the request method is `POST`, the body of the post

  • :url (String)

    Request url


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/megaphone_client.rb', line 49

def connection options={}
  request_headers = default_headers.merge({ params: options[:params] })

  begin
    response = RestClient::Request.execute(
      url: options[:url],
      method: options[:method],
      headers: request_headers,
      payload: options[:body].to_json
    )
  rescue RestClient::ExceptionWithResponse => err
    raise ConnectionError.new("Megaphone ConnectionError: #{err.response.description}, Request: #{err.response.request.method} #{err.response.request.url}")
  end

  JSON.parse(response.body, object_class: OpenStruct)
end

.default_headersObject

Returns a hash with the default request headers, includes the token.

Examples:

Calling #default_headers after initializing with a token

megaphone = MegaphoneClient.new({
  network_id: '1234',
  organization_id: '5678',
  token: '910'
})
megaphone.default_headers #=> { content_type: "application/json", authorization: "Token token=910", params: {} }

Returns:

  • a hash with the default request headers, includes the token


75
76
77
78
79
80
81
# File 'lib/megaphone_client.rb', line 75

def default_headers
  {
    content_type: "application/json",
    authorization: "Token token=#{@token}",
    params: {}
  }
end

.episodesObject

Returns a new instance of MegaphoneClient::EpisodeCollection.

Examples:

Make a new Episodes instance

megaphone.episodes #=> new MegaphoneClient::EpisodeCollection

Returns:

  • a new instance of MegaphoneClient::EpisodeCollection


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

def episodes
  self::EpisodeCollection.new
end

.new(options = {}) ⇒ Object

Returns a new instance of MegaphoneClient.

Examples:

Initialize MegaphoneClient into a variable

megaphone = MegaphoneClient.new({
  network_id: '1234',
  organization_id: '5678',
  token: '910'
})
megaphone #=> new MegaphoneClient

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :api_base_url (String)

    The api base url includes the domain and `/api` endpoint

  • :network_id (String)

    The id of the Megaphone network

  • :organization_id (String)

    The id of the Megaphone organization

  • :token (String)

    The api token taken from Megaphone's developer settings (developers.megaphone.fm/)

Returns:

  • a new instance of MegaphoneClient


27
28
29
30
31
32
33
34
# File 'lib/megaphone_client.rb', line 27

def new options={}
  @api_base_url = options[:api_base_url] || "https://cms.megaphone.fm/api"
  @network_id = options[:network_id]
  @organization_id = options[:organization_id]
  @token = options[:token]

  self
end

.podcast(id = nil) ⇒ Object

Returns a new instance of MegaphoneClient::Podcast.

Examples:

Make a new Podcast instance

megaphone.podcast("12345") #=> new MegaphoneClient::Podcast with id 12345

Returns:

  • a new instance of MegaphoneClient::Podcast


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

def podcast(id=nil)
  self::Podcast.new(id)
end

.podcastsObject

Returns a new instance of MegaphoneCilent::PodcastCollection.

Examples:

Make a new Podcasts instance

megaphone.podcasts #=> new MegaphoneClient::PodcastCollection

Returns:

  • a new instance of MegaphoneCilent::PodcastCollection


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

def podcasts
  self::PodcastCollection.new
end